-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactorings & improvements (fluent interface for ClientResult) Relea…
…se 2.3.3
- Loading branch information
1 parent
190e9dd
commit 98448bb
Showing
61 changed files
with
296 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,7 +189,7 @@ Usage | |
import adal | ||
from office365.graph_client import GraphClient | ||
|
||
def acquire_token(): | ||
def acquire_token_func(): | ||
authority_url = 'https://login.microsoftonline.com/{tenant_id_or_name}' | ||
auth_ctx = adal.AuthenticationContext(authority_url) | ||
token = auth_ctx.acquire_token_with_client_credentials( | ||
|
@@ -211,7 +211,7 @@ The example demonstrates how to send an email via [Microsoft Graph endpoint](htt | |
```python | ||
from office365.graph_client import GraphClient | ||
|
||
client = GraphClient(acquire_token) | ||
client = GraphClient(acquire_token_func) | ||
|
||
message_json = { | ||
"Message": { | ||
|
@@ -232,8 +232,7 @@ message_json = { | |
} | ||
|
||
login_name = "[email protected]" | ||
client.users[login_name].send_mail(message_json) | ||
client.execute_query() | ||
client.users[login_name].send_mail(message_json).execute_query() | ||
``` | ||
|
||
|
||
|
@@ -251,7 +250,7 @@ is used to obtain token | |
```python | ||
import msal | ||
|
||
def acquire_token(): | ||
def acquire_token_func(): | ||
""" | ||
Acquire token via MSAL | ||
""" | ||
|
@@ -279,10 +278,8 @@ which corresponds to [`list available drives` endpoint](https://docs.microsoft.c | |
from office365.graph_client import GraphClient | ||
|
||
tenant_name = "contoso.onmicrosoft.com" | ||
client = GraphClient(acquire_token) | ||
drives = client.drives | ||
client.load(drives) | ||
client.execute_query() | ||
client = GraphClient(acquire_token_func) | ||
drives = client.drives.get().execute_query() | ||
for drive in drives: | ||
print("Drive url: {0}".format(drive.web_url)) | ||
``` | ||
|
@@ -292,12 +289,9 @@ for drive in drives: | |
|
||
```python | ||
from office365.graph_client import GraphClient | ||
client = GraphClient(acquire_token) | ||
client = GraphClient(acquire_token_func) | ||
# retrieve drive properties | ||
drive = client.users["{user_id_or_principal_name}"].drive | ||
client.load(drive) | ||
client.execute_query() | ||
|
||
drive = client.users["{user_id_or_principal_name}"].drive.get().execute_query() | ||
# download files from OneDrive into local folder | ||
with tempfile.TemporaryDirectory() as path: | ||
download_files(drive.root, path) | ||
|
@@ -307,15 +301,12 @@ where | |
|
||
```python | ||
def download_files(remote_folder, local_path): | ||
drive_items = remote_folder.children | ||
client.load(drive_items) | ||
client.execute_query() | ||
drive_items = remote_folder.children.get().execute_query() | ||
for drive_item in drive_items: | ||
if not drive_item.file.is_server_object_null: # is file? | ||
# download file content | ||
with open(os.path.join(local_path, drive_item.name), 'wb') as local_file: | ||
drive_item.download(local_file) | ||
client.execute_query() | ||
drive_item.download(local_file).execute_query() | ||
``` | ||
|
||
|
||
|
@@ -339,9 +330,8 @@ which corresponds to [`Create team` endpoint](https://docs.microsoft.com/en-us/g | |
```python | ||
from office365.graph_client import GraphClient | ||
tenant_name = "contoso.onmicrosoft.com" | ||
client = GraphClient(tenant_name, acquire_token) | ||
new_team = client.groups["{group_id}"].add_team() | ||
client.execute_query() | ||
client = GraphClient(tenant_name, acquire_token_func) | ||
new_team = client.groups["{group_id}"].add_team().execute_query_retry() | ||
``` | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,22 @@ | ||
# -------------------------------------------------------------------------- | ||
# Example demonstrates how to export OneDrive files into local file system | ||
# -------------------------------------------------------------------------- | ||
|
||
import os | ||
import tempfile | ||
|
||
from examples import acquire_token_client_credentials | ||
from office365.graph_client import GraphClient | ||
from tests import test_user_principal_name | ||
|
||
|
||
def download_files(remote_folder, local_path): | ||
""" | ||
:type remote_folder: office365.onedrive.driveItem.DriveItem | ||
:type local_path: str | ||
""" | ||
drive_items = remote_folder.children.get().execute_query() | ||
for drive_item in drive_items: | ||
if not drive_item.file.is_server_object_null: # is file? | ||
# download file content | ||
with open(os.path.join(local_path, drive_item.name), 'wb') as local_file: | ||
drive_item.download(local_file) | ||
client.execute_query() | ||
print("File '{0}' has been downloaded".format(local_file.name)) | ||
|
||
|
||
# -------------------------------------------------------------------------- | ||
# Example demonstrates how to export OneDrive files into local file system | ||
# -------------------------------------------------------------------------- | ||
|
||
# connect | ||
client = GraphClient(acquire_token_client_credentials) | ||
drive = client.users[test_user_principal_name].drive # get user's drive | ||
with tempfile.TemporaryDirectory() as local_path: | ||
drive_items = drive.root.children.get().execute_query() | ||
file_items = [item for item in drive_items if not item.file.is_server_object_null] # files only | ||
for drive_item in file_items: | ||
with open(os.path.join(local_path, drive_item.name), 'wb') as local_file: | ||
drive_item.download(local_file).execute_query() # download file content | ||
print("File '{0}' has been downloaded".format(local_file.name)) | ||
|
||
# load drive properties | ||
target_user_name = settings.get('first_account_name') | ||
drive = client.users[target_user_name].drive | ||
# download files from OneDrive | ||
with tempfile.TemporaryDirectory() as path: | ||
download_files(drive.root, path) | ||
print("Done") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from xml.etree import ElementTree | ||
from office365.sharepoint.client_context import ClientContext | ||
from office365.sharepoint.lists.list import List | ||
from tests import test_client_credentials, test_team_site_url | ||
|
||
ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials) | ||
|
||
|
||
def is_custom_list(list_object): | ||
xml = ElementTree.fromstring(list_object.properties["SchemaXml"]) | ||
scope_id = xml.attrib['ScopeId'] | ||
return True | ||
|
||
|
||
lists = ctx.web.lists.select(["Title", "SchemaXml"]).top(10).get().execute_query() | ||
lists_to_delete = [l for l in lists if is_custom_list(l)] | ||
for list_obj in lists_to_delete: # type: List | ||
print(f"Deleting list .. {list_obj.title}") | ||
# list_obj.delete_object().execute_query() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
import json | ||
|
||
from office365.sharepoint.client_context import ClientContext | ||
from office365.sharepoint.search.searchRequest import SearchRequest | ||
from office365.sharepoint.search.searchService import SearchService | ||
from tests import test_site_url, test_user_credentials | ||
|
||
ctx = ClientContext(test_site_url).with_credentials(test_user_credentials) | ||
|
||
search = SearchService(ctx) | ||
request = SearchRequest("IsDocument:1") | ||
result = search.post_query(request) | ||
ctx.execute_query() | ||
relevant_results = result.PrimaryQueryResult.RelevantResults | ||
result = search.post_query(request).execute_query() | ||
relevant_results = result.value.PrimaryQueryResult.RelevantResults | ||
for i in relevant_results['Table']['Rows']: | ||
cells = relevant_results['Table']['Rows'][i]['Cells'] | ||
print(cells[6]['Value']) | ||
print(json.dumps(cells, sort_keys=True, indent=4)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class BodyType: | ||
def __init__(self): | ||
pass | ||
|
||
html = "html" | ||
text = "text" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Importance: | ||
def __init__(self): | ||
pass | ||
|
||
low = "low" | ||
normal = "normal" | ||
high = "high" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.