@@ -7,21 +7,25 @@ import Alamofire
77import SwiftyJSON
88
99public extension NextcloudKit {
10+
11+
1012 /// Performs a unified search using multiple providers and returns results asynchronously.
1113 ///
1214 /// - Parameters:
1315 /// - timeout: The individual request timeout per provider.
1416 /// - account: The Nextcloud account performing the search.
1517 /// - options: Optional configuration for the request (headers, queue, etc.).
18+ /// - handle: Optional operation handle that receives the underlying DataRequest and URLSessionTask
19+ /// as soon as they are created. Use it to cancel the request while it’s in flight
20+ /// (via handle.cancel()) or to observe the task/request lifecycle.
1621 /// - filter: A closure to filter which `NKSearchProvider` are enabled.
17- /// - taskHandler: Callback triggered when a `URLSessionTask` is created.
1822 ///
1923 /// - Returns: NKSearchProvider, NKError
2024 func unifiedSearchProviders( timeout: TimeInterval = 30 ,
2125 account: String ,
2226 options: NKRequestOptions = NKRequestOptions ( ) ,
23- filter : @escaping ( NKSearchProvider ) -> Bool = { _ in true } ,
24- taskHandler : @escaping ( _ task : URLSessionTask ) -> Void = { _ in }
27+ handle : NKOperationHandle ? = nil ,
28+ filter : @escaping ( NKSearchProvider ) -> Bool = { _ in true }
2529 ) async -> ( providers: [ NKSearchProvider ] ? , error: NKError ) {
2630 let endpoint = " ocs/v2.php/search/providers "
2731 guard let nkSession = nkCommonInstance. nksessions. session ( forAccount: account) ,
@@ -32,11 +36,17 @@ public extension NextcloudKit {
3236
3337 let request = nkSession. sessionData
3438 . request ( url, headers: headers, interceptor: NKInterceptor ( nkCommonInstance: nkCommonInstance) )
35- . validate ( statusCode: 200 ..< 300 )
3639 . onURLSessionTaskCreation { task in
3740 task. taskDescription = options. taskDescription
38- taskHandler ( task)
41+ Task {
42+ if let handle {
43+ await handle. set ( task: task)
44+ }
45+ }
3946 }
47+ . validate ( statusCode: 200 ..< 300 )
48+
49+ await handle? . set ( request: request)
4050 let response = await request. serializingData ( ) . response
4151
4252 switch response. result {
@@ -63,7 +73,9 @@ public extension NextcloudKit {
6373 /// - timeout: The timeout interval for the search request.
6474 /// - account: The Nextcloud account performing the search.
6575 /// - options: Optional request configuration such as headers and queue.
66- /// - taskHandler: Callback to observe the underlying URLSessionTask.
76+ /// - handle: Optional operation handle that receives the underlying DataRequest and URLSessionTask
77+ /// as soon as they are created. Use it to cancel the request while it’s in flight
78+ /// (via handle.cancel()) or to observe the task/request lifecycle.
6779 ///
6880 /// - Returns: NKSearchResult, NKError
6981 func unifiedSearch( providerId: String ,
@@ -73,7 +85,7 @@ public extension NextcloudKit {
7385 timeout: TimeInterval = 60 ,
7486 account: String ,
7587 options: NKRequestOptions = NKRequestOptions ( ) ,
76- taskHandler : @escaping ( _ task : URLSessionTask ) -> Void = { _ in } )
88+ handle : NKOperationHandle ? = nil )
7789 async -> ( searchResult: NKSearchResult ? , error: NKError ) {
7890 guard let term = term. urlEncoded,
7991 let nkSession = nkCommonInstance. nksessions. session ( forAccount: account) ,
@@ -105,8 +117,14 @@ public extension NextcloudKit {
105117 . validate ( statusCode: 200 ..< 300 )
106118 . onURLSessionTaskCreation { task in
107119 task. taskDescription = options. taskDescription
108- taskHandler ( task)
120+ Task {
121+ if let handle {
122+ await handle. set ( task: task)
123+ }
124+ }
109125 }
126+
127+ await handle? . set ( request: request)
110128 let response = await request. serializingData ( ) . response
111129
112130 switch response. result {
@@ -211,3 +229,4 @@ public class NKSearchProvider: NSObject {
211229 return allProvider. compactMap ( NKSearchProvider . init)
212230 }
213231}
232+
0 commit comments