Nextcloud login flow v2 support (not in working state yet)
This commit is contained in:
@@ -22,14 +22,14 @@ public enum NetworkError: String, Error {
|
||||
class NetworkController {
|
||||
var userSettings: UserSettings
|
||||
var authString: String
|
||||
var urlString: String
|
||||
var cookBookUrlString: String
|
||||
|
||||
let apiVersion = "1"
|
||||
|
||||
init() {
|
||||
print("Initializing NetworkController.")
|
||||
self.userSettings = UserSettings()
|
||||
self.urlString = "https://\(userSettings.serverAddress)/index.php/apps/cookbook/api/v\(apiVersion)"
|
||||
self.cookBookUrlString = "https://\(userSettings.serverAddress)/index.php/apps/cookbook/api/v\(apiVersion)/"
|
||||
|
||||
let loginString = "\(userSettings.username):\(userSettings.token)"
|
||||
let loginData = loginString.data(using: String.Encoding.utf8)!
|
||||
@@ -38,7 +38,7 @@ class NetworkController {
|
||||
|
||||
func fetchData(path: String) async throws -> Data? {
|
||||
|
||||
let url = URL(string: "\(urlString)/\(path)")!
|
||||
let url = URL(string: "\(cookBookUrlString)/\(path)")!
|
||||
|
||||
var request = URLRequest(url: url)
|
||||
|
||||
@@ -61,9 +61,9 @@ class NetworkController {
|
||||
}
|
||||
}
|
||||
|
||||
func sendHTTPRequest(path: String, _ requestWrapper: RequestWrapper) async throws -> (Data?, NetworkError?) {
|
||||
print("Sending \(requestWrapper.method.rawValue) request (path: \(path)) ...")
|
||||
let urlStringSanitized = "\(urlString)/\(path)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
|
||||
func sendHTTPRequest(_ requestWrapper: RequestWrapper) async throws -> (Data?, NetworkError?) {
|
||||
print("Sending \(requestWrapper.method.rawValue) request (path: \(requestWrapper.prepend(cookBookPath: cookBookUrlString))) ...")
|
||||
let urlStringSanitized = requestWrapper.prepend(cookBookPath: cookBookUrlString).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
|
||||
let url = URL(string: urlStringSanitized!)!
|
||||
var request = URLRequest(url: url)
|
||||
request.setValue(
|
||||
@@ -123,7 +123,7 @@ class NetworkController {
|
||||
|
||||
func sendDataRequest<D: Decodable>(_ request: RequestWrapper) async -> (D?, Error?) {
|
||||
do {
|
||||
let (data, error) = try await sendHTTPRequest(path: request.path, request)
|
||||
let (data, error) = try await sendHTTPRequest(request)
|
||||
if let data = data {
|
||||
return (decodeData(data), error)
|
||||
}
|
||||
@@ -136,7 +136,7 @@ class NetworkController {
|
||||
|
||||
func sendRequest(_ request: RequestWrapper) async -> Error? {
|
||||
do {
|
||||
return try await sendHTTPRequest(path: request.path, request).1
|
||||
return try await sendHTTPRequest(request).1
|
||||
} catch {
|
||||
print("An unknown network error occured.")
|
||||
}
|
||||
@@ -158,3 +158,91 @@ class NetworkController {
|
||||
|
||||
|
||||
|
||||
struct NetworkHandler {
|
||||
static func sendHTTPRequest(_ requestWrapper: RequestWrapper, authString: String? = nil) async throws -> (Data?, NetworkError?) {
|
||||
print("Sending \(requestWrapper.method.rawValue) request (path: \(requestWrapper.path)) ...")
|
||||
let urlStringSanitized = requestWrapper.path.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
|
||||
let url = URL(string: urlStringSanitized!)!
|
||||
var request = URLRequest(url: url)
|
||||
request.setValue(
|
||||
"true",
|
||||
forHTTPHeaderField: "OCS-APIRequest"
|
||||
)
|
||||
if let authString = authString {
|
||||
request.setValue(
|
||||
"Basic \(authString)",
|
||||
forHTTPHeaderField: "Authorization"
|
||||
)
|
||||
}
|
||||
request.setValue(
|
||||
requestWrapper.accept.rawValue,
|
||||
forHTTPHeaderField: "Accept"
|
||||
)
|
||||
|
||||
request.httpMethod = requestWrapper.method.rawValue
|
||||
|
||||
switch requestWrapper.method {
|
||||
case .GET: break
|
||||
case .POST, .PUT:
|
||||
guard let httpBody = requestWrapper.body else { break }
|
||||
do {
|
||||
print("Encoding request ...")
|
||||
request.httpBody = try JSONEncoder().encode(httpBody)
|
||||
print("Request body: \(String(data: request.httpBody ?? Data(), encoding: .utf8) ?? "nil")")
|
||||
} catch {
|
||||
throw error
|
||||
}
|
||||
case .DELETE: throw NotImplementedError.notImplemented
|
||||
}
|
||||
|
||||
var data: Data? = nil
|
||||
var response: URLResponse? = nil
|
||||
do {
|
||||
(data, response) = try await URLSession.shared.data(for: request)
|
||||
print("Response: ", response)
|
||||
return (data, nil)
|
||||
} catch {
|
||||
return (nil, decodeURLResponse(response: response as? HTTPURLResponse))
|
||||
}
|
||||
}
|
||||
|
||||
static func decodeURLResponse(response: HTTPURLResponse?) -> NetworkError? {
|
||||
guard let response = response else {
|
||||
return NetworkError.unknownError
|
||||
}
|
||||
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)
|
||||
default: return (NetworkError.unknownError)
|
||||
}
|
||||
}
|
||||
|
||||
static func sendDataRequest<D: Decodable>(_ request: RequestWrapper) async -> (D?, Error?) {
|
||||
do {
|
||||
let (data, error) = try await sendHTTPRequest(request)
|
||||
if let data = data {
|
||||
print(String(data: data, encoding: .utf8))
|
||||
return (decodeData(data), error)
|
||||
}
|
||||
return (nil, error)
|
||||
} catch {
|
||||
print("An unknown network error occured.")
|
||||
}
|
||||
return (nil, NetworkError.unknownError)
|
||||
}
|
||||
|
||||
private static func decodeData<D: Decodable>(_ data: Data) -> D? {
|
||||
let decoder = JSONDecoder()
|
||||
do {
|
||||
print("Decoding type ", D.self, " ...")
|
||||
return try decoder.decode(D.self, from: data)
|
||||
} catch (let error) {
|
||||
print("DataController - decodeData(): Failed to decode data.")
|
||||
print("Error: ", error)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ enum AcceptHeader: String {
|
||||
|
||||
struct RequestWrapper {
|
||||
let method: RequestMethod
|
||||
let path: String
|
||||
var path: String
|
||||
let accept: AcceptHeader
|
||||
let body: Codable?
|
||||
|
||||
@@ -31,6 +31,24 @@ struct RequestWrapper {
|
||||
self.body = body
|
||||
self.accept = accept
|
||||
}
|
||||
|
||||
func prepend(cookBookPath: String) -> String {
|
||||
return cookBookPath + self.path
|
||||
}
|
||||
}
|
||||
|
||||
struct LoginV2Request: Codable {
|
||||
let poll: LoginV2Poll
|
||||
let login: String
|
||||
}
|
||||
|
||||
struct LoginV2Poll: Codable {
|
||||
let token: String
|
||||
let endpoint: String
|
||||
}
|
||||
|
||||
struct LoginV2Response: Codable {
|
||||
let server: String
|
||||
let loginName: String
|
||||
let appPassword: String
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user