From 3838b480c9d6b9e429ba81d093255e0e7bd8e6c2 Mon Sep 17 00:00:00 2001 From: vgrem Date: Sun, 8 Oct 2023 14:54:21 +0300 Subject: [PATCH] refactorings and type hints for collection & client result types --- .gitignore | 4 ++++ examples/sharepoint/sites/get_admins.py | 10 +++++----- examples/sharepoint/tenant/get_all_sites.py | 2 +- examples/sharepoint/tenant/insights.py | 9 --------- .../sharepoint/tenant/print_tenant_settings.py | 2 +- office365/directory/groups/lifecycle_policy.py | 4 ++-- office365/directory/users/user.py | 4 +++- .../workbooks/tables/columns/collection.py | 2 +- office365/outlook/mail/messages/message.py | 2 +- office365/runtime/client_request.py | 11 ----------- office365/runtime/client_result.py | 8 -------- office365/runtime/client_runtime_context.py | 4 +++- office365/sharepoint/search/setting.py | 2 +- office365/sharepoint/sites/site.py | 2 +- .../sites/properties_collection.py | 2 +- .../sharepoint/tenant/administration/tenant.py | 5 ++++- office365/sharepoint/webs/web.py | 4 +++- tests/onedrive/test_excel.py | 18 +++++++++++------- tests/sharepoint/test_web.py | 2 +- 19 files changed, 43 insertions(+), 54 deletions(-) delete mode 100644 examples/sharepoint/tenant/insights.py diff --git a/.gitignore b/.gitignore index 366f65928..8314eb8d6 100644 --- a/.gitignore +++ b/.gitignore @@ -78,3 +78,7 @@ target/ .envrc Pipfile Pipfile.lock + + +# Self signed certificates +examples/*.pem diff --git a/examples/sharepoint/sites/get_admins.py b/examples/sharepoint/sites/get_admins.py index 3343d77d8..a2e82217d 100644 --- a/examples/sharepoint/sites/get_admins.py +++ b/examples/sharepoint/sites/get_admins.py @@ -1,11 +1,11 @@ """ - +Retrieves site collection administrators """ -import json from office365.sharepoint.client_context import ClientContext -from tests import test_client_credentials, test_team_site_url +from tests import test_admin_credentials, test_site_url -client = ClientContext(test_team_site_url).with_credentials(test_client_credentials) +client = ClientContext(test_site_url).with_credentials(test_admin_credentials) result = client.site.get_site_administrators().execute_query() -print(json.dumps(result.value.to_json(), indent=4)) +for info in result.value: + print(info) diff --git a/examples/sharepoint/tenant/get_all_sites.py b/examples/sharepoint/tenant/get_all_sites.py index d0bdfbd88..cf7efb1ae 100644 --- a/examples/sharepoint/tenant/get_all_sites.py +++ b/examples/sharepoint/tenant/get_all_sites.py @@ -12,5 +12,5 @@ result = admin_client.tenant.get_site_properties_from_sharepoint_by_filters( "" ).execute_query() -for i, siteProps in enumerate(result): # type: SiteProperties +for i, siteProps in enumerate(result): # type: int, SiteProperties print("({0} of {1}) {2}".format(i, len(result), siteProps.url)) diff --git a/examples/sharepoint/tenant/insights.py b/examples/sharepoint/tenant/insights.py deleted file mode 100644 index 38fb934a9..000000000 --- a/examples/sharepoint/tenant/insights.py +++ /dev/null @@ -1,9 +0,0 @@ -""" - -""" -from office365.sharepoint.tenant.administration.tenant import Tenant -from tests import test_admin_credentials, test_admin_site_url - -tenant = Tenant.from_url(test_admin_site_url).with_credentials(test_admin_credentials) -result = tenant.get_collaboration_insights_data().execute_query() -print(result.value) diff --git a/examples/sharepoint/tenant/print_tenant_settings.py b/examples/sharepoint/tenant/print_tenant_settings.py index 595793b5e..31f484087 100644 --- a/examples/sharepoint/tenant/print_tenant_settings.py +++ b/examples/sharepoint/tenant/print_tenant_settings.py @@ -1,5 +1,5 @@ """ - +Prints tenant settings """ from pprint import pprint diff --git a/office365/directory/groups/lifecycle_policy.py b/office365/directory/groups/lifecycle_policy.py index ac8dd5d30..5f155695d 100644 --- a/office365/directory/groups/lifecycle_policy.py +++ b/office365/directory/groups/lifecycle_policy.py @@ -21,7 +21,7 @@ def add_group(self, group_id): :param str group_id: The identifier of the group to remove from the policy. """ - return_type = ClientResult.from_boolean(self.context) + return_type = ClientResult[bool](self.context) payload = {"groupId": group_id} qry = ServiceOperationQuery(self, "addGroup", None, payload, None, return_type) self.context.add_query(qry) @@ -33,7 +33,7 @@ def remove_group(self, group_id): :param str group_id: The identifier of the group to add to the policy. """ - return_type = ClientResult(self.context, bool()) + return_type = ClientResult[bool](self.context) payload = {"groupId": group_id} qry = ServiceOperationQuery( self, "removeGroup", None, payload, None, return_type diff --git a/office365/directory/users/user.py b/office365/directory/users/user.py index 582a39cd4..20aa55496 100644 --- a/office365/directory/users/user.py +++ b/office365/directory/users/user.py @@ -293,7 +293,9 @@ def find_meeting_times( "returnSuggestionReasons": return_suggestion_reasons, "minimumAttendeePercentage": minimum_attendee_percentage, } - return_type = ClientResult(self.context, MeetingTimeSuggestionsResult()) + return_type = ClientResult[MeetingTimeSuggestionsResult]( + self.context, MeetingTimeSuggestionsResult() + ) qry = ServiceOperationQuery( self, "findMeetingTimes", None, payload, None, return_type ) diff --git a/office365/onedrive/workbooks/tables/columns/collection.py b/office365/onedrive/workbooks/tables/columns/collection.py index 7be52a559..a88a7bc86 100644 --- a/office365/onedrive/workbooks/tables/columns/collection.py +++ b/office365/onedrive/workbooks/tables/columns/collection.py @@ -26,7 +26,7 @@ def add(self, index, name, values=None): def count(self): """""" - return_type = ClientResult(self.context, int()) + return_type = ClientResult[int](self.context) qry = FunctionQuery(self, "count", None, return_type) self.context.add_query(qry) return return_type diff --git a/office365/outlook/mail/messages/message.py b/office365/outlook/mail/messages/message.py index ce4a24805..3144f50b2 100644 --- a/office365/outlook/mail/messages/message.py +++ b/office365/outlook/mail/messages/message.py @@ -80,7 +80,7 @@ def get_content(self): """ Get MIME content of a message """ - return_type = ClientResult(self.context) + return_type = ClientResult[str](self.context) qry = FunctionQuery(self, "$value", None, return_type) self.context.add_query(qry) return return_type diff --git a/office365/runtime/client_request.py b/office365/runtime/client_request.py index 23059768a..6c20d7938 100644 --- a/office365/runtime/client_request.py +++ b/office365/runtime/client_request.py @@ -26,17 +26,6 @@ def build_request(self, query): """ pass - def build_custom_request(self, query): - """ - Builds a request - - :type query: office365.runtime.queries.client_query.ClientQuery - :rtype: office365.runtime.http.request_options.RequestOptions - """ - request = self.build_request(query) - self.beforeExecute.notify(request) - return request - @abstractmethod def process_response(self, response, query): """ diff --git a/office365/runtime/client_result.py b/office365/runtime/client_result.py index 05172a5de..2fa6f2f5f 100644 --- a/office365/runtime/client_result.py +++ b/office365/runtime/client_result.py @@ -71,11 +71,3 @@ def execute_query_retry( failure_callback=failure_callback, ) return self - - @staticmethod - def from_boolean(context): - """ - :type context: office365.runtime.client_runtime_context.ClientRuntimeContext - :rtype: ClientResult[bool] - """ - return ClientResult(context, bool()) diff --git a/office365/runtime/client_runtime_context.py b/office365/runtime/client_runtime_context.py index 3b410ec3f..e09c2a24c 100644 --- a/office365/runtime/client_runtime_context.py +++ b/office365/runtime/client_runtime_context.py @@ -36,7 +36,9 @@ def build_request(self, query): :type query: office365.runtime.queries.client_query.ClientQuery """ self._current_query = query - return self.pending_request().build_custom_request(query) + request = self.pending_request().build_request(query) + self.pending_request().beforeExecute.notify(request) + return request def execute_query_retry( self, diff --git a/office365/sharepoint/search/setting.py b/office365/sharepoint/search/setting.py index 603f1d41d..a7d4fb3ab 100644 --- a/office365/sharepoint/search/setting.py +++ b/office365/sharepoint/search/setting.py @@ -78,7 +78,7 @@ def export_search_reports( def ping_admin_endpoint(self): """ """ - return_type = ClientResult.from_boolean(self.context) + return_type = ClientResult[bool](self.context) qry = ServiceOperationQuery( self, "PingAdminEndpoint", None, None, None, return_type ) diff --git a/office365/sharepoint/sites/site.py b/office365/sharepoint/sites/site.py index 132750877..a7facf168 100644 --- a/office365/sharepoint/sites/site.py +++ b/office365/sharepoint/sites/site.py @@ -225,7 +225,7 @@ def get_site_administrators(self): """ return_type = ClientResult( self.context, ClientValueCollection(SiteAdministratorsInfo) - ) + ) # type: ClientResult[ClientValueCollection[SiteAdministratorsInfo]] def _site_loaded(): self.context.tenant.get_site_administrators(self.id, return_type) diff --git a/office365/sharepoint/tenant/administration/sites/properties_collection.py b/office365/sharepoint/tenant/administration/sites/properties_collection.py index aabd425be..4986a01c5 100644 --- a/office365/sharepoint/tenant/administration/sites/properties_collection.py +++ b/office365/sharepoint/tenant/administration/sites/properties_collection.py @@ -7,7 +7,7 @@ ) -class SitePropertiesCollection(EntityCollection): +class SitePropertiesCollection(EntityCollection[SiteProperties]): """SiteProperties resource collection""" def __init__(self, context, resource_path=None): diff --git a/office365/sharepoint/tenant/administration/tenant.py b/office365/sharepoint/tenant/administration/tenant.py index 09865537a..6574af8f1 100644 --- a/office365/sharepoint/tenant/administration/tenant.py +++ b/office365/sharepoint/tenant/administration/tenant.py @@ -128,7 +128,10 @@ def get_onedrive_site_sharing_insights(self, query_mode): def get_collaboration_insights_data(self): """""" - return_type = ClientResult(self.context, CollaborationInsightsData()) + return_type = ClientResult[CollaborationInsightsData]( + self.context, CollaborationInsightsData() + ) + qry = ServiceOperationQuery( self, "GetCollaborationInsightsData", None, None, None, return_type ) diff --git a/office365/sharepoint/webs/web.py b/office365/sharepoint/webs/web.py index 05146ef0d..b015b93e2 100644 --- a/office365/sharepoint/webs/web.py +++ b/office365/sharepoint/webs/web.py @@ -651,7 +651,9 @@ def get_sharing_link_data(self, link_url): :param str link_url: A URL that is either a tokenized sharing link or a canonical URL for a document """ - return_type = ClientResult(self.context, SharingLinkData()) + return_type = ClientResult( + self.context, SharingLinkData() + ) # type: ClientResult[SharingLinkData] payload = {"linkUrl": link_url} qry = ServiceOperationQuery( self, "GetSharingLinkData", None, payload, None, return_type diff --git a/tests/onedrive/test_excel.py b/tests/onedrive/test_excel.py index a8b4a7128..d685bb849 100644 --- a/tests/onedrive/test_excel.py +++ b/tests/onedrive/test_excel.py @@ -56,30 +56,34 @@ def test5_create_table_column(self): column = self.__class__.table.columns.add(3, "Column4").execute_query() self.assertIsNotNone(column.resource_path) - def test6_list_table_columns(self): + def test6_create_table_column_count(self): + result = self.__class__.table.columns.count().execute_query() + self.assertGreater(result.value, 0) + + def test7_list_table_columns(self): columns = self.__class__.table.columns.get().execute_query() self.assertIsNotNone(columns.resource_path) - def test7_list_table_rows(self): + def test8_list_table_rows(self): rows = self.__class__.table.rows.get().execute_query() self.assertIsNotNone(rows.resource_path) - def test8_create_table_rows(self): + def test9_create_table_rows(self): rows = self.__class__.table.rows.add( [["Val11", "Val12", "Val13", "Val14"]] ).execute_query() self.assertIsNotNone(rows.resource_path) - def test9_table_range(self): + def test_10_table_range(self): result = self.__class__.table.range().execute_query() self.assertIsNotNone(result.address) - def test_10_delete_workbook_table(self): + def test_11_delete_workbook_table(self): self.__class__.table.delete_object().execute_query() - # def test_11_workbook_create_session(self): + # def test_12_workbook_create_session(self): # result = self.__class__.target_item.workbook.create_session().execute_query() # self.assertIsNotNone(result.value) - # def test_12_workbook_close_session(self): + # def test_13_workbook_close_session(self): # self.__class__.target_item.workbook.close_session().execute_query() diff --git a/tests/sharepoint/test_web.py b/tests/sharepoint/test_web.py index 9604de69a..dcd52d260 100644 --- a/tests/sharepoint/test_web.py +++ b/tests/sharepoint/test_web.py @@ -179,7 +179,7 @@ def test_24_get_list_item_by_path(self): def test_25_parse_datetime(self): today = str(datetime.today()) - result = self.client.web.try_parse_datetime(today).execute_query() + result = self.client.web.parse_datetime(today).execute_query() self.assertIsNotNone(result.value) # def test_26_list_acs_service_principals(self):