From 25e878827430ba7a5178e4a5e903985ef446088c Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Fri, 8 Mar 2024 11:06:45 +0100 Subject: [PATCH] normalized taskHandler taskHandler(task) }. Signed-off-by: Marino Faggiana --- .../NextcloudKit/API/NextcloudKit+API.swift | 85 +++++++++++++++---- .../NextcloudKit/E2EE/NextcloudKit+E2EE.swift | 55 +++++++++--- .../NextcloudKit/NextcloudKit+Comments.swift | 25 ++++-- .../NextcloudKit/NextcloudKit+Dashboard.swift | 10 ++- .../NextcloudKit/NextcloudKit+FilesLock.swift | 5 +- .../NextcloudKit+Groupfolders.swift | 5 +- .../NextcloudKit/NextcloudKit+Hovercard.swift | 5 +- .../NextcloudKit/NextcloudKit+Livephoto.swift | 5 +- Sources/NextcloudKit/NextcloudKit+Login.swift | 20 ++++- .../NextcloudKit/NextcloudKit+NCText.swift | 20 ++++- .../NextcloudKit+PushNotification.swift | 20 ++++- .../NextcloudKit+Richdocuments.swift | 20 ++++- .../NextcloudKit/NextcloudKit+Search.swift | 10 ++- Sources/NextcloudKit/NextcloudKit+Share.swift | 39 +++++++-- .../NextcloudKit+UserStatus.swift | 35 ++++++-- .../WebDAV/NextcloudKit+WebDAV.swift | 65 +++++++++++--- 16 files changed, 340 insertions(+), 84 deletions(-) diff --git a/Sources/NextcloudKit/API/NextcloudKit+API.swift b/Sources/NextcloudKit/API/NextcloudKit+API.swift index 78b151f1..9ef8b4b9 100644 --- a/Sources/NextcloudKit/API/NextcloudKit+API.swift +++ b/Sources/NextcloudKit/API/NextcloudKit+API.swift @@ -34,13 +34,16 @@ extension NextcloudKit { @objc public func checkServer(serverUrl: String, queue: DispatchQueue = .main, + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ error: NKError) -> Void) { guard let url = serverUrl.asUrl else { return queue.async { completion(.urlError) } } - sessionManager.request(url, method: .head, parameters: nil, encoding: URLEncoding.default, headers: nil, interceptor: nil).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .head, parameters: nil, encoding: URLEncoding.default, headers: nil, interceptor: nil).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -60,6 +63,7 @@ extension NextcloudKit { @objc public func generalWithEndpoint(_ endpoint: String, method: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -73,7 +77,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: method, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: method, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -91,6 +97,7 @@ extension NextcloudKit { // MARK: - @objc public func getExternalSite(options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ externalFiles: [NKExternalSite], _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -105,7 +112,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -156,6 +165,7 @@ extension NextcloudKit { public func getServerStatus(serverUrl: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (ServerInfoResult) -> Void) { let endpoint = "status.php" @@ -166,7 +176,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -210,13 +222,16 @@ extension NextcloudKit { @objc public func getPreview(url: URL, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -245,6 +260,7 @@ extension NextcloudKit { endpointTrashbin: Bool = false, useInternalEndpoint: Bool = true, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ imagePreview: UIImage?, _ imageIcon: UIImage?, _ imageOriginal: UIImage?, _ etag: String?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -280,7 +296,9 @@ extension NextcloudKit { headers.update(name: "If-None-Match", value: etag) } - sessionManager.request(urlRequest, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -321,6 +339,7 @@ extension NextcloudKit { avatarSizeRounded: Int = 0, etag: String?, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ imageAvatar: UIImage?, _ imageOriginal: UIImage?, _ etag: String?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -338,7 +357,9 @@ extension NextcloudKit { headers.update(name: "If-None-Match", value: etag) } - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -403,6 +424,7 @@ extension NextcloudKit { @objc public func downloadContent(serverUrl: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -413,7 +435,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -435,6 +459,7 @@ extension NextcloudKit { // MARK: - @objc public func getUserProfile(options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ userProfile: NKUserProfile?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -448,7 +473,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -508,6 +535,7 @@ extension NextcloudKit { } @objc public func getCapabilities(options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -521,7 +549,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -545,6 +575,7 @@ extension NextcloudKit { @objc public func getRemoteWipeStatus(serverUrl: String, token: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ wipe: Bool, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -559,7 +590,9 @@ extension NextcloudKit { return options.queue.async { completion(account, false, nil, .urlError) } } - sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -579,6 +612,7 @@ extension NextcloudKit { @objc public func setRemoteWipeCompletition(serverUrl: String, token: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -593,7 +627,9 @@ extension NextcloudKit { return options.queue.async { completion(account, .urlError) } } - sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -616,6 +652,7 @@ extension NextcloudKit { objectType: String?, previews: Bool, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ activities: [NKActivity], _ activityFirstKnown: Int, _ activityLastGiven: Int, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -650,7 +687,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -714,6 +753,7 @@ extension NextcloudKit { // MARK: - @objc public func getNotifications(options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ notifications: [NKNotifications]?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -729,7 +769,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -794,6 +836,7 @@ extension NextcloudKit { idNotification: Int, method: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -814,7 +857,9 @@ extension NextcloudKit { let method = HTTPMethod(rawValue: method) let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(urlRequest, method: method, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest, method: method, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -833,6 +878,7 @@ extension NextcloudKit { @objc public func getDirectDownload(fileId: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ url: String?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -851,7 +897,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -873,6 +921,7 @@ extension NextcloudKit { public func sendClientDiagnosticsRemoteOperation(data: Data, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -894,7 +943,9 @@ extension NextcloudKit { return options.queue.async { completion(account, NKError(error: error)) } } - sessionManager.request(urlRequest).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } diff --git a/Sources/NextcloudKit/E2EE/NextcloudKit+E2EE.swift b/Sources/NextcloudKit/E2EE/NextcloudKit+E2EE.swift index 7de101a4..75470434 100644 --- a/Sources/NextcloudKit/E2EE/NextcloudKit+E2EE.swift +++ b/Sources/NextcloudKit/E2EE/NextcloudKit+E2EE.swift @@ -30,6 +30,7 @@ extension NextcloudKit { @objc public func markE2EEFolder(fileId: String, delete: Bool, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -49,7 +50,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: method, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: method, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -75,6 +78,7 @@ extension NextcloudKit { e2eCounter: String?, method: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ e2eToken: String?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -103,7 +107,9 @@ extension NextcloudKit { headers.update(name: "X-NC-E2EE-COUNTER", value: e2eCounter) } - sessionManager.request(url, method: method, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: method, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -128,6 +134,7 @@ extension NextcloudKit { @objc public func getE2EEMetadata(fileId: String, e2eToken: String?, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ e2eMetadata: String?, _ signature: String?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -150,7 +157,9 @@ extension NextcloudKit { parameters["e2e-token"] = e2eToken } - sessionManager.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -179,6 +188,7 @@ extension NextcloudKit { signature: String?, method: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ metadata: String?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -209,7 +219,9 @@ extension NextcloudKit { headers.update(name: "X-NC-E2EE-SIGNATURE", value: signature) } - sessionManager.request(url, method: method, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: method, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -235,6 +247,7 @@ extension NextcloudKit { @objc public func getE2EECertificate(user: String? = nil, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ certificate: String?, _ certificateUser: String?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -262,7 +275,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -290,6 +305,7 @@ extension NextcloudKit { } @objc public func getE2EEPrivateKey(options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ privateKey: String?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -307,7 +323,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -330,6 +348,7 @@ extension NextcloudKit { } @objc public func getE2EEPublicKey(options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ publicKey: String?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -347,7 +366,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -371,6 +392,7 @@ extension NextcloudKit { @objc public func signE2EECertificate(certificate: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ certificate: String?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -390,7 +412,9 @@ extension NextcloudKit { let parameters = ["csr": certificate] - sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -415,6 +439,7 @@ extension NextcloudKit { @objc public func storeE2EEPrivateKey(privateKey: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ privateKey: String?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -434,7 +459,9 @@ extension NextcloudKit { let parameters = ["privateKey": privateKey] - sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -457,6 +484,7 @@ extension NextcloudKit { } @objc public func deleteE2EECertificate(options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -474,7 +502,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .delete, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .delete, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -490,6 +520,7 @@ extension NextcloudKit { } @objc public func deleteE2EEPrivateKey(options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -507,7 +538,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .delete, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .delete, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } diff --git a/Sources/NextcloudKit/NextcloudKit+Comments.swift b/Sources/NextcloudKit/NextcloudKit+Comments.swift index e8831367..fedd6639 100644 --- a/Sources/NextcloudKit/NextcloudKit+Comments.swift +++ b/Sources/NextcloudKit/NextcloudKit+Comments.swift @@ -28,6 +28,7 @@ extension NextcloudKit { @objc public func getComments(fileId: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ items: [NKComments]?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -50,7 +51,9 @@ extension NextcloudKit { return options.queue.async { completion(account, nil, nil, NKError(error: error)) } } - sessionManager.request(urlRequest).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -73,6 +76,7 @@ extension NextcloudKit { @objc public func putComments(fileId: String, message: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -95,7 +99,9 @@ extension NextcloudKit { return options.queue.async { completion(account, NKError(error: error)) } } - sessionManager.request(urlRequest).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -114,6 +120,7 @@ extension NextcloudKit { messageId: String, message: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -137,7 +144,9 @@ extension NextcloudKit { return options.queue.async { completion(account, NKError(error: error)) } } - sessionManager.request(urlRequest).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -155,6 +164,7 @@ extension NextcloudKit { @objc public func deleteComments(fileId: String, messageId: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -168,7 +178,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .delete, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .delete, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -185,6 +197,7 @@ extension NextcloudKit { @objc public func markAsReadComments(fileId: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -208,7 +221,9 @@ extension NextcloudKit { return options.queue.async { completion(account, NKError(error: error)) } } - sessionManager.request(urlRequest).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } diff --git a/Sources/NextcloudKit/NextcloudKit+Dashboard.swift b/Sources/NextcloudKit/NextcloudKit+Dashboard.swift index ba401808..4b1ed83c 100644 --- a/Sources/NextcloudKit/NextcloudKit+Dashboard.swift +++ b/Sources/NextcloudKit/NextcloudKit+Dashboard.swift @@ -29,6 +29,7 @@ extension NextcloudKit { public func getDashboardWidget(options: NKRequestOptions = NKRequestOptions(), request: @escaping (DataRequest?) -> Void = { _ in }, + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ dashboardWidgets: [NCCDashboardWidget]?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -48,7 +49,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - let dashboardRequest = sessionManager.request(url, method: .get, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + let dashboardRequest = sessionManager.request(url, method: .get, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -75,6 +78,7 @@ extension NextcloudKit { public func getDashboardWidgetsApplication(_ items: String, options: NKRequestOptions = NKRequestOptions(), request: @escaping (DataRequest?) -> Void = { _ in }, + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ dashboardApplications: [NCCDashboardApplication]?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -94,7 +98,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - let dashboardRequest = sessionManager.request(url, method: .get, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + let dashboardRequest = sessionManager.request(url, method: .get, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } diff --git a/Sources/NextcloudKit/NextcloudKit+FilesLock.swift b/Sources/NextcloudKit/NextcloudKit+FilesLock.swift index a4e02244..e91b5583 100644 --- a/Sources/NextcloudKit/NextcloudKit+FilesLock.swift +++ b/Sources/NextcloudKit/NextcloudKit+FilesLock.swift @@ -32,6 +32,7 @@ extension NextcloudKit { @objc public func lockUnlockFile(serverUrlFileName: String, shouldLock: Bool, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -46,7 +47,9 @@ extension NextcloudKit { var headers = self.nkCommonInstance.getStandardHeaders(options: options) headers.update(name: "X-User-Lock", value: "1") - sessionManager.request(url, method: method, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: method, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } diff --git a/Sources/NextcloudKit/NextcloudKit+Groupfolders.swift b/Sources/NextcloudKit/NextcloudKit+Groupfolders.swift index 15438f70..6ebe9ca8 100644 --- a/Sources/NextcloudKit/NextcloudKit+Groupfolders.swift +++ b/Sources/NextcloudKit/NextcloudKit+Groupfolders.swift @@ -28,6 +28,7 @@ import SwiftyJSON extension NextcloudKit { @objc public func getGroupfolders(options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ results: [NKGroupfolders]?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -42,7 +43,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } diff --git a/Sources/NextcloudKit/NextcloudKit+Hovercard.swift b/Sources/NextcloudKit/NextcloudKit+Hovercard.swift index 352885b6..27b4600d 100644 --- a/Sources/NextcloudKit/NextcloudKit+Hovercard.swift +++ b/Sources/NextcloudKit/NextcloudKit+Hovercard.swift @@ -30,6 +30,7 @@ extension NextcloudKit { @objc public func getHovercard(for userId: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ result: NKHovercard?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -44,7 +45,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } diff --git a/Sources/NextcloudKit/NextcloudKit+Livephoto.swift b/Sources/NextcloudKit/NextcloudKit+Livephoto.swift index b5ca7ca9..4f862051 100644 --- a/Sources/NextcloudKit/NextcloudKit+Livephoto.swift +++ b/Sources/NextcloudKit/NextcloudKit+Livephoto.swift @@ -29,6 +29,7 @@ extension NextcloudKit { public func setLivephoto(serverUrlfileNamePath: String, livePhotoFile: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -49,7 +50,9 @@ extension NextcloudKit { return options.queue.async { completion(account, NKError(error: error)) } } - sessionManager.request(urlRequest).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } diff --git a/Sources/NextcloudKit/NextcloudKit+Login.swift b/Sources/NextcloudKit/NextcloudKit+Login.swift index 65750d3d..f590db9e 100644 --- a/Sources/NextcloudKit/NextcloudKit+Login.swift +++ b/Sources/NextcloudKit/NextcloudKit+Login.swift @@ -34,6 +34,7 @@ extension NextcloudKit { password: String, userAgent: String? = nil, queue: DispatchQueue = .main, + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ token: String?, _ data: Data?, _ error: NKError) -> Void) { let endpoint = "ocs/v2.php/core/getapppassword" @@ -55,7 +56,9 @@ extension NextcloudKit { return queue.async { completion(nil, nil, NKError(error: error)) } } - sessionManager.request(urlRequest).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -80,6 +83,7 @@ extension NextcloudKit { password: String, userAgent: String? = nil, queue: DispatchQueue = .main, + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ data: Data?, _ error: NKError) -> Void) { let endpoint = "ocs/v2.php/core/apppassword" @@ -101,7 +105,9 @@ extension NextcloudKit { return queue.async { completion(nil, NKError(error: error)) } } - sessionManager.request(urlRequest).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -121,6 +127,7 @@ extension NextcloudKit { @objc public func getLoginFlowV2(serverUrl: String, userAgent: String? = nil, queue: DispatchQueue = .main, + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ token: String?, _ endpoint: String?, _ login: String?, _ data: Data?, _ error: NKError) -> Void) { let endpoint = "index.php/login/v2" @@ -134,7 +141,9 @@ extension NextcloudKit { headers = [HTTPHeader.userAgent(userAgent)] } - sessionManager.request(url, method: .post, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .post, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -159,6 +168,7 @@ extension NextcloudKit { endpoint: String, userAgent: String? = nil, queue: DispatchQueue = .main, + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ server: String?, _ loginName: String?, _ appPassword: String?, _ data: Data?, _ error: NKError) -> Void) { let serverUrl = endpoint + "?token=" + token @@ -172,7 +182,9 @@ extension NextcloudKit { headers = [HTTPHeader.userAgent(userAgent)] } - sessionManager.request(url, method: .post, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .post, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } diff --git a/Sources/NextcloudKit/NextcloudKit+NCText.swift b/Sources/NextcloudKit/NextcloudKit+NCText.swift index cb77d7af..d6684371 100644 --- a/Sources/NextcloudKit/NextcloudKit+NCText.swift +++ b/Sources/NextcloudKit/NextcloudKit+NCText.swift @@ -28,6 +28,7 @@ import SwiftyJSON extension NextcloudKit { @objc public func NCTextObtainEditorDetails(options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ editors: [NKEditorDetailsEditors], _ creators: [NKEditorDetailsCreators], _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -44,7 +45,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -97,6 +100,7 @@ extension NextcloudKit { fileId: String? = nil, editor: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ url: String?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -117,7 +121,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .post, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .post, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -135,6 +141,7 @@ extension NextcloudKit { } @objc public func NCTextGetListOfTemplates(options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ templates: [NKEditorTemplates], _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -150,7 +157,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -184,6 +193,7 @@ extension NextcloudKit { creatorId: String, templateId: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ url: String?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -207,7 +217,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .post, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .post, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } diff --git a/Sources/NextcloudKit/NextcloudKit+PushNotification.swift b/Sources/NextcloudKit/NextcloudKit+PushNotification.swift index 4e51169c..5307bf00 100644 --- a/Sources/NextcloudKit/NextcloudKit+PushNotification.swift +++ b/Sources/NextcloudKit/NextcloudKit+PushNotification.swift @@ -35,6 +35,7 @@ extension NextcloudKit { devicePublicKey: String, proxyServerUrl: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ deviceIdentifier: String?, _ signature: String?, _ publicKey: String?, _ data: Data?, _ error: NKError) -> Void) { let endpoint = "ocs/v2.php/apps/notifications/api/v2/push" @@ -51,7 +52,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(user: user, password: password, appendHeaders: options.customHeader, customUserAgent: options.customUserAgent) - sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -80,6 +83,7 @@ extension NextcloudKit { user: String, password: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let endpoint = "ocs/v2.php/apps/notifications/api/v2/push" @@ -90,7 +94,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(user: user, password: password, appendHeaders: options.customHeader, customUserAgent: options.customUserAgent) - sessionManager.request(url, method: .delete, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .delete, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -111,6 +117,7 @@ extension NextcloudKit { signature: String, publicKey: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ error: NKError) -> Void) { let endpoint = "devices?format=json" @@ -129,7 +136,9 @@ extension NextcloudKit { let headers = HTTPHeaders(arrayLiteral: .userAgent(userAgent)) - sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -149,6 +158,7 @@ extension NextcloudKit { signature: String, publicKey: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ error: NKError) -> Void) { let endpoint = "devices" @@ -166,7 +176,9 @@ extension NextcloudKit { let headers = HTTPHeaders(arrayLiteral: .userAgent(userAgent)) - sessionManager.request(url, method: .delete, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .delete, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } diff --git a/Sources/NextcloudKit/NextcloudKit+Richdocuments.swift b/Sources/NextcloudKit/NextcloudKit+Richdocuments.swift index 05b0e68d..a9119889 100644 --- a/Sources/NextcloudKit/NextcloudKit+Richdocuments.swift +++ b/Sources/NextcloudKit/NextcloudKit+Richdocuments.swift @@ -29,6 +29,7 @@ extension NextcloudKit { @objc public func createUrlRichdocuments(fileID: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ url: String?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -44,7 +45,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -67,6 +70,7 @@ extension NextcloudKit { @objc public func getTemplatesRichdocuments(typeTemplate: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ templates: [NKRichdocumentsTemplate]?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -80,7 +84,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -117,6 +123,7 @@ extension NextcloudKit { @objc public func createRichdocuments(path: String, templateId: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ url: String?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -132,7 +139,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -155,6 +164,7 @@ extension NextcloudKit { @objc public func createAssetRichdocuments(path: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ url: String?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -170,7 +180,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } diff --git a/Sources/NextcloudKit/NextcloudKit+Search.swift b/Sources/NextcloudKit/NextcloudKit+Search.swift index a4414f01..5fca06fe 100644 --- a/Sources/NextcloudKit/NextcloudKit+Search.swift +++ b/Sources/NextcloudKit/NextcloudKit+Search.swift @@ -48,6 +48,7 @@ extension NextcloudKit { timeoutProvider: TimeInterval = 60, filter: @escaping (NKSearchProvider) -> Bool = { _ in true }, request: @escaping (DataRequest?) -> Void, + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, providers: @escaping (_ account: String, _ searchProviders: [NKSearchProvider]?) -> Void, update: @escaping (_ account: String, _ searchResult: NKSearchResult?, _ provider: NKSearchProvider, _ error: NKError) -> Void, completion: @escaping (_ account: String, _ data: Data?, _ error: NKError) -> Void) { @@ -63,7 +64,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - let requestUnifiedSearch = sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + let requestUnifiedSearch = sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -124,6 +127,7 @@ extension NextcloudKit { cursor: Int? = nil, options: NKRequestOptions = NKRequestOptions(), timeout: TimeInterval = 60, + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ accoun: String, NKSearchResult?, _ data: Data?, _ error: NKError) -> Void) -> DataRequest? { let urlBase = self.nkCommonInstance.urlBase @@ -162,7 +166,9 @@ extension NextcloudKit { return nil } - let requestSearchProvider = sessionManager.request(urlRequest).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + let requestSearchProvider = sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } diff --git a/Sources/NextcloudKit/NextcloudKit+Share.swift b/Sources/NextcloudKit/NextcloudKit+Share.swift index 7d7ee36a..d792266d 100644 --- a/Sources/NextcloudKit/NextcloudKit+Share.swift +++ b/Sources/NextcloudKit/NextcloudKit+Share.swift @@ -81,6 +81,7 @@ extension NextcloudKit { @objc public func readShares(parameters: NKShareParameter, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ shares: [NKShare]?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -93,7 +94,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: parameters.queryParameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: parameters.queryParameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -134,6 +137,7 @@ extension NextcloudKit { itemType: String = "file", lookup: Bool = false, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ sharees: [NKSharee]?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -160,7 +164,9 @@ extension NextcloudKit { "lookup": lookupString ] - sessionManager.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -255,9 +261,14 @@ extension NextcloudKit { password: String? = nil, permissions: Int = 1, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ share: NKShare?, _ data: Data?, _ error: NKError) -> Void) { - createShare(path: path, shareType: 3, shareWith: nil, publicUpload: publicUpload, hideDownload: hideDownload, password: password, permissions: permissions, options: options, completion: completion) + createShare(path: path, shareType: 3, shareWith: nil, publicUpload: publicUpload, hideDownload: hideDownload, password: password, permissions: permissions, options: options) { task in + taskHandler(task) + } completion: { account, share, data, error in + completion(account, share, data, error) + } } @objc public func createShare(path: String, @@ -268,9 +279,14 @@ extension NextcloudKit { permissions: Int = 1, options: NKRequestOptions = NKRequestOptions(), attributes: String? = nil, + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ share: NKShare?, _ data: Data?, _ error: NKError) -> Void) { - createShare(path: path, shareType: shareType, shareWith: shareWith, publicUpload: false, note: note, hideDownload: false, password: password, permissions: permissions, attributes: attributes, options: options, completion: completion) + createShare(path: path, shareType: shareType, shareWith: shareWith, publicUpload: false, note: note, hideDownload: false, password: password, permissions: permissions, attributes: attributes, options: options) { task in + taskHandler(task) + } completion: { account, share, data, error in + completion(account, share, data, error) + } } private func createShare(path: String, @@ -283,6 +299,7 @@ extension NextcloudKit { permissions: Int = 1, attributes: String? = nil, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ share: NKShare?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -320,7 +337,9 @@ extension NextcloudKit { parameters["attributes"] = attributes } - sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -371,6 +390,7 @@ extension NextcloudKit { hideDownload: Bool, attributes: String? = nil, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ share: NKShare?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -407,7 +427,9 @@ extension NextcloudKit { parameters["attributes"] = "[]" } - sessionManager.request(url, method: .put, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .put, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -434,6 +456,7 @@ extension NextcloudKit { @objc public func deleteShare(idShare: Int, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -447,7 +470,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .delete, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .delete, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } diff --git a/Sources/NextcloudKit/NextcloudKit+UserStatus.swift b/Sources/NextcloudKit/NextcloudKit+UserStatus.swift index d4525b14..b4e3f14b 100644 --- a/Sources/NextcloudKit/NextcloudKit+UserStatus.swift +++ b/Sources/NextcloudKit/NextcloudKit+UserStatus.swift @@ -29,6 +29,7 @@ extension NextcloudKit { @objc public func getUserStatus(userId: String? = nil, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ clearAt: NSDate?, _ icon: String?, _ message: String?, _ messageId: String?, _ messageIsPredefined: Bool, _ status: String?, _ statusIsUserDefined: Bool, _ userId: String?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -45,7 +46,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -81,6 +84,7 @@ extension NextcloudKit { @objc public func setUserStatus(status: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -98,7 +102,9 @@ extension NextcloudKit { "statusType": String(status) ] - sessionManager.request(url, method: .put, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .put, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -122,6 +128,7 @@ extension NextcloudKit { @objc public func setCustomMessagePredefined(messageId: String, clearAt: Double, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -142,7 +149,9 @@ extension NextcloudKit { parameters["clearAt"] = String(clearAt) } - sessionManager.request(url, method: .put, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .put, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -168,6 +177,7 @@ extension NextcloudKit { message: String, clearAt: Double, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -191,7 +201,9 @@ extension NextcloudKit { parameters["clearAt"] = String(clearAt) } - sessionManager.request(url, method: .put, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .put, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -214,6 +226,7 @@ extension NextcloudKit { } @objc public func clearMessage(options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -227,7 +240,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .delete, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .delete, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -250,6 +265,7 @@ extension NextcloudKit { } @objc public func getUserStatusPredefinedStatuses(options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ userStatuses: [NKUserStatus]?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -264,7 +280,9 @@ extension NextcloudKit { let headers = self.nkCommonInstance.getStandardHeaders(options: options) - sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -307,6 +325,7 @@ extension NextcloudKit { @objc public func getUserStatusRetrieveStatuses(limit: Int, offset: Int, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ userStatuses: [NKUserStatus]?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -326,7 +345,9 @@ extension NextcloudKit { "offset": String(offset) ] - sessionManager.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } diff --git a/Sources/NextcloudKit/WebDAV/NextcloudKit+WebDAV.swift b/Sources/NextcloudKit/WebDAV/NextcloudKit+WebDAV.swift index 1c5a7886..0872a5fe 100644 --- a/Sources/NextcloudKit/WebDAV/NextcloudKit+WebDAV.swift +++ b/Sources/NextcloudKit/WebDAV/NextcloudKit+WebDAV.swift @@ -29,6 +29,7 @@ extension NextcloudKit { @objc public func createFolder(serverUrlFileName: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ ocId: String?, _ date: NSDate?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -49,7 +50,9 @@ extension NextcloudKit { return options.queue.async { completion(account, nil, nil, NKError(error: error)) } } - sessionManager.request(urlRequest).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -75,6 +78,7 @@ extension NextcloudKit { @objc public func deleteFileOrFolder(serverUrlFileName: String, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -93,7 +97,9 @@ extension NextcloudKit { return options.queue.async { completion(account, NKError(error: error)) } } - sessionManager.request(urlRequest).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -112,6 +118,7 @@ extension NextcloudKit { serverUrlFileNameDestination: String, overwrite: Bool, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -138,7 +145,9 @@ extension NextcloudKit { return options.queue.async { completion(account, NKError(error: error)) } } - sessionManager.request(urlRequest).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -157,6 +166,7 @@ extension NextcloudKit { serverUrlFileNameDestination: String, overwrite: Bool, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -183,7 +193,9 @@ extension NextcloudKit { return options.queue.async { completion(account, NKError(error: error)) } } - sessionManager.request(urlRequest).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -204,6 +216,7 @@ extension NextcloudKit { includeHiddenFiles: [String] = [], requestBody: Data? = nil, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ files: [NKFile], _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -242,7 +255,9 @@ extension NextcloudKit { return options.queue.async { completion(account, files, nil, NKError(error: error)) } } - sessionManager.request(urlRequest).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -265,6 +280,7 @@ extension NextcloudKit { @objc public func getFileFromFileId(fileId: String? = nil, link: String? = nil, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ file: NKFile?, _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -284,7 +300,9 @@ extension NextcloudKit { return options.queue.async { completion(account, nil, nil, .urlError) } } - search(serverUrl: urlBase, httpBody: httpBody, showHiddenFiles: true, includeHiddenFiles: [], options: options) { account, files, data, error in + search(serverUrl: urlBase, httpBody: httpBody, showHiddenFiles: true, includeHiddenFiles: [], options: options) { task in + taskHandler(task) + } completion: { account, files, data, error in options.queue.async { completion(account, files.first, data, error) } } } @@ -294,11 +312,14 @@ extension NextcloudKit { showHiddenFiles: Bool, includeHiddenFiles: [String] = [], options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ files: [NKFile], _ data: Data?, _ error: NKError) -> Void) { let httpBody = requestBody.data(using: .utf8)! - search(serverUrl: serverUrl, httpBody: httpBody, showHiddenFiles: showHiddenFiles, includeHiddenFiles: includeHiddenFiles, options: options) { account, files, data, error in + search(serverUrl: serverUrl, httpBody: httpBody, showHiddenFiles: showHiddenFiles, includeHiddenFiles: includeHiddenFiles, options: options) { task in + taskHandler(task) + } completion: { account, files, data, error in options.queue.async { completion(account, files, data, error) } } } @@ -309,6 +330,7 @@ extension NextcloudKit { showHiddenFiles: Bool, includeHiddenFiles: [String] = [], options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ files: [NKFile], _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -321,7 +343,9 @@ extension NextcloudKit { let requestBody = String(format: NKDataFileXML(nkCommonInstance: self.nkCommonInstance).requestBodySearchFileName, href, depth, "%" + literal + "%") let httpBody = requestBody.data(using: .utf8)! - search(serverUrl: serverUrl, httpBody: httpBody, showHiddenFiles: showHiddenFiles, includeHiddenFiles: includeHiddenFiles, options: options) { account, files, data, error in + search(serverUrl: serverUrl, httpBody: httpBody, showHiddenFiles: showHiddenFiles, includeHiddenFiles: includeHiddenFiles, options: options) { task in + taskHandler(task) + } completion: { account, files, data, error in options.queue.async { completion(account, files, data, error) } } } @@ -334,6 +358,7 @@ extension NextcloudKit { showHiddenFiles: Bool, includeHiddenFiles: [String] = [], options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ files: [NKFile], _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -368,7 +393,9 @@ extension NextcloudKit { let httpBody = requestBody.data(using: .utf8)! - search(serverUrl: urlBase, httpBody: httpBody, showHiddenFiles: showHiddenFiles, includeHiddenFiles: includeHiddenFiles, options: options) { account, files, data, error in + search(serverUrl: urlBase, httpBody: httpBody, showHiddenFiles: showHiddenFiles, includeHiddenFiles: includeHiddenFiles, options: options) { task in + taskHandler(task) + } completion: { account, files, data, error in options.queue.async { completion(account, files, data, error) } } } @@ -378,6 +405,7 @@ extension NextcloudKit { showHiddenFiles: Bool, includeHiddenFiles: [String], options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ files: [NKFile], _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -404,7 +432,9 @@ extension NextcloudKit { return options.queue.async { completion(account, files, nil, NKError(error: error)) } } - sessionManager.request(urlRequest).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -427,6 +457,7 @@ extension NextcloudKit { @objc public func setFavorite(fileName: String, favorite: Bool, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -453,7 +484,9 @@ extension NextcloudKit { return options.queue.async { completion(account, NKError(error: error)) } } - sessionManager.request(urlRequest).validate(statusCode: 200..<300).response(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.response(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -471,6 +504,7 @@ extension NextcloudKit { @objc public func listingFavorites(showHiddenFiles: Bool, includeHiddenFiles: [String] = [], options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ files: [NKFile], _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -498,7 +532,9 @@ extension NextcloudKit { return options.queue.async { completion(account, files, nil, NKError(error: error)) } } - sessionManager.request(urlRequest).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) } @@ -520,6 +556,7 @@ extension NextcloudKit { @objc public func listingTrash(showHiddenFiles: Bool, options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ items: [NKTrash], _ data: Data?, _ error: NKError) -> Void) { let account = self.nkCommonInstance.account @@ -547,7 +584,9 @@ extension NextcloudKit { return options.queue.async { completion(account, items, nil, NKError(error: error)) } } - sessionManager.request(urlRequest).validate(statusCode: 200..<300).responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + sessionManager.request(urlRequest).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in if self.nkCommonInstance.levelLog > 0 { debugPrint(response) }