Skip to content

Commit d1b2adb

Browse files
committed
Asynchronous method wrappers.
Various method wrappers which enable calls in fashion of modern concurrency. Signed-off-by: Iva Horn <iva.horn@icloud.com>
1 parent 8aac3fd commit d1b2adb

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

Sources/NextcloudKit/NKError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extension OCSPath {
3232
static var ocsXMLMsg: Self { ["d:error", "s:message"] }
3333
}
3434

35-
public struct NKError: Sendable, Equatable {
35+
public struct NKError: Error, Equatable {
3636
static let internalError = -9999
3737
// Chunk error
3838
public static let chunkNoEnoughMemory = -9998

Sources/NextcloudKit/NextcloudKit+API.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ public extension NextcloudKit {
151151
case failure(NKError)
152152
}
153153

154+
///
155+
/// Asynchronous method wrapper for ``getServerStatus(serverUrl:options:taskHandler:completion:)``.
156+
///
157+
func getServerStatus(serverUrl: String, options: NKRequestOptions = NKRequestOptions(), taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }) async -> ServerInfoResult {
158+
await withCheckedContinuation { continuation in
159+
getServerStatus(serverUrl: serverUrl, options: options, taskHandler: taskHandler) { _, serverInfoResult in
160+
continuation.resume(returning: serverInfoResult)
161+
}
162+
}
163+
}
164+
154165
func getServerStatus(serverUrl: String,
155166
options: NKRequestOptions = NKRequestOptions(),
156167
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },

Sources/NextcloudKit/NextcloudKit+Login.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,39 @@ public extension NextcloudKit {
9999

100100
// MARK: - Login Flow V2
101101

102+
///
103+
/// Requests the initiation of a login process and retrieves required information.
104+
///
105+
/// - Returns: A tuple consisting of the `endpoint` to poll for the login status with the `token`. Additionally, the `login` to open for the user to log in.
106+
///
107+
func getLoginFlowV2(serverUrl: String, options: NKRequestOptions = NKRequestOptions(), taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }) async throws -> (endpoint: URL, login: URL, token: String) {
108+
try await withCheckedThrowingContinuation { continuation in
109+
getLoginFlowV2(serverUrl: serverUrl, options: options, taskHandler: taskHandler) { token, endpointString, loginString, _, error in
110+
if error != .success {
111+
continuation.resume(throwing: error)
112+
return
113+
}
114+
115+
guard let endpointString, let endpointURL = URL(string: endpointString) else {
116+
continuation.resume(throwing: NKError.urlError)
117+
return
118+
}
119+
120+
guard let loginString, let loginURL = URL(string: loginString) else {
121+
continuation.resume(throwing: NKError.urlError)
122+
return
123+
}
124+
125+
guard let token else {
126+
continuation.resume(throwing: NKError.invalidData)
127+
return
128+
}
129+
130+
continuation.resume(returning: (endpointURL, loginURL, token))
131+
}
132+
}
133+
}
134+
102135
func getLoginFlowV2(serverUrl: String,
103136
options: NKRequestOptions = NKRequestOptions(),
104137
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in },

0 commit comments

Comments
 (0)