From 96c31aeb2b5c20f1fd35779646ac4caf6b0c389a Mon Sep 17 00:00:00 2001 From: vgrem Date: Tue, 20 Feb 2024 21:40:19 +0200 Subject: [PATCH] refactorings & examples updates --- .../directory/applications/app_password.py | 13 +- examples/onedrive/lists/enum_in_site.py | 4 +- examples/onedrive/lists/get_metadata.py | 4 +- examples/onedrive/workbook/work_with_excel.py | 6 +- examples/sharepoint/lists/clear.py | 3 +- generator/import_metadata.py | 4 +- generator/metadata/SharePoint.xml | 365 +++++++++++++++++- office365/directory/password_credential.py | 3 + office365/onedrive/termstore/store.py | 6 +- office365/runtime/auth/sts_profile.py | 1 - office365/sharepoint/entity.py | 8 +- office365/sharepoint/folders/coloring.py | 4 + office365/sharepoint/taxonomy/stores/store.py | 6 +- office365/teams/operations/async_operation.py | 12 +- 14 files changed, 397 insertions(+), 42 deletions(-) diff --git a/examples/directory/applications/app_password.py b/examples/directory/applications/app_password.py index 641dc040e..6b1cdfa0d 100644 --- a/examples/directory/applications/app_password.py +++ b/examples/directory/applications/app_password.py @@ -3,10 +3,17 @@ https://learn.microsoft.com/en-us/graph/api/application-addpassword?view=graph-rest-1.0 """ from office365.graph_client import GraphClient -from tests import test_client_credentials -from tests.graph_case import acquire_token_by_username_password +from tests import ( + test_client_credentials, + test_client_id, + test_password, + test_tenant, + test_username, +) -client = GraphClient(acquire_token_by_username_password) +client = GraphClient.with_username_and_password( + test_tenant, test_client_id, test_username, test_password +) target_app = client.applications.get_by_app_id(test_client_credentials.clientId) result = target_app.add_password("Password friendly name").execute_query() print(result.value) diff --git a/examples/onedrive/lists/enum_in_site.py b/examples/onedrive/lists/enum_in_site.py index a0a6cbbca..77bd5f753 100644 --- a/examples/onedrive/lists/enum_in_site.py +++ b/examples/onedrive/lists/enum_in_site.py @@ -5,9 +5,9 @@ """ from office365.graph_client import GraphClient -from tests.graph_case import acquire_token_by_client_credentials +from tests import test_client_id, test_client_secret, test_tenant -client = GraphClient(acquire_token_by_client_credentials) +client = GraphClient.with_client_secret(test_tenant, test_client_id, test_client_secret) lists = client.sites.root.lists.get().execute_query() for lst in lists: print(lst.display_name) diff --git a/examples/onedrive/lists/get_metadata.py b/examples/onedrive/lists/get_metadata.py index e86d353de..5094d880d 100644 --- a/examples/onedrive/lists/get_metadata.py +++ b/examples/onedrive/lists/get_metadata.py @@ -5,8 +5,8 @@ """ from office365.graph_client import GraphClient -from tests.graph_case import acquire_token_by_client_credentials +from tests import test_client_id, test_client_secret, test_tenant -client = GraphClient(acquire_token_by_client_credentials) +client = GraphClient.with_client_secret(test_tenant, test_client_id, test_client_secret) lib = client.sites.root.lists["Documents"].get().execute_query() print(lib.web_url) diff --git a/examples/onedrive/workbook/work_with_excel.py b/examples/onedrive/workbook/work_with_excel.py index 7083c0a1f..2ef8285c1 100644 --- a/examples/onedrive/workbook/work_with_excel.py +++ b/examples/onedrive/workbook/work_with_excel.py @@ -5,9 +5,11 @@ """ from examples.onedrive import ensure_workbook_sample from office365.graph_client import GraphClient -from tests.graph_case import acquire_token_by_username_password +from tests import test_client_id, test_password, test_tenant, test_username -client = GraphClient(acquire_token_by_username_password) +client = GraphClient.with_username_and_password( + test_tenant, test_client_id, test_username, test_password +) workbook = ensure_workbook_sample(client) print("Creating a session...") diff --git a/examples/sharepoint/lists/clear.py b/examples/sharepoint/lists/clear.py index c834e081b..8e363d1a4 100644 --- a/examples/sharepoint/lists/clear.py +++ b/examples/sharepoint/lists/clear.py @@ -6,10 +6,11 @@ def print_progress(items_count): + # type: (int) -> None print("List items count: {0}".format(target_list.item_count)) ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials) target_list = ctx.web.lists.get_by_title("Contacts_Large") -target_list.clear().get().execute_query() +target_list.clear().get().execute_batch() print("List items count: {0}".format(target_list.item_count)) diff --git a/generator/import_metadata.py b/generator/import_metadata.py index cddc3963e..16166b566 100644 --- a/generator/import_metadata.py +++ b/generator/import_metadata.py @@ -21,13 +21,13 @@ def export_to_file(path, content): "--endpoint", dest="endpoint", help="Import metadata endpoint", - default="microsoftgraph", + default="sharepoint", ) parser.add_argument( "-p", "--path", dest="path", - default="./metadata/MicrosoftGraph.xml", + default="./metadata/SharePoint.xml", help="Import metadata endpoint", ) diff --git a/generator/metadata/SharePoint.xml b/generator/metadata/SharePoint.xml index 176784a42..d12f091e0 100644 --- a/generator/metadata/SharePoint.xml +++ b/generator/metadata/SharePoint.xml @@ -168,11 +168,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + @@ -343,6 +368,11 @@ + + + + + @@ -353,6 +383,12 @@ + + + + + + @@ -415,11 +451,24 @@ + + + + + + + + + + + + + @@ -1343,6 +1392,22 @@ + + + + + + + + + + + + + + + + @@ -8885,6 +8950,7 @@ + @@ -13160,6 +13226,7 @@ + @@ -15917,10 +15984,6 @@ - - - - @@ -16059,6 +16122,10 @@ + + + + @@ -16329,6 +16396,12 @@ + + + + + + @@ -16457,6 +16530,10 @@ + + + + @@ -16766,6 +16843,10 @@ + + + + @@ -19092,6 +19173,8 @@ + + @@ -19160,6 +19243,22 @@ + + + + + + + + + + + + + + + + @@ -19563,6 +19662,27 @@ + + + + + + + + + + + + + + + + + + + + + @@ -19575,12 +19695,33 @@ + + + + + + + + + + + + + + + + + + + + + @@ -19683,6 +19824,9 @@ + + + @@ -19698,6 +19842,9 @@ + + + @@ -20592,6 +20739,9 @@ + + + @@ -21573,6 +21723,12 @@ + + + + + + @@ -22957,6 +23113,9 @@ + + + @@ -22969,6 +23128,9 @@ + + + @@ -24742,7 +24904,7 @@ - + @@ -26266,6 +26428,9 @@ + + + @@ -26882,9 +27047,6 @@ - - - @@ -27258,14 +27420,31 @@ + + + + + + + + + + + + + + + + + @@ -27343,6 +27522,8 @@ + + @@ -31140,15 +31321,24 @@ + + + + + + + + + @@ -31169,6 +31359,11 @@ + + + + + @@ -31660,10 +31855,12 @@ + + @@ -34468,6 +34665,7 @@ + @@ -34481,7 +34679,6 @@ - @@ -34489,7 +34686,6 @@ - @@ -34500,6 +34696,10 @@ + + + + @@ -34958,8 +35158,11 @@ + + + @@ -36568,7 +36771,7 @@ - + @@ -36684,6 +36887,7 @@ + @@ -38669,7 +38873,7 @@ - + @@ -38690,6 +38894,9 @@ + + + @@ -39255,6 +39462,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -41463,6 +41788,22 @@ + + + + + + + + + + + + + + + + diff --git a/office365/directory/password_credential.py b/office365/directory/password_credential.py index 4a04b06f3..67e805d3f 100644 --- a/office365/directory/password_credential.py +++ b/office365/directory/password_credential.py @@ -31,3 +31,6 @@ def __init__( self.keyId = key_id self.startDateTime = start_datetime self.endDateTime = end_datetime + + def __str__(self): + return self.displayName or self.entity_type_name diff --git a/office365/onedrive/termstore/store.py b/office365/onedrive/termstore/store.py index f0bbb750d..c1a473920 100644 --- a/office365/onedrive/termstore/store.py +++ b/office365/onedrive/termstore/store.py @@ -3,7 +3,6 @@ from office365.entity import Entity from office365.entity_collection import EntityCollection from office365.onedrive.termstore.groups.collection import GroupCollection -from office365.onedrive.termstore.groups.group import Group from office365.onedrive.termstore.sets.collection import SetCollection from office365.onedrive.termstore.sets.set import Set from office365.runtime.paths.resource_path import ResourcePath @@ -23,10 +22,9 @@ def _sets_loaded(sets): def _groups_loaded(groups): # type: (GroupCollection) -> None - for g in groups: - self.context.load(g.sets, after_loaded=_sets_loaded) + [grp.sets.get().after_execute(_sets_loaded) for grp in groups] - self.context.load(self.groups, after_loaded=_groups_loaded) + self.groups.get().after_execute(_groups_loaded) return return_type diff --git a/office365/runtime/auth/sts_profile.py b/office365/runtime/auth/sts_profile.py index 6ce8ed3d5..5f00337f5 100644 --- a/office365/runtime/auth/sts_profile.py +++ b/office365/runtime/auth/sts_profile.py @@ -6,7 +6,6 @@ class STSProfile(object): def __init__(self, authority_url, environment): """ - :type authority_url: str """ self.authorityUrl = authority_url diff --git a/office365/sharepoint/entity.py b/office365/sharepoint/entity.py index f0a1615bb..a5e5d987d 100644 --- a/office365/sharepoint/entity.py +++ b/office365/sharepoint/entity.py @@ -2,6 +2,8 @@ from typing_extensions import Self +from office365.runtime.auth.client_credential import ClientCredential +from office365.runtime.auth.user_credential import UserCredential from office365.runtime.client_object import ClientObject from office365.runtime.queries.delete_entity import DeleteEntityQuery from office365.runtime.queries.update_entity import UpdateEntityQuery @@ -19,10 +21,8 @@ def execute_batch(self, items_per_batch=100, success_callback=None): return self.context.execute_batch(items_per_batch, success_callback) def with_credentials(self, credentials): - """ - :type self: T - :type credentials: UserCredential or ClientCredential - """ + # type: (UserCredential|ClientCredential) -> Self + """ """ self.context.with_credentials(credentials) return self diff --git a/office365/sharepoint/folders/coloring.py b/office365/sharepoint/folders/coloring.py index 3c16f75ec..8b5b5d937 100644 --- a/office365/sharepoint/folders/coloring.py +++ b/office365/sharepoint/folders/coloring.py @@ -1,5 +1,8 @@ +from typing_extensions import Self + from office365.runtime.queries.service_operation import ServiceOperationQuery from office365.sharepoint.entity import Entity +from office365.sharepoint.folders.coloring_information import FolderColoringInformation from office365.sharepoint.folders.folder import Folder @@ -34,6 +37,7 @@ def create_folder( return return_type def stamp_color(self, decoded_url, coloring_information): + # type: (str, FolderColoringInformation) -> Self """ :param str decoded_url: :param FolderColoringInformation coloring_information: diff --git a/office365/sharepoint/taxonomy/stores/store.py b/office365/sharepoint/taxonomy/stores/store.py index 981d87d45..1d58178c4 100644 --- a/office365/sharepoint/taxonomy/stores/store.py +++ b/office365/sharepoint/taxonomy/stores/store.py @@ -26,10 +26,12 @@ def _sets_loaded(sets): def _groups_loaded(col): # type: (TermGroupCollection) -> None - for grp in col: + [ grp.get_term_sets_by_name(label, lcid).after_execute(_sets_loaded) + for grp in col + ] - self.context.load(self.term_groups, after_loaded=_groups_loaded) + self.term_groups.get().after_execute(_groups_loaded) return return_type def search_term(self, label, set_id=None, parent_term_id=None, language_tag=None): diff --git a/office365/teams/operations/async_operation.py b/office365/teams/operations/async_operation.py index 11f7fd382..85c9d2353 100644 --- a/office365/teams/operations/async_operation.py +++ b/office365/teams/operations/async_operation.py @@ -38,9 +38,7 @@ def poll_for_status( """ def _poll_for_status(polling_number): - """ - :type polling_number: int - """ + # type: (int) -> None if polling_number > max_polling_count: if callable(failure_callback): failure_callback(self) @@ -48,16 +46,16 @@ def _poll_for_status(polling_number): raise TypeError("The maximum polling count has been reached") def _verify_status(return_type): - if self.status != status_type: + if return_type.status != status_type: time.sleep(polling_interval_secs) _poll_for_status(polling_number + 1) else: if callable(success_callback): - success_callback(self) + success_callback(return_type) - self.context.load(self, after_loaded=_verify_status) + self.get().after_execute(_verify_status, execute_first=True) - self.ensure_property("id", _poll_for_status, 1) + _poll_for_status(1) return self @property