Skip to content

Commit

Permalink
refactorings and type hints for collection & client result types
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Oct 8, 2023
1 parent 949f683 commit 3838b48
Show file tree
Hide file tree
Showing 19 changed files with 43 additions and 54 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ target/
.envrc
Pipfile
Pipfile.lock


# Self signed certificates
examples/*.pem
10 changes: 5 additions & 5 deletions examples/sharepoint/sites/get_admins.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion examples/sharepoint/tenant/get_all_sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
9 changes: 0 additions & 9 deletions examples/sharepoint/tenant/insights.py

This file was deleted.

2 changes: 1 addition & 1 deletion examples/sharepoint/tenant/print_tenant_settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Prints tenant settings
"""

from pprint import pprint
Expand Down
4 changes: 2 additions & 2 deletions office365/directory/groups/lifecycle_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion office365/directory/users/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion office365/onedrive/workbooks/tables/columns/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion office365/outlook/mail/messages/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions office365/runtime/client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
8 changes: 0 additions & 8 deletions office365/runtime/client_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
4 changes: 3 additions & 1 deletion office365/runtime/client_runtime_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion office365/sharepoint/search/setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion office365/sharepoint/sites/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
)


class SitePropertiesCollection(EntityCollection):
class SitePropertiesCollection(EntityCollection[SiteProperties]):
"""SiteProperties resource collection"""

def __init__(self, context, resource_path=None):
Expand Down
5 changes: 4 additions & 1 deletion office365/sharepoint/tenant/administration/tenant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 3 additions & 1 deletion office365/sharepoint/webs/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions tests/onedrive/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion tests/sharepoint/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 3838b48

Please sign in to comment.