throw error when needing token and does not have one

multistore
Laurent 1 year ago
parent 91bbc3943c
commit 51c9169621
  1. 36
      LeStorage/Services.swift

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

Loading…
Cancel
Save