dify/api/core/plugin/manager/plugin.py

50 lines
1.8 KiB
Python
Raw Normal View History

2024-09-20 21:35:19 +08:00
from collections.abc import Generator
from core.plugin.entities.plugin_daemon import InstallPluginMessage
from core.plugin.manager.base import BasePluginManager
class PluginInstallationManager(BasePluginManager):
def fetch_plugin_by_identifier(self, tenant_id: str, identifier: str) -> bool:
# urlencode the identifier
return self._request_with_plugin_daemon_response(
2024-09-23 13:09:46 +08:00
"GET", f"plugin/{tenant_id}/fetch/identifier", bool, params={"plugin_unique_identifier": identifier}
2024-09-20 21:35:19 +08:00
)
def install_from_pkg(self, tenant_id: str, pkg: bytes) -> Generator[InstallPluginMessage, None, None]:
"""
Install a plugin from a package.
"""
# using multipart/form-data to encode body
body = {"dify_pkg": ("dify_pkg", pkg, "application/octet-stream")}
return self._request_with_plugin_daemon_response_stream(
2024-09-23 13:09:46 +08:00
"POST", f"plugin/{tenant_id}/install/pkg", InstallPluginMessage, data=body
2024-09-20 21:35:19 +08:00
)
def install_from_identifier(self, tenant_id: str, identifier: str) -> bool:
"""
Install a plugin from an identifier.
"""
# exception will be raised if the request failed
2024-09-20 21:50:44 +08:00
return self._request_with_plugin_daemon_response(
2024-09-20 21:35:19 +08:00
"POST",
2024-09-23 13:09:46 +08:00
f"plugin/{tenant_id}/install/identifier",
2024-09-20 21:50:44 +08:00
bool,
2024-09-23 13:09:46 +08:00
params={
"plugin_unique_identifier": identifier,
2024-09-20 21:35:19 +08:00
},
data={
"plugin_unique_identifier": identifier,
},
)
2024-09-20 21:50:44 +08:00
def uninstall(self, tenant_id: str, identifier: str) -> bool:
"""
Uninstall a plugin.
"""
return self._request_with_plugin_daemon_response(
2024-09-23 13:09:46 +08:00
"DELETE", f"plugin/{tenant_id}/uninstall", bool, params={"plugin_unique_identifier": identifier}
2024-09-20 21:50:44 +08:00
)