From 384dea132592e9a197c34784bbfb06792bd7309a Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Mon, 5 May 2025 09:13:47 +0200 Subject: [PATCH 1/3] cod Signed-off-by: Marino Faggiana --- Sources/NextcloudKit/NextcloudKit+API.swift | 70 +++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/Sources/NextcloudKit/NextcloudKit+API.swift b/Sources/NextcloudKit/NextcloudKit+API.swift index 3c25c0d6..d40a7293 100644 --- a/Sources/NextcloudKit/NextcloudKit+API.swift +++ b/Sources/NextcloudKit/NextcloudKit+API.swift @@ -452,6 +452,76 @@ public extension NextcloudKit { // MARK: - + func getUserMetadata(account: String, + userId: String, + options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, + completion: @escaping (_ account: String, _ userProfile: NKUserProfile?, _ responseData: AFDataResponse?, _ error: NKError) -> Void) { + let endpoint = "ocs/v2.php/cloud/user/\(userId)" + guard let nkSession = nkCommonInstance.getSession(account: account), + let url = nkCommonInstance.createStandardUrl(serverUrl: nkSession.urlBase, endpoint: endpoint, options: options), + let headers = nkCommonInstance.getStandardHeaders(account: account, options: options) else { + return options.queue.async { completion(account, nil, nil, .urlError) } + } + + nkSession.sessionDataNoCache.request(url, method: .get, encoding: URLEncoding.default, headers: headers, interceptor: NKInterceptor(nkCommonInstance: nkCommonInstance)).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + task.taskDescription = options.taskDescription + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + if self.nkCommonInstance.levelLog > 0 { + debugPrint(response) + } + switch response.result { + case .failure(let error): + let error = NKError(error: error, afResponse: response, responseData: response.data) + options.queue.async { completion(account, nil, response, error) } + case .success(let jsonData): + let json = JSON(jsonData) + let ocs = json["ocs"] + let data = ocs["data"] + + if json["ocs"]["meta"]["statuscode"].int == 200 { + let userProfile = NKUserProfile() + userProfile.address = data["address"].stringValue + userProfile.backend = data["backend"].stringValue + userProfile.backendCapabilitiesSetDisplayName = data["backendCapabilities"]["setDisplayName"].boolValue + userProfile.backendCapabilitiesSetPassword = data["backendCapabilities"]["setPassword"].boolValue + userProfile.displayName = data["display-name"].stringValue + userProfile.email = data["email"].stringValue + userProfile.enabled = data["enabled"].boolValue + if let groups = data["groups"].array { + for group in groups { + userProfile.groups.append(group.stringValue) + } + } + userProfile.userId = data["id"].stringValue + userProfile.language = data["language"].stringValue + userProfile.lastLogin = data["lastLogin"].int64Value + userProfile.locale = data["locale"].stringValue + userProfile.organisation = data["organisation"].stringValue + userProfile.phone = data["phone"].stringValue + userProfile.quotaFree = data["quota"]["free"].int64Value + userProfile.quota = data["quota"]["quota"].int64Value + userProfile.quotaRelative = data["quota"]["relative"].doubleValue + userProfile.quotaTotal = data["quota"]["total"].int64Value + userProfile.quotaUsed = data["quota"]["used"].int64Value + userProfile.storageLocation = data["storageLocation"].stringValue + if let subadmins = data["subadmin"].array { + for subadmin in subadmins { + userProfile.subadmin.append(subadmin.stringValue) + } + } + userProfile.twitter = data["twitter"].stringValue + userProfile.website = data["website"].stringValue + + options.queue.async { completion(account, userProfile, response, .success) } + } else { + options.queue.async { completion(account, nil, response, NKError(rootJson: json, fallbackStatusCode: response.response?.statusCode)) } + } + } + } + } + func getUserProfile(account: String, options: NKRequestOptions = NKRequestOptions(), taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, From adb1107fd42b7f6a89f40a8d3dd5fa550c01ee28 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Mon, 5 May 2025 09:22:39 +0200 Subject: [PATCH 2/3] fix Signed-off-by: Marino Faggiana --- Sources/NextcloudKit/NextcloudKit+API.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sources/NextcloudKit/NextcloudKit+API.swift b/Sources/NextcloudKit/NextcloudKit+API.swift index d40a7293..175222cd 100644 --- a/Sources/NextcloudKit/NextcloudKit+API.swift +++ b/Sources/NextcloudKit/NextcloudKit+API.swift @@ -457,7 +457,10 @@ public extension NextcloudKit { options: NKRequestOptions = NKRequestOptions(), taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ userProfile: NKUserProfile?, _ responseData: AFDataResponse?, _ error: NKError) -> Void) { - let endpoint = "ocs/v2.php/cloud/user/\(userId)" + let endpoint = "ocs/v2.php/cloud/users/\(userId)" + /// + options.contentType = "application/json" + /// guard let nkSession = nkCommonInstance.getSession(account: account), let url = nkCommonInstance.createStandardUrl(serverUrl: nkSession.urlBase, endpoint: endpoint, options: options), let headers = nkCommonInstance.getStandardHeaders(account: account, options: options) else { From 0a1c2ddfb75378823e2cca1b93c73a5a01e5eb2f Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Mon, 5 May 2025 09:56:52 +0200 Subject: [PATCH 3/3] cleaning code Signed-off-by: Marino Faggiana --- Sources/NextcloudKit/NextcloudKit+API.swift | 124 ++++++++------------ 1 file changed, 52 insertions(+), 72 deletions(-) diff --git a/Sources/NextcloudKit/NextcloudKit+API.swift b/Sources/NextcloudKit/NextcloudKit+API.swift index 175222cd..7dbad3e1 100644 --- a/Sources/NextcloudKit/NextcloudKit+API.swift +++ b/Sources/NextcloudKit/NextcloudKit+API.swift @@ -480,43 +480,8 @@ public extension NextcloudKit { options.queue.async { completion(account, nil, response, error) } case .success(let jsonData): let json = JSON(jsonData) - let ocs = json["ocs"] - let data = ocs["data"] - - if json["ocs"]["meta"]["statuscode"].int == 200 { - let userProfile = NKUserProfile() - userProfile.address = data["address"].stringValue - userProfile.backend = data["backend"].stringValue - userProfile.backendCapabilitiesSetDisplayName = data["backendCapabilities"]["setDisplayName"].boolValue - userProfile.backendCapabilitiesSetPassword = data["backendCapabilities"]["setPassword"].boolValue - userProfile.displayName = data["display-name"].stringValue - userProfile.email = data["email"].stringValue - userProfile.enabled = data["enabled"].boolValue - if let groups = data["groups"].array { - for group in groups { - userProfile.groups.append(group.stringValue) - } - } - userProfile.userId = data["id"].stringValue - userProfile.language = data["language"].stringValue - userProfile.lastLogin = data["lastLogin"].int64Value - userProfile.locale = data["locale"].stringValue - userProfile.organisation = data["organisation"].stringValue - userProfile.phone = data["phone"].stringValue - userProfile.quotaFree = data["quota"]["free"].int64Value - userProfile.quota = data["quota"]["quota"].int64Value - userProfile.quotaRelative = data["quota"]["relative"].doubleValue - userProfile.quotaTotal = data["quota"]["total"].int64Value - userProfile.quotaUsed = data["quota"]["used"].int64Value - userProfile.storageLocation = data["storageLocation"].stringValue - if let subadmins = data["subadmin"].array { - for subadmin in subadmins { - userProfile.subadmin.append(subadmin.stringValue) - } - } - userProfile.twitter = data["twitter"].stringValue - userProfile.website = data["website"].stringValue + if let userProfile = self.getUserProfile(json: json) { options.queue.async { completion(account, userProfile, response, .success) } } else { options.queue.async { completion(account, nil, response, NKError(rootJson: json, fallbackStatusCode: response.response?.statusCode)) } @@ -530,6 +495,9 @@ public extension NextcloudKit { taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, completion: @escaping (_ account: String, _ userProfile: NKUserProfile?, _ responseData: AFDataResponse?, _ error: NKError) -> Void) { let endpoint = "ocs/v2.php/cloud/user" + /// + options.contentType = "application/json" + /// guard let nkSession = nkCommonInstance.getSession(account: account), let url = nkCommonInstance.createStandardUrl(serverUrl: nkSession.urlBase, endpoint: endpoint, options: options), let headers = nkCommonInstance.getStandardHeaders(account: account, options: options) else { @@ -549,43 +517,8 @@ public extension NextcloudKit { options.queue.async { completion(account, nil, response, error) } case .success(let jsonData): let json = JSON(jsonData) - let ocs = json["ocs"] - let data = ocs["data"] - - if json["ocs"]["meta"]["statuscode"].int == 200 { - let userProfile = NKUserProfile() - userProfile.address = data["address"].stringValue - userProfile.backend = data["backend"].stringValue - userProfile.backendCapabilitiesSetDisplayName = data["backendCapabilities"]["setDisplayName"].boolValue - userProfile.backendCapabilitiesSetPassword = data["backendCapabilities"]["setPassword"].boolValue - userProfile.displayName = data["display-name"].stringValue - userProfile.email = data["email"].stringValue - userProfile.enabled = data["enabled"].boolValue - if let groups = data["groups"].array { - for group in groups { - userProfile.groups.append(group.stringValue) - } - } - userProfile.userId = data["id"].stringValue - userProfile.language = data["language"].stringValue - userProfile.lastLogin = data["lastLogin"].int64Value - userProfile.locale = data["locale"].stringValue - userProfile.organisation = data["organisation"].stringValue - userProfile.phone = data["phone"].stringValue - userProfile.quotaFree = data["quota"]["free"].int64Value - userProfile.quota = data["quota"]["quota"].int64Value - userProfile.quotaRelative = data["quota"]["relative"].doubleValue - userProfile.quotaTotal = data["quota"]["total"].int64Value - userProfile.quotaUsed = data["quota"]["used"].int64Value - userProfile.storageLocation = data["storageLocation"].stringValue - if let subadmins = data["subadmin"].array { - for subadmin in subadmins { - userProfile.subadmin.append(subadmin.stringValue) - } - } - userProfile.twitter = data["twitter"].stringValue - userProfile.website = data["website"].stringValue + if let userProfile = self.getUserProfile(json: json) { options.queue.async { completion(account, userProfile, response, .success) } } else { options.queue.async { completion(account, nil, response, NKError(rootJson: json, fallbackStatusCode: response.response?.statusCode)) } @@ -594,6 +527,53 @@ public extension NextcloudKit { } } + private func getUserProfile(json: JSON) -> NKUserProfile? { + let ocs = json["ocs"] + let data = ocs["data"] + + if json["ocs"]["meta"]["statuscode"].int == 200 { + let userProfile = NKUserProfile() + + userProfile.address = data["address"].stringValue + userProfile.backend = data["backend"].stringValue + userProfile.backendCapabilitiesSetDisplayName = data["backendCapabilities"]["setDisplayName"].boolValue + userProfile.backendCapabilitiesSetPassword = data["backendCapabilities"]["setPassword"].boolValue + userProfile.displayName = data["display-name"].stringValue + userProfile.email = data["email"].stringValue + userProfile.enabled = data["enabled"].boolValue + if let groups = data["groups"].array { + for group in groups { + userProfile.groups.append(group.stringValue) + } + } + userProfile.userId = data["id"].stringValue + userProfile.language = data["language"].stringValue + userProfile.lastLogin = data["lastLogin"].int64Value + userProfile.locale = data["locale"].stringValue + userProfile.organisation = data["organisation"].stringValue + userProfile.phone = data["phone"].stringValue + userProfile.quotaFree = data["quota"]["free"].int64Value + userProfile.quota = data["quota"]["quota"].int64Value + userProfile.quotaRelative = data["quota"]["relative"].doubleValue + userProfile.quotaTotal = data["quota"]["total"].int64Value + userProfile.quotaUsed = data["quota"]["used"].int64Value + userProfile.storageLocation = data["storageLocation"].stringValue + if let subadmins = data["subadmin"].array { + for subadmin in subadmins { + userProfile.subadmin.append(subadmin.stringValue) + } + } + userProfile.twitter = data["twitter"].stringValue + userProfile.website = data["website"].stringValue + + return userProfile + } + + return nil + } + // MARK: - + + func getCapabilities(account: String, options: NKRequestOptions = NKRequestOptions(), taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },