Skip to content

Commit 229337d

Browse files
async
Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
1 parent a501927 commit 229337d

4 files changed

Lines changed: 514 additions & 104 deletions

File tree

Sources/NextcloudKit/NextcloudKit+Livephoto.swift

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import Foundation
66
import Alamofire
77

88
public extension NextcloudKit {
9+
// Associates a Live Photo video file with a photo on the server.
10+
//
11+
// Parameters:
12+
// - serverUrlfileNamePath: The full server path to the original photo.
13+
// - livePhotoFile: The local path to the Live Photo video file (.mov).
14+
// - account: The account performing the operation.
15+
// - options: Optional request configuration (e.g., headers, queue, version).
16+
// - taskHandler: Callback for tracking the underlying URLSessionTask.
17+
// - completion: Returns the account, raw response data, and NKError result.
918
func setLivephoto(serverUrlfileNamePath: String,
1019
livePhotoFile: String,
1120
account: String,
@@ -46,14 +55,37 @@ public extension NextcloudKit {
4655
}
4756
}
4857

58+
/// Asynchronously attaches a Live Photo video file to an existing image on the server.
59+
///
60+
/// - Parameters:
61+
/// - serverUrlfileNamePath: The full server-side path of the target image.
62+
/// - livePhotoFile: Local file path of the Live Photo (.mov).
63+
/// - account: The Nextcloud account to use for the request.
64+
/// - options: Optional request context and headers.
65+
/// - taskHandler: Optional callback to observe the URLSessionTask.
66+
/// - Returns: A tuple with the account, response data, and NKError result.
4967
func setLivephotoAsync(serverUrlfileNamePath: String,
5068
livePhotoFile: String,
5169
account: String,
52-
options: NKRequestOptions = NKRequestOptions()) async -> (account: String, responseData: AFDataResponse<Data>?, error: NKError) {
53-
await withUnsafeContinuation({ continuation in
54-
NextcloudKit.shared.setLivephoto(serverUrlfileNamePath: serverUrlfileNamePath, livePhotoFile: livePhotoFile, account: account, options: options) { account, responseData, error in
55-
continuation.resume(returning: (account: account, responseData: responseData, error: error))
70+
options: NKRequestOptions = NKRequestOptions(),
71+
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }
72+
) async -> (
73+
account: String,
74+
responseData: AFDataResponse<Data>?,
75+
error: NKError
76+
) {
77+
await withCheckedContinuation { continuation in
78+
setLivephoto(serverUrlfileNamePath: serverUrlfileNamePath,
79+
livePhotoFile: livePhotoFile,
80+
account: account,
81+
options: options,
82+
taskHandler: taskHandler) { account, responseData, error in
83+
continuation.resume(returning: (
84+
account: account,
85+
responseData: responseData,
86+
error: error
87+
))
5688
}
57-
})
89+
}
5890
}
5991
}

Sources/NextcloudKit/NextcloudKit+Login.swift

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ import SwiftyJSON
88

99
public extension NextcloudKit {
1010
// MARK: - App Password
11+
12+
// Retrieves an app password (token) for the given user credentials and server URL.
13+
//
14+
// Parameters:
15+
// - url: The base server URL (e.g., https://cloud.example.com).
16+
// - user: The username for authentication.
17+
// - password: The user's password.
18+
// - userAgent: Optional user-agent string to include in the request.
19+
// - options: Optional request configuration (headers, queue, etc.).
20+
// - taskHandler: Callback for observing the underlying URLSessionTask.
21+
// - completion: Returns the token string (if any), raw response data, and NKError result.
1122
func getAppPassword(url: String,
1223
user: String,
1324
password: String,
@@ -47,6 +58,54 @@ public extension NextcloudKit {
4758
}
4859
}
4960

61+
/// Asynchronously fetches an app password for the provided user credentials.
62+
///
63+
/// - Parameters:
64+
/// - url: The base URL of the Nextcloud server.
65+
/// - user: The user login name.
66+
/// - password: The user’s password.
67+
/// - userAgent: Optional custom user agent for the request.
68+
/// - options: Optional request configuration.
69+
/// - taskHandler: Callback to observe the task, if needed.
70+
/// - Returns: A tuple containing the token, response data, and error result.
71+
func getAppPasswordAsync(url: String,
72+
user: String,
73+
password: String,
74+
userAgent: String? = nil,
75+
options: NKRequestOptions = NKRequestOptions(),
76+
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }
77+
) async -> (
78+
token: String?,
79+
responseData: AFDataResponse<Data>?,
80+
error: NKError
81+
) {
82+
await withCheckedContinuation { continuation in
83+
getAppPassword(url: url,
84+
user: user,
85+
password: password,
86+
userAgent: userAgent,
87+
options: options,
88+
taskHandler: taskHandler) { token, responseData, error in
89+
continuation.resume(returning: (
90+
token: token,
91+
responseData: responseData,
92+
error: error
93+
))
94+
}
95+
}
96+
}
97+
98+
// Deletes the app password (token) for a specific account using basic authentication.
99+
//
100+
// Parameters:
101+
// - serverUrl: The full server URL (e.g., https://cloud.example.com).
102+
// - username: The username associated with the app password.
103+
// - password: The password or app password used for authentication.
104+
// - userAgent: Optional user-agent string for the request.
105+
// - account: The logical account identifier used in the app.
106+
// - options: Optional request configuration (headers, queues, etc.).
107+
// - taskHandler: Callback to observe the underlying URLSessionTask.
108+
// - completion: Returns the raw response and a possible NKError result.
50109
func deleteAppPassword(serverUrl: String,
51110
username: String,
52111
password: String,
@@ -87,6 +146,44 @@ public extension NextcloudKit {
87146
}
88147
}
89148

149+
/// Asynchronously deletes the current app password/token from the server.
150+
///
151+
/// - Parameters:
152+
/// - serverUrl: Full URL of the Nextcloud server.
153+
/// - username: The user identifier.
154+
/// - password: The password or token used for deletion authorization.
155+
/// - userAgent: Optional string to customize the User-Agent header.
156+
/// - account: Logical account identifier.
157+
/// - options: Configuration options for the request.
158+
/// - taskHandler: Optional callback for observing the URLSessionTask.
159+
/// - Returns: A tuple containing the response and a possible error.
160+
func deleteAppPasswordAsync(serverUrl: String,
161+
username: String,
162+
password: String,
163+
userAgent: String? = nil,
164+
account: String,
165+
options: NKRequestOptions = NKRequestOptions(),
166+
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }
167+
) async -> (
168+
responseData: AFDataResponse<Data>?,
169+
error: NKError
170+
) {
171+
await withCheckedContinuation { continuation in
172+
deleteAppPassword(serverUrl: serverUrl,
173+
username: username,
174+
password: password,
175+
userAgent: userAgent,
176+
account: account,
177+
options: options,
178+
taskHandler: taskHandler) { responseData, error in
179+
continuation.resume(returning: (
180+
responseData: responseData,
181+
error: error
182+
))
183+
}
184+
}
185+
}
186+
90187
// MARK: - Login Flow V2
91188

92189
///
@@ -155,6 +252,14 @@ public extension NextcloudKit {
155252
}
156253
}
157254

255+
// Polls the login flow V2 endpoint to retrieve login credentials (OAuth-style).
256+
//
257+
// Parameters:
258+
// - token: The login flow token to poll for.
259+
// - endpoint: The base URL endpoint (e.g., https://cloud.example.com).
260+
// - options: Optional request configuration (version, headers, queues, etc.).
261+
// - taskHandler: Callback to observe the underlying URLSessionTask.
262+
// - completion: Returns the discovered server URL, loginName, appPassword, the raw response data, and any NKError.
158263
func getLoginFlowV2Poll(token: String,
159264
endpoint: String,
160265
options: NKRequestOptions = NKRequestOptions(),
@@ -187,4 +292,39 @@ public extension NextcloudKit {
187292
}
188293
}
189294
}
295+
296+
/// Asynchronously polls the login flow V2 endpoint for login credentials.
297+
///
298+
/// - Parameters:
299+
/// - token: The token used in the login flow process.
300+
/// - endpoint: Full base endpoint URL to call the polling API.
301+
/// - options: Request configuration such as version, headers, queue.
302+
/// - taskHandler: Optional callback to observe the underlying URLSessionTask.
303+
/// - Returns: A tuple with server URL, login name, app password, raw response, and NKError.
304+
func getLoginFlowV2PollAsync(token: String,
305+
endpoint: String,
306+
options: NKRequestOptions = NKRequestOptions(),
307+
taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }
308+
) async -> (
309+
server: String?,
310+
loginName: String?,
311+
appPassword: String?,
312+
responseData: AFDataResponse<Data>?,
313+
error: NKError
314+
) {
315+
await withCheckedContinuation { continuation in
316+
getLoginFlowV2Poll(token: token,
317+
endpoint: endpoint,
318+
options: options,
319+
taskHandler: taskHandler) { server, loginName, appPassword, responseData, error in
320+
continuation.resume(returning: (
321+
server: server,
322+
loginName: loginName,
323+
appPassword: appPassword,
324+
responseData: responseData,
325+
error: error
326+
))
327+
}
328+
}
329+
}
190330
}

0 commit comments

Comments
 (0)