Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 89 additions & 36 deletions Sources/NextcloudKit/NextcloudKit+API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -452,11 +452,52 @@ 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<Data>?, _ error: NKError) -> Void) {
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 {
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)

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)) }
}
}
}
}

func getUserProfile(account: String,
options: NKRequestOptions = NKRequestOptions(),
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },
completion: @escaping (_ account: String, _ userProfile: NKUserProfile?, _ responseData: AFDataResponse<Data>?, _ 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 {
Expand All @@ -476,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)) }
Expand All @@ -521,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 },
Expand Down