Skip to content

Commit 3664d90

Browse files
committed
feat: Add task processing API
Signed-off-by: provokateurin <[email protected]>
1 parent 818dc2e commit 3664d90

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

nc_py_api/ex_app/providers/providers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from ..._session import AsyncNcSessionApp, NcSessionApp
44
from .speech_to_text import _AsyncSpeechToTextProviderAPI, _SpeechToTextProviderAPI
5+
from .task_processing import _AsyncTaskProcessingProviderAPI, _TaskProcessingProviderAPI
56
from .text_processing import _AsyncTextProcessingProviderAPI, _TextProcessingProviderAPI
67
from .translations import _AsyncTranslationsProviderAPI, _TranslationsProviderAPI
78

@@ -15,11 +16,14 @@ class ProvidersApi:
1516
"""TextProcessing Provider API."""
1617
translations: _TranslationsProviderAPI
1718
"""Translations Provider API."""
19+
task_processing: _TaskProcessingProviderAPI
20+
"""TaskProcessing Provider API."""
1821

1922
def __init__(self, session: NcSessionApp):
2023
self.speech_to_text = _SpeechToTextProviderAPI(session)
2124
self.text_processing = _TextProcessingProviderAPI(session)
2225
self.translations = _TranslationsProviderAPI(session)
26+
self.task_processing = _TaskProcessingProviderAPI(session)
2327

2428

2529
class AsyncProvidersApi:
@@ -31,8 +35,11 @@ class AsyncProvidersApi:
3135
"""TextProcessing Provider API."""
3236
translations: _AsyncTranslationsProviderAPI
3337
"""Translations Provider API."""
38+
task_processing: _AsyncTaskProcessingProviderAPI
39+
"""TaskProcessing Provider API."""
3440

3541
def __init__(self, session: AsyncNcSessionApp):
3642
self.speech_to_text = _AsyncSpeechToTextProviderAPI(session)
3743
self.text_processing = _AsyncTextProcessingProviderAPI(session)
3844
self.translations = _AsyncTranslationsProviderAPI(session)
45+
self.task_processing = _AsyncTaskProcessingProviderAPI(session)
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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

Comments
 (0)