|
| 1 | +"""Nextcloud API for declaring TaskProcessing provider.""" |
| 2 | + |
| 3 | +import contextlib |
| 4 | +import dataclasses |
| 5 | + |
| 6 | +from ..._exceptions import NextcloudException, NextcloudExceptionNotFound |
| 7 | +from ..._misc import require_capabilities |
| 8 | +from ..._session import AsyncNcSessionApp, NcSessionApp |
| 9 | + |
| 10 | +_EP_SUFFIX: str = "ai_provider/task_processing" |
| 11 | + |
| 12 | + |
| 13 | +@dataclasses.dataclass |
| 14 | +class TaskProcessingProvider: |
| 15 | + """TaskProcessing provider description.""" |
| 16 | + |
| 17 | + def __init__(self, raw_data: dict): |
| 18 | + self._raw_data = raw_data |
| 19 | + |
| 20 | + @property |
| 21 | + def name(self) -> str: |
| 22 | + """Unique ID for the provider.""" |
| 23 | + return self._raw_data["name"] |
| 24 | + |
| 25 | + @property |
| 26 | + def display_name(self) -> str: |
| 27 | + """Providers display name.""" |
| 28 | + return self._raw_data["display_name"] |
| 29 | + |
| 30 | + @property |
| 31 | + def task_type(self) -> str: |
| 32 | + """The TaskType provided by this provider.""" |
| 33 | + return self._raw_data["task_type"] |
| 34 | + |
| 35 | + def __repr__(self): |
| 36 | + return f"<{self.__class__.__name__} name={self.name}, type={self.task_type}>" |
| 37 | + |
| 38 | + |
| 39 | +class _TaskProcessingProviderAPI: |
| 40 | + """API for TaskProcessing providers, available as **nc.providers.task_processing.<method>**.""" |
| 41 | + |
| 42 | + def __init__(self, session: NcSessionApp): |
| 43 | + self._session = session |
| 44 | + |
| 45 | + def register(self, name: str, display_name: str, task_type: str) -> None: |
| 46 | + """Registers or edit the TaskProcessing provider.""" |
| 47 | + require_capabilities("app_api", self._session.capabilities) |
| 48 | + params = { |
| 49 | + "name": name, |
| 50 | + "displayName": display_name, |
| 51 | + "taskType": task_type, |
| 52 | + } |
| 53 | + self._session.ocs("POST", f"{self._session.ae_url}/{_EP_SUFFIX}", json=params) |
| 54 | + |
| 55 | + def unregister(self, name: str, not_fail=True) -> None: |
| 56 | + """Removes TaskProcessing provider.""" |
| 57 | + require_capabilities("app_api", self._session.capabilities) |
| 58 | + try: |
| 59 | + self._session.ocs("DELETE", f"{self._session.ae_url}/{_EP_SUFFIX}", params={"name": name}) |
| 60 | + except NextcloudExceptionNotFound as e: |
| 61 | + if not not_fail: |
| 62 | + raise e from None |
| 63 | + |
| 64 | + def next_task(self, provider_ids: [str], task_types: [str]): |
| 65 | + """Get the next task processing task from Nextcloud""" |
| 66 | + with contextlib.suppress(NextcloudException): |
| 67 | + return self._session.ocs( |
| 68 | + "GET", |
| 69 | + "/ocs/v2.php/taskprocessing/tasks_provider/next", |
| 70 | + json={"providerIds": provider_ids, "taskTypeIds": task_types}, |
| 71 | + ) |
| 72 | + |
| 73 | + def report_result(self, task_id: int, output: [str, str] = None, error_message: str = None) -> None: |
| 74 | + """Report results of the task processing to Nextcloud.""" |
| 75 | + with contextlib.suppress(NextcloudException): |
| 76 | + return self._session.ocs( |
| 77 | + "POST", |
| 78 | + f"/ocs/v2.php/taskprocessing/tasks_provider/{task_id}/result", |
| 79 | + json={"taskId": task_id, "output": output, "errorMessage": error_message}, |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +class _AsyncTaskProcessingProviderAPI: |
| 84 | + """Async API for TaskProcessing providers.""" |
| 85 | + |
| 86 | + def __init__(self, session: AsyncNcSessionApp): |
| 87 | + self._session = session |
| 88 | + |
| 89 | + async def register(self, name: str, display_name: str, task_type: str) -> None: |
| 90 | + """Registers or edit the TaskProcessing provider.""" |
| 91 | + require_capabilities("app_api", await self._session.capabilities) |
| 92 | + params = { |
| 93 | + "name": name, |
| 94 | + "displayName": display_name, |
| 95 | + "taskType": task_type, |
| 96 | + } |
| 97 | + await self._session.ocs("POST", f"{self._session.ae_url}/{_EP_SUFFIX}", json=params) |
| 98 | + |
| 99 | + async def unregister(self, name: str, not_fail=True) -> None: |
| 100 | + """Removes TaskProcessing provider.""" |
| 101 | + require_capabilities("app_api", await self._session.capabilities) |
| 102 | + try: |
| 103 | + await self._session.ocs("DELETE", f"{self._session.ae_url}/{_EP_SUFFIX}", params={"name": name}) |
| 104 | + except NextcloudExceptionNotFound as e: |
| 105 | + if not not_fail: |
| 106 | + raise e from None |
| 107 | + |
| 108 | + async def next_task(self, provider_ids: [str], task_types: [str]): |
| 109 | + """Get the next task processing task from Nextcloud""" |
| 110 | + with contextlib.suppress(NextcloudException): |
| 111 | + return await self._session.ocs( |
| 112 | + "GET", |
| 113 | + "/ocs/v2.php/taskprocessing/tasks_provider/next", |
| 114 | + json={"providerIds": provider_ids, "taskTypeIds": task_types}, |
| 115 | + ) |
| 116 | + |
| 117 | + async def report_result(self, task_id: int, output: [str, str] = None, error_message: str = None) -> None: |
| 118 | + """Report results of the task processing to Nextcloud.""" |
| 119 | + require_capabilities("app_api", await self._session.capabilities) |
| 120 | + with contextlib.suppress(NextcloudException): |
| 121 | + await self._session.ocs( |
| 122 | + "POST", |
| 123 | + f"/ocs/v2.php/taskprocessing/tasks_provider/{task_id}/result", |
| 124 | + json={"taskId": task_id, "output": output, "errorMessage": error_message}, |
| 125 | + ) |
0 commit comments