marketplace.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from collections.abc import Sequence
  2. import requests
  3. from yarl import URL
  4. from configs import dify_config
  5. from core.helper.download import download_with_size_limit
  6. from core.plugin.entities.marketplace import MarketplacePluginDeclaration
  7. def get_plugin_pkg_url(plugin_unique_identifier: str):
  8. return (URL(str(dify_config.MARKETPLACE_API_URL)) / "api/v1/plugins/download").with_query(
  9. unique_identifier=plugin_unique_identifier
  10. )
  11. def download_plugin_pkg(plugin_unique_identifier: str):
  12. url = str(get_plugin_pkg_url(plugin_unique_identifier))
  13. return download_with_size_limit(url, dify_config.PLUGIN_MAX_PACKAGE_SIZE)
  14. def batch_fetch_plugin_manifests(plugin_ids: list[str]) -> Sequence[MarketplacePluginDeclaration]:
  15. if len(plugin_ids) == 0:
  16. return []
  17. url = str(URL(str(dify_config.MARKETPLACE_API_URL)) / "api/v1/plugins/batch")
  18. response = requests.post(url, json={"plugin_ids": plugin_ids})
  19. response.raise_for_status()
  20. return [MarketplacePluginDeclaration(**plugin) for plugin in response.json()["data"]["plugins"]]
  21. def record_install_plugin_event(plugin_unique_identifier: str):
  22. url = str(URL(str(dify_config.MARKETPLACE_API_URL)) / "api/v1/stats/plugins/install_count")
  23. response = requests.post(url, json={"unique_identifier": plugin_unique_identifier})
  24. response.raise_for_status()