From a90ffed6a330b8a117dc018562830e78e5323746 Mon Sep 17 00:00:00 2001 From: Oleh Paduchak Date: Tue, 5 Nov 2024 14:50:41 +0200 Subject: [PATCH] cleaned up wb settings creation --- addon_imps/storage/box_dot_com.py | 4 ++-- addon_imps/storage/dataverse.py | 8 +++++--- addon_imps/storage/dropbox.py | 4 ++-- addon_imps/storage/figshare.py | 4 ++-- addon_imps/storage/github.py | 4 ++-- addon_imps/storage/gitlab.py | 6 +++--- addon_imps/storage/google_drive.py | 4 ++-- addon_imps/storage/onedrive.py | 4 ++-- addon_imps/storage/s3.py | 6 +++--- addon_service/common/waterbutler_compat.py | 6 +----- addon_toolkit/interfaces/storage.py | 2 +- 11 files changed, 25 insertions(+), 27 deletions(-) diff --git a/addon_imps/storage/box_dot_com.py b/addon_imps/storage/box_dot_com.py index 6f90d1c1..38395e8b 100644 --- a/addon_imps/storage/box_dot_com.py +++ b/addon_imps/storage/box_dot_com.py @@ -24,9 +24,9 @@ async def list_root_items(self, page_cursor: str = "") -> storage.ItemSampleResu total_count=1, ) - async def build_wb_config(self, root_folder_id: str, host: str) -> dict: + async def build_wb_config(self) -> dict: return { - "folder": root_folder_id.split(":")[1], + "folder": self.config.connected_root_id.split(":")[1], } async def get_item_info(self, item_id: str) -> storage.ItemResult: diff --git a/addon_imps/storage/dataverse.py b/addon_imps/storage/dataverse.py index 278bc37f..159c0c0d 100644 --- a/addon_imps/storage/dataverse.py +++ b/addon_imps/storage/dataverse.py @@ -28,8 +28,10 @@ class DataverseStorageImp(storage.StorageAddonHttpRequestorImp): async def get_external_account_id(self, _: dict[str, str]) -> str: return "" - async def build_wb_config(self, root_folder_id: str, host: str) -> dict: - match = DATASET_REGEX.match(root_folder_id) + async def build_wb_config( + self, + ) -> dict: + match = DATASET_REGEX.match(self.config.connected_root_id) async with self.network.GET(f"datasets/{match['id']}") as response: content = await response.json_content() parsed = parse_dataset(content) @@ -37,7 +39,7 @@ async def build_wb_config(self, root_folder_id: str, host: str) -> dict: "id": match["id"], "name": parsed.item_name, "doi": content["data"]["latestVersion"]["datasetPersistentId"], - "host": urlparse(host).hostname, + "host": urlparse(self.config.external_api_url).hostname, } async def list_root_items(self, page_cursor: str = "") -> storage.ItemSampleResult: diff --git a/addon_imps/storage/dropbox.py b/addon_imps/storage/dropbox.py index e6a88b5b..51f66ccb 100644 --- a/addon_imps/storage/dropbox.py +++ b/addon_imps/storage/dropbox.py @@ -16,9 +16,9 @@ async def get_external_account_id(self, _: dict[str, str]) -> str: async def list_root_items(self, page_cursor: str = "") -> storage.ItemSampleResult: return await self.list_child_items(item_id="", page_cursor=page_cursor) - async def build_wb_config(self, root_folder_id: str, _: str) -> dict: + async def build_wb_config(self) -> dict: return { - "folder": root_folder_id, + "folder": self.config.connected_root_id, } async def get_item_info(self, item_id: str) -> storage.ItemResult: diff --git a/addon_imps/storage/figshare.py b/addon_imps/storage/figshare.py index 5cd6e044..4d815b39 100644 --- a/addon_imps/storage/figshare.py +++ b/addon_imps/storage/figshare.py @@ -39,8 +39,8 @@ async def list_root_items(self, page_cursor: str = "") -> storage.ItemSampleResu next_sample_cursor=str(page_cursor + 1), ) - async def build_wb_config(self, root_folder_id: str, _: str) -> dict: - segments = root_folder_id.split("/") + async def build_wb_config(self) -> dict: + segments = self.config.connected_root_id.split("/") return {"container_type": segments[0], "container_id": segments[1]} async def get_item_info(self, item_id: str) -> storage.ItemResult: diff --git a/addon_imps/storage/github.py b/addon_imps/storage/github.py index 554c1a47..162fbd90 100644 --- a/addon_imps/storage/github.py +++ b/addon_imps/storage/github.py @@ -35,8 +35,8 @@ async def list_root_items(self, page_cursor: str = "") -> storage.ItemSampleResu items=items, total_count=len(items) ).with_cursor(self._create_offset_cursor(len(items), page_cursor)) - async def build_wb_config(self, root_folder_id: str, _: str) -> dict: - owner, repo, _ = self._parse_github_item_id(root_folder_id) + async def build_wb_config(self) -> dict: + owner, repo, _ = self._parse_github_item_id(self.config.connected_root_id) return { "owner": owner, "repo": repo, diff --git a/addon_imps/storage/gitlab.py b/addon_imps/storage/gitlab.py index c4a69c34..3369d0c6 100644 --- a/addon_imps/storage/gitlab.py +++ b/addon_imps/storage/gitlab.py @@ -33,11 +33,11 @@ async def get_external_account_id(self, _: dict[str, str]) -> str: resp_json = await response.json_content() return resp_json.get("user_id", "") - async def build_wb_config(self, root_folder_id: str, host: str) -> dict: - item_id = ItemId.parse(root_folder_id) + async def build_wb_config(self) -> dict: + item_id = ItemId.parse(self.config.connected_root_id) owner, repo_name = unquote_plus(item_id.repo_id).split("/") repo = await self._get_repository(item_id.repo_id) - url = urlparse(host) + url = urlparse(self.config.external_api_url) return { "repo": repo_name, "repo_id": str(repo.id), diff --git a/addon_imps/storage/google_drive.py b/addon_imps/storage/google_drive.py index 85758b2d..d4c8231a 100644 --- a/addon_imps/storage/google_drive.py +++ b/addon_imps/storage/google_drive.py @@ -27,8 +27,8 @@ async def get_external_account_id(self, _: dict[str, str]) -> str: async def list_root_items(self, page_cursor: str = "") -> storage.ItemSampleResult: return ItemSampleResult(items=[await self.get_item_info("root")], total_count=1) - async def build_wb_config(self, root_folder_id: str, _: str) -> dict: - return {"folder": {"id": root_folder_id}} + async def build_wb_config(self) -> dict: + return {"folder": {"id": self.config.connected_root_id}} async def get_item_info(self, item_id: str) -> storage.ItemResult: item_id = item_id or "root" diff --git a/addon_imps/storage/onedrive.py b/addon_imps/storage/onedrive.py index adcc8c00..8b88c2dc 100644 --- a/addon_imps/storage/onedrive.py +++ b/addon_imps/storage/onedrive.py @@ -30,11 +30,11 @@ async def list_root_items(self, page_cursor: str = "") -> storage.ItemSampleResu total_count=1, ) - async def build_wb_config(self, root_folder_id: str, _: str) -> dict: + async def build_wb_config(self) -> dict: async with self.network.GET("me/drive") as _response: json = await _response.json_content() return { - "folder": root_folder_id, + "folder": self.config.connected_root_id, "drive_id": json.get("id"), } diff --git a/addon_imps/storage/s3.py b/addon_imps/storage/s3.py index 3129d958..6ca6da0a 100644 --- a/addon_imps/storage/s3.py +++ b/addon_imps/storage/s3.py @@ -25,10 +25,10 @@ def create_client(credentials: AccessKeySecretKeyCredentials): "s3", aws_access_key_id=access_key, aws_secret_access_key=secret_key ) - async def build_wb_config(self, root_folder_id: str, _: str) -> dict: + async def build_wb_config(self) -> dict: return { - "bucket": root_folder_id.split(":/")[0], - "id": root_folder_id, + "bucket": self.config.connected_root_id.split(":/")[0], + "id": self.config.connected_root_id, "encrypt_uploads": True, # TODO: somehow include this into settings } diff --git a/addon_service/common/waterbutler_compat.py b/addon_service/common/waterbutler_compat.py index df7c1c8d..48a67e48 100644 --- a/addon_service/common/waterbutler_compat.py +++ b/addon_service/common/waterbutler_compat.py @@ -42,8 +42,4 @@ def _config_for_waterbutler(self, configured_storage_addon: ConfiguredStorageAdd configured_storage_addon.base_account, configured_storage_addon.config, ) - imp_config = async_to_sync(imp.build_wb_config)( - configured_storage_addon.root_folder, - configured_storage_addon.base_account.api_base_url, - ) - return imp_config + return async_to_sync(imp.build_wb_config)() diff --git a/addon_toolkit/interfaces/storage.py b/addon_toolkit/interfaces/storage.py index 827f6a04..dd270ee3 100644 --- a/addon_toolkit/interfaces/storage.py +++ b/addon_toolkit/interfaces/storage.py @@ -160,7 +160,7 @@ class StorageAddonImp(AddonImp): config: StorageConfig - async def build_wb_config(self, root_folder_id: str, host: str) -> dict: + async def build_wb_config(self) -> dict: return {}