From b58f8dd7b468bc52610803b54ab9442345faa93f Mon Sep 17 00:00:00 2001 From: Junyan Qin <1010553892@qq.com> Date: Fri, 11 Oct 2024 02:00:02 +0800 Subject: [PATCH] feat: download pkg from marketplace (#9184) --- api/.env.example | 5 ++++- api/configs/feature/__init__.py | 11 +++++++++++ api/core/helper/marketplace.py | 13 +++++++++++++ api/services/plugin/plugin_service.py | 3 ++- api/tests/integration_tests/.env.example | 5 ++++- .../unit_tests/core/helper/test_marketplace.py | 7 +++++++ 6 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 api/core/helper/marketplace.py create mode 100644 api/tests/unit_tests/core/helper/test_marketplace.py diff --git a/api/.env.example b/api/.env.example index f1ab74b433..2f5cb8f3f8 100644 --- a/api/.env.example +++ b/api/.env.example @@ -297,4 +297,7 @@ PLUGIN_API_KEY=lYkiYYT6owG+71oLerGzA7GXCgOT++6ovaezWAjpCjf+Sjc3ZtU+qUEi+vRjI/+Xb PLUGIN_API_URL=http://127.0.0.1:5002 PLUGIN_REMOTE_INSTALL_PORT=5003 PLUGIN_REMOTE_INSTALL_HOST=localhost -INNER_API_KEY=QaHbTe77CtuXmsfyhR7+vRjI/+XbV1AaFy691iy+kGDv2Jvy0/eAh8Y1 \ No newline at end of file +INNER_API_KEY=QaHbTe77CtuXmsfyhR7+vRjI/+XbV1AaFy691iy+kGDv2Jvy0/eAh8Y1 + +# Marketplace configuration +MARKETPLACE_API_URL=https://marketplace.dify.ai diff --git a/api/configs/feature/__init__.py b/api/configs/feature/__init__.py index dc17571ca0..7d5896bc05 100644 --- a/api/configs/feature/__init__.py +++ b/api/configs/feature/__init__.py @@ -137,6 +137,16 @@ class PluginConfig(BaseSettings): default=5003, ) +class MarketplaceConfig(BaseSettings): + """ + Configuration for marketplace + """ + + MARKETPLACE_API_URL: HttpUrl = Field( + description="Marketplace API URL", + default="https://marketplace.dify.ai", + ) + class EndpointConfig(BaseSettings): """ @@ -636,6 +646,7 @@ class FeatureConfig( BillingConfig, CodeExecutionSandboxConfig, PluginConfig, + MarketplaceConfig, DataSetConfig, EndpointConfig, FileAccessConfig, diff --git a/api/core/helper/marketplace.py b/api/core/helper/marketplace.py new file mode 100644 index 0000000000..dc3fad6d4f --- /dev/null +++ b/api/core/helper/marketplace.py @@ -0,0 +1,13 @@ +from yarl import URL + +from configs import dify_config +from core.helper.download import download_with_size_limit + + +def get_plugin_pkg_url(plugin_unique_identifier: str): + return (URL(str(dify_config.MARKETPLACE_API_URL)) / "api/v1/plugins/download").with_query(unique_identifier=plugin_unique_identifier) + + +def download_plugin_pkg(plugin_unique_identifier: str): + url = str(get_plugin_pkg_url(plugin_unique_identifier)) + return download_with_size_limit(url, 15 * 1024 * 1024) diff --git a/api/services/plugin/plugin_service.py b/api/services/plugin/plugin_service.py index 92ea3caa3f..94d8858dc6 100644 --- a/api/services/plugin/plugin_service.py +++ b/api/services/plugin/plugin_service.py @@ -2,6 +2,7 @@ from collections.abc import Generator from mimetypes import guess_type from core.helper.download import download_with_size_limit +from core.helper.marketplace import download_plugin_pkg from core.plugin.entities.plugin import PluginEntity, PluginInstallationSource from core.plugin.entities.plugin_daemon import InstallPluginMessage, PluginDaemonInnerError from core.plugin.manager.asset import PluginAssetManager @@ -83,7 +84,7 @@ class PluginService: """ manager = PluginInstallationManager() - pkg = b"" + pkg = download_plugin_pkg(plugin_unique_identifier) try: yield from manager.install_from_pkg( diff --git a/api/tests/integration_tests/.env.example b/api/tests/integration_tests/.env.example index 8bb1ab96fc..452aa0ad86 100644 --- a/api/tests/integration_tests/.env.example +++ b/api/tests/integration_tests/.env.example @@ -87,4 +87,7 @@ ZHINAO_API_KEY= # Plugin configuration PLUGIN_API_KEY= PLUGIN_API_URL= -INNER_API_KEY= \ No newline at end of file +INNER_API_KEY= + +# Marketplace configuration +MARKETPLACE_API_URL= diff --git a/api/tests/unit_tests/core/helper/test_marketplace.py b/api/tests/unit_tests/core/helper/test_marketplace.py new file mode 100644 index 0000000000..51011a574a --- /dev/null +++ b/api/tests/unit_tests/core/helper/test_marketplace.py @@ -0,0 +1,7 @@ +from core.helper.marketplace import download_plugin_pkg + + +def test_download_plugin_pkg(): + pkg = download_plugin_pkg("yeuoly/google:0.0.1@4ff79ee644987e5b744d9c5b7a735d459fe66f26b28724326a7834d7e459e708") + assert pkg is not None + assert len(pkg) > 0