Nextcloud Login refactoring

This commit is contained in:
VincentMeilinger
2025-05-31 11:12:14 +02:00
parent 5acf3b9c4f
commit 48b31a7997
29 changed files with 1277 additions and 720 deletions

View File

@@ -69,7 +69,7 @@ struct ApiRequest {
var response: URLResponse? = nil
do {
(data, response) = try await URLSession.shared.data(for: request)
Logger.network.debug("\(method.rawValue) \(path) SUCCESS!")
Logger.network.debug("\(method.rawValue) \(path) received response ...")
if let error = decodeURLResponse(response: response as? HTTPURLResponse) {
print("\(method.rawValue) \(path) FAILURE: \(error.localizedDescription)")
return (nil, error)
@@ -94,9 +94,8 @@ struct ApiRequest {
switch response.statusCode {
case 200...299: return (nil)
case 300...399: return (NetworkError.redirectionError)
case 400...499: return (NetworkError.clientError)
case 500...599: return (NetworkError.serverError)
case 600: return (NetworkError.invalidRequest)
case 400...499: return (NetworkError.clientError(statusCode: response.statusCode))
case 500...599: return (NetworkError.serverError(statusCode: response.statusCode))
default: return (NetworkError.unknownError)
}
}

View File

@@ -99,7 +99,7 @@ protocol CookbookApi {
/// - Returns: A list of categories. A NetworkError if the request fails.
static func getCategories(
auth: String
) async -> ([Category]?, NetworkError?)
) async -> ([CookbookApiCategory]?, NetworkError?)
/// Get all recipes of a specified category.
/// - Parameters:

View File

@@ -77,7 +77,7 @@ class CookbookApiV1: CookbookApi {
if let id = json as? Int {
return nil
} else if let dict = json as? [String: Any] {
return .serverError
return .unknownError
}
} catch {
return .decodingFailed
@@ -103,7 +103,7 @@ class CookbookApiV1: CookbookApi {
static func updateRecipe(auth: String, recipe: Recipe) async -> (NetworkError?) {
let cookbookRecipe = CookbookApiRecipeDetailV1.fromRecipe(recipe)
guard let recipeData = JSONEncoder.safeEncode(recipe) else {
guard let recipeData = JSONEncoder.safeEncode(cookbookRecipe) else {
return .dataError
}
let request = ApiRequest(
@@ -121,7 +121,7 @@ class CookbookApiV1: CookbookApi {
if let id = json as? Int {
return nil
} else if let dict = json as? [String: Any] {
return .serverError
return .unknownError
}
} catch {
return .decodingFailed
@@ -143,7 +143,7 @@ class CookbookApiV1: CookbookApi {
return nil
}
static func getCategories(auth: String) async -> ([Category]?, NetworkError?) {
static func getCategories(auth: String) async -> ([CookbookApiCategory]?, NetworkError?) {
let request = ApiRequest(
path: basePath + "/categories",
method: .GET,

View File

@@ -8,10 +8,10 @@
import Foundation
import SwiftUI
struct Category: Codable, Identifiable, Hashable {
struct CookbookApiCategory: Codable, Identifiable, Hashable {
var id: String { name }
let name: String
let recipe_count: Int
var name: String
var recipe_count: Int
private enum CodingKeys: String, CodingKey {
case name, recipe_count

View File

@@ -15,8 +15,8 @@ public enum NetworkError: UserAlert {
case encodingFailed
case decodingFailed
case redirectionError
case clientError
case serverError
case clientError(statusCode: Int)
case serverError(statusCode: Int)
case invalidRequest
case unknownError
case dataError
@@ -33,10 +33,10 @@ public enum NetworkError: UserAlert {
"Data decoding failed."
case .redirectionError:
"Redirection error"
case .clientError:
"Client error"
case .serverError:
"Server error"
case .clientError(let code):
"Client error: \(code)"
case .serverError(let code):
"Server error: \(code)"
case .invalidRequest:
"Invalid request"
case .unknownError:
@@ -47,7 +47,7 @@ public enum NetworkError: UserAlert {
}
var localizedDescription: LocalizedStringKey {
return "" // TODO: Add description
return self.localizedTitle
}
var alertButtons: [AlertButton] {

View File

@@ -20,10 +20,9 @@ class NextcloudApi {
/// - `LoginV2Request?`: An object containing the necessary information for the second step of the login process, if successful.
/// - `NetworkError?`: An error encountered during the network request, if any.
static func loginV2Request() async -> (LoginV2Request?, NetworkError?) {
let path = UserSettings.shared.serverProtocol + UserSettings.shared.serverAddress
static func loginV2Request(_ baseAddress: String) async -> (LoginV2Request?, NetworkError?) {
let request = ApiRequest(
path: path + "/index.php/login/v2",
path: baseAddress + "/index.php/login/v2",
method: .POST
)
@@ -52,16 +51,16 @@ class NextcloudApi {
/// - `LoginV2Response?`: An object representing the response of the login process, if successful.
/// - `NetworkError?`: An error encountered during the network request, if any.
static func loginV2Response(req: LoginV2Request) async -> (LoginV2Response?, NetworkError?) {
static func loginV2Poll(pollURL: String, pollToken: String) async -> (LoginV2Response?, NetworkError?) {
let request = ApiRequest(
path: req.poll.endpoint,
path: pollURL,
method: .POST,
headerFields: [
HeaderField.ocsRequest(value: true),
HeaderField.accept(value: .JSON),
HeaderField.contentType(value: .FORM)
],
body: "token=\(req.poll.token)".data(using: .utf8)
body: "token=\(pollToken)".data(using: .utf8)
)
let (data, error) = await request.send(pathCompletion: false)