You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

marketplace.py 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. marketplace_api_url = URL(str(dify_config.MARKETPLACE_API_URL))
  8. def get_plugin_pkg_url(plugin_unique_identifier: str) -> str:
  9. return str((marketplace_api_url / "api/v1/plugins/download").with_query(unique_identifier=plugin_unique_identifier))
  10. def download_plugin_pkg(plugin_unique_identifier: str):
  11. return download_with_size_limit(get_plugin_pkg_url(plugin_unique_identifier), dify_config.PLUGIN_MAX_PACKAGE_SIZE)
  12. def batch_fetch_plugin_manifests(plugin_ids: list[str]) -> Sequence[MarketplacePluginDeclaration]:
  13. if len(plugin_ids) == 0:
  14. return []
  15. url = str(marketplace_api_url / "api/v1/plugins/batch")
  16. response = requests.post(url, json={"plugin_ids": plugin_ids})
  17. response.raise_for_status()
  18. return [MarketplacePluginDeclaration(**plugin) for plugin in response.json()["data"]["plugins"]]
  19. def batch_fetch_plugin_manifests_ignore_deserialization_error(
  20. plugin_ids: list[str],
  21. ) -> Sequence[MarketplacePluginDeclaration]:
  22. if len(plugin_ids) == 0:
  23. return []
  24. url = str(marketplace_api_url / "api/v1/plugins/batch")
  25. response = requests.post(url, json={"plugin_ids": plugin_ids})
  26. response.raise_for_status()
  27. result: list[MarketplacePluginDeclaration] = []
  28. for plugin in response.json()["data"]["plugins"]:
  29. try:
  30. result.append(MarketplacePluginDeclaration(**plugin))
  31. except Exception as e:
  32. pass
  33. return result
  34. def record_install_plugin_event(plugin_unique_identifier: str):
  35. url = str(marketplace_api_url / "api/v1/stats/plugins/install_count")
  36. response = requests.post(url, json={"unique_identifier": plugin_unique_identifier})
  37. response.raise_for_status()