Skip to content

Commit 6ae1648

Browse files
Merge pull request #158 from nextcloud/async
Async + doc
2 parents a21bf9d + 8bee098 commit 6ae1648

22 files changed

Lines changed: 4251 additions & 302 deletions

Sources/NextcloudKit/NextcloudKit+API.swift

Lines changed: 752 additions & 37 deletions
Large diffs are not rendered by default.

Sources/NextcloudKit/NextcloudKit+Assistant.swift

Lines changed: 213 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ import Alamofire
77
import SwiftyJSON
88

99
public extension NextcloudKit {
10+
/// Retrieves the list of supported text processing task types from the server.
11+
/// These types define the kinds of operations (e.g., summarization, translation) supported by the assistant API.
12+
///
13+
/// Parameters:
14+
/// - account: The Nextcloud account initiating the request.
15+
/// - options: Optional configuration for the HTTP request.
16+
/// - taskHandler: Optional closure to access the underlying URLSessionTask.
17+
/// - completion: Completion handler providing the account, available task types, response, and NKError.
1018
func textProcessingGetTypes(account: String,
1119
options: NKRequestOptions = NKRequestOptions(),
1220
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },
@@ -40,6 +48,47 @@ public extension NextcloudKit {
4048
}
4149
}
4250

51+
/// Asynchronously retrieves available text processing task types from the server.
52+
/// - Parameters:
53+
/// - account: The Nextcloud account initiating the request.
54+
/// - options: Optional request options.
55+
/// - taskHandler: Closure to access the session task.
56+
/// - Returns: A tuple with named values for account, list of task types, response, and error.
57+
func textProcessingGetTypesAsync(account: String,
58+
options: NKRequestOptions = NKRequestOptions(),
59+
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }
60+
) async -> (
61+
account: String,
62+
types: [NKTextProcessingTaskType]?,
63+
responseData: AFDataResponse<Data>?,
64+
error: NKError
65+
) {
66+
await withCheckedContinuation { continuation in
67+
textProcessingGetTypes(account: account,
68+
options: options,
69+
taskHandler: taskHandler) { account, types, responseData, error in
70+
continuation.resume(returning: (
71+
account: account,
72+
types: types,
73+
responseData: responseData,
74+
error: error
75+
))
76+
}
77+
}
78+
}
79+
80+
/// Schedules a new text processing task on the server (e.g., translation, summary, etc.).
81+
/// The request includes the input text, the type of task to execute, and a unique identifier.
82+
///
83+
/// Parameters:
84+
/// - input: The raw input string to be processed.
85+
/// - typeId: The identifier of the task type (e.g., "summarize", "translate").
86+
/// - appId: The application identifier (default is "assistant").
87+
/// - identifier: A client-side unique string to track this task.
88+
/// - account: The Nextcloud account executing the request.
89+
/// - options: Optional request configuration (headers, queue, etc.).
90+
/// - taskHandler: Optional closure to access the URLSessionTask.
91+
/// - completion: Completion handler returning the account, resulting task object, response, and any NKError.
4392
func textProcessingSchedule(input: String,
4493
typeId: String,
4594
appId: String = "assistant",
@@ -78,6 +127,56 @@ public extension NextcloudKit {
78127
}
79128
}
80129

130+
/// Asynchronously schedules a text processing task on the server.
131+
/// - Parameters:
132+
/// - input: Input string to process.
133+
/// - typeId: Task type identifier.
134+
/// - appId: Optional app ID, defaults to "assistant".
135+
/// - identifier: Unique task identifier.
136+
/// - account: Nextcloud account.
137+
/// - options: Request configuration.
138+
/// - taskHandler: Optional access to the URLSessionTask.
139+
/// - Returns: A tuple with named values for account, task object, raw response, and error.
140+
func textProcessingScheduleAsync(input: String,
141+
typeId: String,
142+
appId: String = "assistant",
143+
identifier: String,
144+
account: String,
145+
options: NKRequestOptions = NKRequestOptions(),
146+
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }
147+
) async -> (
148+
account: String,
149+
task: NKTextProcessingTask?,
150+
responseData: AFDataResponse<Data>?,
151+
error: NKError
152+
) {
153+
await withCheckedContinuation { continuation in
154+
textProcessingSchedule(input: input,
155+
typeId: typeId,
156+
appId: appId,
157+
identifier: identifier,
158+
account: account,
159+
options: options,
160+
taskHandler: taskHandler) { account, task, responseData, error in
161+
continuation.resume(returning: (
162+
account: account,
163+
task: task,
164+
responseData: responseData,
165+
error: error
166+
))
167+
}
168+
}
169+
}
170+
171+
/// Retrieves the current status and data of a previously scheduled text processing task.
172+
/// Useful for polling or checking the result of a long-running task by its unique ID.
173+
///
174+
/// Parameters:
175+
/// - taskId: The server-side ID of the text processing task to retrieve.
176+
/// - account: The Nextcloud account making the request.
177+
/// - options: Optional request configuration.
178+
/// - taskHandler: Optional closure to access the URLSessionTask.
179+
/// - completion: Completion handler returning the account, task object, raw response, and NKError.
81180
func textProcessingGetTask(taskId: Int,
82181
account: String,
83182
options: NKRequestOptions = NKRequestOptions(),
@@ -112,6 +211,47 @@ public extension NextcloudKit {
112211
}
113212
}
114213

214+
/// Asynchronously retrieves the details of a specific text processing task.
215+
/// - Parameters:
216+
/// - taskId: The ID of the task to fetch.
217+
/// - account: The account used for the request.
218+
/// - options: Optional configuration.
219+
/// - taskHandler: Closure to access the session task.
220+
/// - Returns: A tuple with named values for account, task object, response, and error.
221+
func textProcessingGetTaskAsync(taskId: Int,
222+
account: String,
223+
options: NKRequestOptions = NKRequestOptions(),
224+
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }
225+
) async -> (
226+
account: String,
227+
task: NKTextProcessingTask?,
228+
responseData: AFDataResponse<Data>?,
229+
error: NKError
230+
) {
231+
await withCheckedContinuation { continuation in
232+
textProcessingGetTask(taskId: taskId,
233+
account: account,
234+
options: options,
235+
taskHandler: taskHandler) { account, task, responseData, error in
236+
continuation.resume(returning: (
237+
account: account,
238+
task: task,
239+
responseData: responseData,
240+
error: error
241+
))
242+
}
243+
}
244+
}
245+
246+
/// Deletes a specific text processing task on the server.
247+
/// This is used to cancel or clean up tasks that are no longer needed.
248+
///
249+
/// Parameters:
250+
/// - taskId: The ID of the task to be deleted.
251+
/// - account: The Nextcloud account making the request.
252+
/// - options: Optional request configuration.
253+
/// - taskHandler: Optional closure to access the URLSessionTask.
254+
/// - completion: Completion handler returning the account, deleted task object, raw response, and NKError.
115255
func textProcessingDeleteTask(taskId: Int,
116256
account: String,
117257
options: NKRequestOptions = NKRequestOptions(),
@@ -146,6 +286,47 @@ public extension NextcloudKit {
146286
}
147287
}
148288

289+
/// Asynchronously deletes a scheduled text processing task from the server.
290+
/// - Parameters:
291+
/// - taskId: ID of the task to delete.
292+
/// - account: Account executing the deletion.
293+
/// - options: Request options.
294+
/// - taskHandler: Callback for the underlying URLSessionTask.
295+
/// - Returns: A tuple with named values for account, deleted task object, response, and error.
296+
func textProcessingDeleteTaskAsync(taskId: Int,
297+
account: String,
298+
options: NKRequestOptions = NKRequestOptions(),
299+
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }
300+
) async -> (
301+
account: String,
302+
task: NKTextProcessingTask?,
303+
responseData: AFDataResponse<Data>?,
304+
error: NKError
305+
) {
306+
await withCheckedContinuation { continuation in
307+
textProcessingDeleteTask(taskId: taskId,
308+
account: account,
309+
options: options,
310+
taskHandler: taskHandler) { account, task, responseData, error in
311+
continuation.resume(returning: (
312+
account: account,
313+
task: task,
314+
responseData: responseData,
315+
error: error
316+
))
317+
}
318+
}
319+
}
320+
321+
/// Retrieves a list of all text processing tasks associated with a specific app ID.
322+
/// This includes both pending and completed tasks, useful for tracking the assistant's activity.
323+
///
324+
/// Parameters:
325+
/// - appId: Identifier of the application requesting the tasks (e.g., "assistant").
326+
/// - account: The Nextcloud account making the request.
327+
/// - options: Optional HTTP request configuration.
328+
/// - taskHandler: Optional closure to access the URLSessionTask.
329+
/// - completion: Completion handler returning the account, task list, raw response, and NKError.
149330
func textProcessingTaskList(appId: String,
150331
account: String,
151332
options: NKRequestOptions = NKRequestOptions(),
@@ -179,6 +360,36 @@ public extension NextcloudKit {
179360
}
180361
}
181362
}
182-
}
183-
184363

364+
/// Asynchronously retrieves all text processing tasks associated with a specific app ID.
365+
/// - Parameters:
366+
/// - appId: The application identifier to filter the task list.
367+
/// - account: The account performing the request.
368+
/// - options: Optional request configuration.
369+
/// - taskHandler: Callback for the URLSessionTask.
370+
/// - Returns: A tuple with named values for account, task list, response, and error.
371+
func textProcessingTaskListAsync(appId: String,
372+
account: String,
373+
options: NKRequestOptions = NKRequestOptions(),
374+
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }
375+
) async -> (
376+
account: String,
377+
task: [NKTextProcessingTask]?,
378+
responseData: AFDataResponse<Data>?,
379+
error: NKError
380+
) {
381+
await withCheckedContinuation { continuation in
382+
textProcessingTaskList(appId: appId,
383+
account: account,
384+
options: options,
385+
taskHandler: taskHandler) { account, task, responseData, error in
386+
continuation.resume(returning: (
387+
account: account,
388+
task: task,
389+
responseData: responseData,
390+
error: error
391+
))
392+
}
393+
}
394+
}
395+
}

0 commit comments

Comments
 (0)