From 9d1f2ea23890bb72b59a830e17be7f969a54a1e2 Mon Sep 17 00:00:00 2001 From: Karel van Klink <karel.vanklink@geant.org> Date: Thu, 2 May 2024 15:49:22 +0200 Subject: [PATCH] make sharepoint client blocking --- gso/services/sharepoint.py | 56 ++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/gso/services/sharepoint.py b/gso/services/sharepoint.py index 8547c3b3..0af9e20f 100644 --- a/gso/services/sharepoint.py +++ b/gso/services/sharepoint.py @@ -1,5 +1,7 @@ """Sharepoint service used for creating new list items.""" +import asyncio + from azure.identity.aio import CertificateCredential from msgraph import GraphServiceClient from msgraph.generated.models.field_value_set import FieldValueSet @@ -27,26 +29,34 @@ class SPClient: self.site_id = sp_params.site_id self.list_ids = sp_params.list_ids - async def get_site(self) -> Site | None: + def get_site(self) -> Site | None: """Get the SharePoint site that this orchestrator connects to.""" - return await self.client.sites.by_site_id(self.site_id).get() - async def get_list_items(self, list_name: str) -> ListItemCollectionResponse | None: + async def _get_site(): + return await self.client.sites.by_site_id(self.site_id).get() + + return asyncio.run(_get_site()) + + def get_list_items(self, list_name: str) -> ListItemCollectionResponse | None: """Get list items from a given list in SharePoint. :param str list_name: The name of the list. """ - query_params = ItemsRequestBuilder.ItemsRequestBuilderGetQueryParameters(expand=["fields"]) - request_configuration = ItemsRequestBuilder.ItemsRequestBuilderGetRequestConfiguration( - query_parameters=query_params - ) - return ( - await self.client.sites.by_site_id(self.site_id) - .lists.by_list_id(self.list_ids[list_name]) - .items.get(request_configuration=request_configuration) - ) - async def add_list_item(self, list_name: str, fields: dict[str, str]) -> str: + async def _get_list_items(): + query_params = ItemsRequestBuilder.ItemsRequestBuilderGetQueryParameters(expand=["fields"]) + request_configuration = ItemsRequestBuilder.ItemsRequestBuilderGetRequestConfiguration( + query_parameters=query_params + ) + return ( + await self.client.sites.by_site_id(self.site_id) + .lists.by_list_id(self.list_ids[list_name]) + .items.get(request_configuration=request_configuration) + ) + + return asyncio.run(_get_list_items()) + + def add_list_item(self, list_name: str, fields: dict[str, str]) -> str: """Add a new entry to a SharePoint list. :param str list_name: The name of the list. @@ -54,12 +64,16 @@ class SPClient: :return str: The URL of the list in which a new item has been created. """ - request_body = ListItem(fields=FieldValueSet(additional_data=fields)) - new_item = ( - await self.client.sites.by_site_id(self.site_id) - .lists.by_list_id(self.list_ids[list_name]) - .items.post(request_body) - ) - # Strip the last part of the URL, since we want the link to the list, not the list item. - return new_item.web_url.rsplit("/", 1)[0] + async def _new_list_item(): + request_body = ListItem(fields=FieldValueSet(additional_data=fields)) + new_item = ( + await self.client.sites.by_site_id(self.site_id) + .lists.by_list_id(self.list_ids[list_name]) + .items.post(request_body) + ) + + # Strip the last part of the URL, since we want the link to the list, not the list item. + return new_item.web_url.rsplit("/", 1)[0] + + return asyncio.run(_new_list_item()) -- GitLab