Skip to content

Commit

Permalink
new getUserProfile()
Browse files Browse the repository at this point in the history
Signed-off-by: Marino Faggiana <[email protected]>
  • Loading branch information
marinofaggiana committed Jul 20, 2024
1 parent 4cf36e5 commit 499fa9b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
8 changes: 4 additions & 4 deletions Sources/NextcloudKit/NKCommon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -497,15 +497,15 @@ public class NKCommon: NSObject {
public func getStandardHeaders(user: String?, password: String?, appendHeaders: [String: String]?, customUserAgent: String?, contentType: String? = nil) -> HTTPHeaders {
var headers: HTTPHeaders = []

if let username = user, let password = password {
headers.update(.authorization(username: username, password: password))
if let user, let password {
headers.update(.authorization(username: user, password: password))
}
if let customUserAgent = customUserAgent {
if let customUserAgent {
headers.update(.userAgent(customUserAgent))
} else if let userAgent = userAgent {
headers.update(.userAgent(userAgent))
}
if let contentType = contentType {
if let contentType {
headers.update(.contentType(contentType))
} else {
headers.update(.contentType("application/x-www-form-urlencoded"))
Expand Down
37 changes: 34 additions & 3 deletions Sources/NextcloudKit/NextcloudKit+API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,25 @@ public extension NextcloudKit {

// MARK: -

func getUserProfile(url: String,
user: String,
password: String,
userAgent: String,
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },
completion: @escaping (_ userProfile: NKUserProfile?, _ data: Data?, _ error: NKError) -> Void) {
let endpoint = "ocs/v2.php/cloud/user"
guard let url = self.nkCommonInstance.createStandardUrl(serverUrl: url, endpoint: endpoint) else {
return completion(nil, nil, .urlError)
}
let headers = self.nkCommonInstance.getStandardHeaders(user: user, password: password, appendHeaders: nil, customUserAgent: userAgent)

getUserProfile(url: url, headers: headers, options: NKRequestOptions()) { task in
taskHandler(task)
} completion: { userProfile, data, error in
completion(userProfile, data, error)
}
}

func getUserProfile(options: NKRequestOptions = NKRequestOptions(),
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },
completion: @escaping (_ account: String, _ userProfile: NKUserProfile?, _ data: Data?, _ error: NKError) -> Void) {
Expand All @@ -547,6 +566,18 @@ public extension NextcloudKit {
}
let headers = self.nkCommonInstance.getStandardHeaders(options: options)

getUserProfile(url: url, headers: headers, options: options) { task in
taskHandler(task)
} completion: { userProfile, data, error in
completion(account, userProfile, data, error)
}
}

private func getUserProfile(url: URLConvertible,
headers: HTTPHeaders,
options: NKRequestOptions = NKRequestOptions(),
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },
completion: @escaping (_ userProfile: NKUserProfile?, _ data: Data?, _ error: NKError) -> Void) {
sessionManager.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in
task.taskDescription = options.taskDescription
taskHandler(task)
Expand All @@ -557,7 +588,7 @@ public extension NextcloudKit {
switch response.result {
case .failure(let error):
let error = NKError(error: error, afResponse: response, responseData: response.data)
options.queue.async { completion(account, nil, nil, error) }
options.queue.async { completion(nil, nil, error) }
case .success(let jsonData):
let json = JSON(jsonData)
let ocs = json["ocs"]
Expand Down Expand Up @@ -597,9 +628,9 @@ public extension NextcloudKit {
userProfile.twitter = data["twitter"].stringValue
userProfile.website = data["website"].stringValue

options.queue.async { completion(account, userProfile, jsonData, .success) }
options.queue.async { completion(userProfile, jsonData, .success) }
} else {
options.queue.async { completion(account, nil, jsonData, NKError(rootJson: json, fallbackStatusCode: response.response?.statusCode)) }
options.queue.async { completion(nil, jsonData, NKError(rootJson: json, fallbackStatusCode: response.response?.statusCode)) }
}
}
}
Expand Down

0 comments on commit 499fa9b

Please sign in to comment.