|
|
|
|
@ -19,6 +19,7 @@ public enum ServiceError: Error { |
|
|
|
|
case cantConvertToUUID(id: String) |
|
|
|
|
case missingUserName |
|
|
|
|
case responseError(response: String) |
|
|
|
|
case missingToken |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate enum ServiceConf: String { |
|
|
|
|
@ -86,11 +87,11 @@ public class Services { |
|
|
|
|
return try await _runRequest(request, apiCallId: apiCallId) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _runRequest<T: Encodable, U: Decodable>(servicePath: String, method: HTTPMethod, payload: T, apiCallId: String? = nil) async throws -> U { |
|
|
|
|
var request = try self._baseRequest(servicePath: servicePath, method: method) |
|
|
|
|
request.httpBody = try jsonEncoder.encode(payload) |
|
|
|
|
return try await _runRequest(request, apiCallId: apiCallId) |
|
|
|
|
} |
|
|
|
|
// fileprivate func _runRequest<T: Encodable, U: Decodable>(servicePath: String, method: HTTPMethod, payload: T, apiCallId: String? = nil) async throws -> U { |
|
|
|
|
// var request = try self._baseRequest(servicePath: servicePath, method: method) |
|
|
|
|
// request.httpBody = try jsonEncoder.encode(payload) |
|
|
|
|
// return try await _runRequest(request, apiCallId: apiCallId) |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
fileprivate func _runRequest<T: Decodable>(_ request: URLRequest, apiCallId: String? = nil) async throws -> T { |
|
|
|
|
Logger.log("Run \(request.httpMethod ?? "") \(request.url?.absoluteString ?? "")") |
|
|
|
|
@ -101,8 +102,8 @@ public class Services { |
|
|
|
|
let statusCode = response.statusCode |
|
|
|
|
Logger.log("request ended with status code = \(statusCode)") |
|
|
|
|
switch statusCode { |
|
|
|
|
case 200...300: |
|
|
|
|
if let apiCallId, |
|
|
|
|
case 200..<300: |
|
|
|
|
if let apiCallId, |
|
|
|
|
let collectionName = (T.self as? any Storable.Type)?.resourceName() { |
|
|
|
|
try await MainActor.run { |
|
|
|
|
try Store.main.deleteApiCallById(apiCallId, collectionName: collectionName) |
|
|
|
|
@ -166,9 +167,14 @@ public class Services { |
|
|
|
|
var request = URLRequest(url: url) |
|
|
|
|
request.httpMethod = method.rawValue |
|
|
|
|
request.setValue("application/json", forHTTPHeaderField: "Content-Type") |
|
|
|
|
if !(requiresToken == false), let token = try? self.keychainStore.getToken() { |
|
|
|
|
if !(requiresToken == false) { |
|
|
|
|
// Logger.log("current token = \(token)") |
|
|
|
|
request.addValue("Token \(token)", forHTTPHeaderField: "Authorization") |
|
|
|
|
do { |
|
|
|
|
let token = try self.keychainStore.getToken() |
|
|
|
|
request.addValue("Token \(token)", forHTTPHeaderField: "Authorization") |
|
|
|
|
} catch { |
|
|
|
|
throw ServiceError.missingToken |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return request |
|
|
|
|
@ -275,32 +281,16 @@ public class Services { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public func forgotPassword(email: String) async throws { |
|
|
|
|
|
|
|
|
|
var postRequest = try self._baseRequest(servicePath: "dj-rest-auth/password/reset/", method: .post) |
|
|
|
|
postRequest.httpBody = try jsonEncoder.encode(Email(email: email)) |
|
|
|
|
let response: Email = try await self._runRequest(postRequest) |
|
|
|
|
Logger.log("response = \(response)") |
|
|
|
|
// return response |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func disconnect() throws { |
|
|
|
|
try self.keychainStore.deleteToken() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// public func addPurchase(_ purchase: Purchase) async throws { |
|
|
|
|
// var postRequest = try self._postRequest(servicePath: Purchase.resourceName()) |
|
|
|
|
// postRequest.httpBody = try jsonEncoder.encode(purchase) |
|
|
|
|
// let purchase: Purchase = try await self._runRequest(postRequest) |
|
|
|
|
// if var purchases: [Purchase] = UserDefaults(suiteName: "le.storage")?.array(forKey: Purchase.resourceName()) as? [Purchase] { |
|
|
|
|
// purchases.append(purchase) |
|
|
|
|
// UserDefaults(suiteName: "le.storage")?.setValue(purchases, forKey: Purchase.resourceName()) |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// var purchases: [Purchase] { |
|
|
|
|
// return UserDefaults(suiteName: "le.storage")?.array(forKey: Purchase.resourceName()) as? [Purchase] ?? [] |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
struct AuthResponse: Codable { |
|
|
|
|
|