Skip to content

Commit 628f44b

Browse files
async
Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
1 parent 229337d commit 628f44b

3 files changed

Lines changed: 280 additions & 39 deletions

File tree

Sources/NextcloudKit/NextcloudKit+RecommendedFiles.swift

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ import Alamofire
77
import SwiftyJSON
88

99
public extension NextcloudKit {
10+
// Retrieves a list of recommended files from the server.
11+
//
12+
// Parameters:
13+
// - account: The Nextcloud account used to perform the request.
14+
// - options: Optional configuration for headers, queue, versioning, etc.
15+
// - request: Optional callback to observe or manipulate the underlying DataRequest.
16+
// - taskHandler: Callback triggered when the URLSessionTask is created.
17+
// - completion: Completion handler returning the account, the list of recommendations,
18+
// the raw response data, and an NKError result.
1019
func getRecommendedFiles(account: String,
1120
options: NKRequestOptions = NKRequestOptions(),
1221
request: @escaping (DataRequest?) -> Void = { _ in },
@@ -48,12 +57,36 @@ public extension NextcloudKit {
4857
options.queue.async { request(tosRequest) }
4958
}
5059

60+
/// Asynchronously fetches a list of recommended files for the given account.
61+
///
62+
/// - Parameters:
63+
/// - account: The Nextcloud account requesting the recommendations.
64+
/// - options: Optional configuration for queue, headers, etc.
65+
/// - request: Optional callback to capture the DataRequest object.
66+
/// - taskHandler: Optional handler for the URLSessionTask.
67+
/// - Returns: A tuple containing the account, list of recommended files, raw response data, and NKError result.
5168
func getRecommendedFilesAsync(account: String,
52-
options: NKRequestOptions = NKRequestOptions()) async -> (account: String, recommendations: [NKRecommendation]?, responseData: AFDataResponse<Data>?, error: NKError) {
53-
await withUnsafeContinuation({ continuation in
54-
NextcloudKit.shared.getRecommendedFiles(account: account, options: options) { account, recommendations, responseData, error in
55-
continuation.resume(returning: (account: account, recommendations: recommendations, responseData: responseData, error: error))
69+
options: NKRequestOptions = NKRequestOptions(),
70+
request: @escaping (DataRequest?) -> Void = { _ in },
71+
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }
72+
) async -> (
73+
account: String,
74+
recommendations: [NKRecommendation]?,
75+
responseData: AFDataResponse<Data>?,
76+
error: NKError
77+
) {
78+
await withCheckedContinuation { continuation in
79+
getRecommendedFiles(account: account,
80+
options: options,
81+
request: request,
82+
taskHandler: taskHandler) { account, recommendations, responseData, error in
83+
continuation.resume(returning: (
84+
account: account,
85+
recommendations: recommendations,
86+
responseData: responseData,
87+
error: error
88+
))
5689
}
57-
})
90+
}
5891
}
5992
}

Sources/NextcloudKit/NextcloudKit+Richdocuments.swift

Lines changed: 164 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ import Alamofire
77
import SwiftyJSON
88

99
public extension NextcloudKit {
10+
// Requests a URL for editing or viewing a file via the Richdocuments (Collabora/OnlyOffice) app.
11+
//
12+
// Parameters:
13+
// - fileID: The unique identifier of the file for which the document URL is requested.
14+
// - account: The Nextcloud account performing the request.
15+
// - options: Optional configuration such as custom headers, queue, or API version.
16+
// - taskHandler: Callback invoked when the underlying URLSessionTask is created.
17+
// - completion: Completion handler returning the account, document URL (if available),
18+
// the raw HTTP response, and an NKError object.
1019
func createUrlRichdocuments(fileID: String,
1120
account: String,
1221
options: NKRequestOptions = NKRequestOptions(),
@@ -40,6 +49,47 @@ public extension NextcloudKit {
4049
}
4150
}
4251

52+
/// Asynchronously retrieves the URL for opening a file in Richdocuments (e.g., Collabora or OnlyOffice).
53+
///
54+
/// - Parameters:
55+
/// - fileID: The identifier of the target file.
56+
/// - account: The Nextcloud account used for the operation.
57+
/// - options: Request configuration (headers, queue, version, etc.).
58+
/// - taskHandler: Optional handler to observe the URLSessionTask.
59+
/// - Returns: A tuple containing the account, the richdocument URL (if any), the raw response data, and any NKError.
60+
func createUrlRichdocumentsAsync(fileID: String,
61+
account: String,
62+
options: NKRequestOptions = NKRequestOptions(),
63+
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }
64+
) async -> (
65+
account: String,
66+
url: String?,
67+
responseData: AFDataResponse<Data>?,
68+
error: NKError
69+
) {
70+
await withCheckedContinuation { continuation in
71+
createUrlRichdocuments(fileID: fileID,
72+
account: account,
73+
options: options,
74+
taskHandler: taskHandler) { account, url, responseData, error in
75+
continuation.resume(returning: (
76+
account: account,
77+
url: url,
78+
responseData: responseData,
79+
error: error
80+
))
81+
}
82+
}
83+
}
84+
85+
// Retrieves the list of Richdocuments templates of a given type (e.g., "document", "spreadsheet").
86+
//
87+
// Parameters:
88+
// - typeTemplate: The type of template to retrieve (e.g., "document", "presentation").
89+
// - account: The Nextcloud account performing the request.
90+
// - options: Optional configuration (headers, queue, API version, etc.).
91+
// - taskHandler: Callback invoked when the underlying URLSessionTask is created.
92+
// - completion: Completion handler returning the account, array of templates, response data, and NKError.
4393
func getTemplatesRichdocuments(typeTemplate: String,
4494
account: String,
4595
options: NKRequestOptions = NKRequestOptions(),
@@ -84,16 +134,48 @@ public extension NextcloudKit {
84134
}
85135
}
86136

137+
/// Asynchronously fetches Richdocuments templates filtered by type.
138+
///
139+
/// - Parameters:
140+
/// - typeTemplate: The type of template to retrieve (e.g., "document").
141+
/// - account: The Nextcloud account for which templates are requested.
142+
/// - options: Optional request configuration.
143+
/// - taskHandler: Optional handler to observe the `URLSessionTask`.
144+
/// - Returns: A tuple containing the account, array of templates, raw response data, and any NKError.
87145
func getTemplatesRichdocumentsAsync(typeTemplate: String,
88146
account: String,
89-
options: NKRequestOptions = NKRequestOptions()) async -> (account: String, templates: [NKRichdocumentsTemplate]?, responseData: AFDataResponse<Data>?, error: NKError) {
90-
await withUnsafeContinuation({ continuation in
91-
NextcloudKit.shared.getTemplatesRichdocuments(typeTemplate: typeTemplate, account: account, options: options) { account, templates, responseData, error in
92-
continuation.resume(returning: (account: account, templates: templates, responseData: responseData, error: error))
147+
options: NKRequestOptions = NKRequestOptions(),
148+
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }
149+
) async -> (
150+
account: String,
151+
templates: [NKRichdocumentsTemplate]?,
152+
responseData: AFDataResponse<Data>?,
153+
error: NKError
154+
) {
155+
await withCheckedContinuation { continuation in
156+
getTemplatesRichdocuments(typeTemplate: typeTemplate,
157+
account: account,
158+
options: options,
159+
taskHandler: taskHandler) { account, templates, responseData, error in
160+
continuation.resume(returning: (
161+
account: account,
162+
templates: templates,
163+
responseData: responseData,
164+
error: error
165+
))
93166
}
94-
})
167+
}
95168
}
96169

170+
// Creates a new Richdocuments file using a specific template.
171+
//
172+
// Parameters:
173+
// - path: The target path where the new document should be created.
174+
// - templateId: The ID of the Richdocuments template to use.
175+
// - account: The Nextcloud account performing the request.
176+
// - options: Optional request configuration (headers, queue, API version, etc.).
177+
// - taskHandler: Callback invoked when the underlying URLSessionTask is created.
178+
// - completion: Completion handler returning the account, resulting file URL, raw response, and NKError.
97179
func createRichdocuments(path: String,
98180
templateId: String,
99181
account: String,
@@ -128,6 +210,50 @@ public extension NextcloudKit {
128210
}
129211
}
130212

213+
/// Asynchronously creates a new Richdocuments file from a given template.
214+
///
215+
/// - Parameters:
216+
/// - path: Destination path for the new document.
217+
/// - templateId: Template ID used to generate the new file.
218+
/// - account: The Nextcloud account performing the operation.
219+
/// - options: Optional request parameters.
220+
/// - taskHandler: Optional monitoring of the underlying task.
221+
/// - Returns: A tuple with account, resulting URL (if successful), raw response, and error result.
222+
func createRichdocumentsAsync(path: String,
223+
templateId: String,
224+
account: String,
225+
options: NKRequestOptions = NKRequestOptions(),
226+
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }
227+
) async -> (
228+
account: String,
229+
url: String?,
230+
responseData: AFDataResponse<Data>?,
231+
error: NKError
232+
) {
233+
await withCheckedContinuation { continuation in
234+
createRichdocuments(path: path,
235+
templateId: templateId,
236+
account: account,
237+
options: options,
238+
taskHandler: taskHandler) { account, url, responseData, error in
239+
continuation.resume(returning: (
240+
account: account,
241+
url: url,
242+
responseData: responseData,
243+
error: error
244+
))
245+
}
246+
}
247+
}
248+
249+
// Creates a new Richdocuments file based on a default asset (no template).
250+
//
251+
// Parameters:
252+
// - path: The destination path where the asset will be created.
253+
// - account: The Nextcloud account initiating the creation.
254+
// - options: Optional configuration for the request (e.g. headers, queue, API version).
255+
// - taskHandler: Callback invoked when the underlying URLSessionTask is created.
256+
// - completion: Completion handler returning account, resulting file URL, raw response data, and NKError.
131257
func createAssetRichdocuments(path: String,
132258
account: String,
133259
options: NKRequestOptions = NKRequestOptions(),
@@ -156,4 +282,37 @@ public extension NextcloudKit {
156282
}
157283
}
158284
}
285+
286+
/// Asynchronously creates a Richdocuments asset file at a specified path.
287+
///
288+
/// - Parameters:
289+
/// - path: Target path for the asset document.
290+
/// - account: The Nextcloud account performing the operation.
291+
/// - options: Optional request customization.
292+
/// - taskHandler: Optional monitoring of the underlying task.
293+
/// - Returns: A tuple with account, resulting URL, raw response, and error.
294+
func createAssetRichdocumentsAsync(path: String,
295+
account: String,
296+
options: NKRequestOptions = NKRequestOptions(),
297+
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }
298+
) async -> (
299+
account: String,
300+
url: String?,
301+
responseData: AFDataResponse<Data>?,
302+
error: NKError
303+
) {
304+
await withCheckedContinuation { continuation in
305+
createAssetRichdocuments(path: path,
306+
account: account,
307+
options: options,
308+
taskHandler: taskHandler) { account, url, responseData, error in
309+
continuation.resume(returning: (
310+
account: account,
311+
url: url,
312+
responseData: responseData,
313+
error: error
314+
))
315+
}
316+
}
317+
}
159318
}

Sources/NextcloudKit/NextcloudKit+Search.swift

Lines changed: 78 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@ import Alamofire
88
import SwiftyJSON
99

1010
public extension NextcloudKit {
11-
/// Available NC >= 20
12-
/// Search many different datasources in the cloud and combine them into one result.
13-
///
14-
/// - Warning: Providers are requested concurrently. Not filtering will result in a high network load.
15-
///
16-
/// - SeeAlso:
17-
/// [Nextcloud Search API](https://docs.nextcloud.com/server/latest/developer_manual/digging_deeper/search.html)
18-
///
19-
/// - Parameters:
20-
/// - term: The search term
21-
/// - options: Additional request options
22-
/// - filter: Filter search provider that should be searched. Default is all available provider..
23-
/// - update: Callback, notifying that a search provider return its result. Does not include previous results.
24-
/// - completion: Callback, notifying that all search providers have been searched. The search is done. Includes all search results.
11+
// Available NC >= 20
12+
// Performs a unified search using multiple providers and returns results asynchronously.
13+
//
14+
// Parameters:
15+
// - term: The search term to query.
16+
// - timeout: The individual request timeout per provider.
17+
// - timeoutProvider: The maximum time allowed for each provider before being cancelled.
18+
// - account: The Nextcloud account performing the search.
19+
// - options: Optional configuration for the request (headers, queue, etc.).
20+
// - filter: A closure to filter which `NKSearchProvider` are enabled.
21+
// - request: Callback to access and inspect the underlying `DataRequest?`.
22+
// - taskHandler: Callback triggered when a `URLSessionTask` is created.
23+
// - providers: Callback providing the list of providers that will be queried.
24+
// - update: Called for every result update from a provider.
25+
// - completion: Called when all providers are finished, returns the response and status.
2526
func unifiedSearch(term: String,
2627
timeout: TimeInterval = 30,
2728
timeoutProvider: TimeInterval = 60,
@@ -76,21 +77,69 @@ public extension NextcloudKit {
7677
request(requestUnifiedSearch)
7778
}
7879

79-
/// Available NC >= 20
80-
/// Search many different datasources in the cloud and combine them into one result.
81-
///
82-
/// - SeeAlso:
83-
/// [Nextcloud Search API](https://docs.nextcloud.com/server/latest/developer_manual/digging_deeper/search.html)
84-
///
85-
/// - Parameters:
86-
/// - id: provider id
87-
/// - term: The search term
88-
/// - limit: limit (pagination)
89-
/// - cursor: cursor (pagination)
90-
/// - options: Additional request options
91-
/// - timeout: Filter search provider that should be searched. Default is all available provider..
92-
/// - update: Callback, notifying that a search provider return its result. Does not include previous results.
93-
/// - completion: Callback, notifying that all search results.
80+
// Asynchronously performs a unified search and returns the final search response.
81+
//
82+
// - Parameters:
83+
// - term: The string to search for.
84+
// - timeout: Per-provider timeout in seconds.
85+
// - timeoutProvider: Overall timeout for a provider.
86+
// - account: The account used to authenticate the request.
87+
// - options: Optional parameters for the search.
88+
// - filter: Closure to filter the search providers.
89+
// - request: Callback with the underlying `DataRequest?`.
90+
// - taskHandler: Monitors the task creation.
91+
// - providers: Callback that reports which providers are used.
92+
// - update: Callback triggered as results come in from providers.
93+
// - Returns: Final completion with account, raw response data, and NKError.
94+
func unifiedSearchAsync(term: String,
95+
timeout: TimeInterval = 30,
96+
timeoutProvider: TimeInterval = 60,
97+
account: String,
98+
options: NKRequestOptions = NKRequestOptions(),
99+
filter: @escaping (NKSearchProvider) -> Bool = { _ in true },
100+
request: @escaping (DataRequest?) -> Void,
101+
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },
102+
providers: @escaping (_ account: String, _ searchProviders: [NKSearchProvider]?) -> Void,
103+
update: @escaping (_ account: String, _ searchResult: NKSearchResult?, _ provider: NKSearchProvider, _ error: NKError) -> Void
104+
) async -> (
105+
account: String,
106+
responseData: AFDataResponse<Data>?,
107+
error: NKError
108+
) {
109+
await withCheckedContinuation { continuation in
110+
unifiedSearch(term: term,
111+
timeout: timeout,
112+
timeoutProvider: timeoutProvider,
113+
account: account,
114+
options: options,
115+
filter: filter,
116+
request: request,
117+
taskHandler: taskHandler,
118+
providers: providers,
119+
update: update) { account, responseData, error in
120+
continuation.resume(returning: (
121+
account: account,
122+
responseData: responseData,
123+
error: error
124+
))
125+
}
126+
}
127+
}
128+
129+
// Available NC >= 20
130+
// Performs a search using a specified provider with pagination and timeout support.
131+
//
132+
// Parameters:
133+
// - id: The identifier of the search provider to use.
134+
// - term: The search term.
135+
// - limit: Optional maximum number of results to return.
136+
// - cursor: Optional pagination cursor for subsequent requests.
137+
// - timeout: The timeout interval for the search request.
138+
// - account: The Nextcloud account performing the search.
139+
// - options: Optional request configuration such as headers and queue.
140+
// - taskHandler: Callback to observe the underlying URLSessionTask.
141+
// - completion: Completion handler returning the account, search results, raw response, and NKError.
142+
// - Returns: The underlying DataRequest object if the request was started, otherwise nil.
94143
func searchProvider(_ id: String,
95144
term: String,
96145
limit: Int? = nil,

0 commit comments

Comments
 (0)