✨ Add global configuration, refacto & updated testing
This commit is contained in:
@@ -66,74 +66,83 @@ public struct Resource<T: Codable> {
|
||||
}
|
||||
}
|
||||
|
||||
public struct Hermes {
|
||||
public final class Hermes {
|
||||
public static let shared = Hermes()
|
||||
private var config: HermesConfiguration
|
||||
|
||||
public init() { }
|
||||
private init(config: HermesConfiguration = HermesConfiguration()) {
|
||||
self.config = config
|
||||
}
|
||||
|
||||
public func configure(_ config: HermesConfiguration) {
|
||||
self.config = config
|
||||
}
|
||||
|
||||
private var defaultHeaders: [String: String] {
|
||||
var headers = ["Content-Type": "application/json"]
|
||||
let defaults = UserDefaults.standard
|
||||
guard let token = defaults.string(forKey: "authToken") else {
|
||||
return headers
|
||||
if let token = UserDefaults.standard.string(forKey: "authToken") {
|
||||
headers["Authorization"] = "Bearer \(token)"
|
||||
}
|
||||
|
||||
headers["Authorization"] = "Bearer \(token)"
|
||||
|
||||
headers.merge(config.defaultHeaders) { (_, new) in new }
|
||||
return headers
|
||||
}
|
||||
|
||||
public func load<T: Codable>(_ resource: Resource<T>) async throws -> T {
|
||||
|
||||
var request = URLRequest(url: resource.url)
|
||||
request.httpMethod = resource.method.name
|
||||
|
||||
switch resource.method {
|
||||
case .get(let queryItems):
|
||||
case .get(let queryItems):
|
||||
if let queryItems = queryItems {
|
||||
var components = URLComponents(url: resource.url, resolvingAgainstBaseURL: false)
|
||||
components?.queryItems = queryItems
|
||||
guard let url = components?.url else {
|
||||
throw NetworkError.badRequest
|
||||
request.url = components?.url
|
||||
}
|
||||
|
||||
request = URLRequest(url: url)
|
||||
|
||||
case .post(let data),
|
||||
.put(let data),
|
||||
.patch(let data):
|
||||
request.httpMethod = resource.method.name
|
||||
request.httpBody = data
|
||||
|
||||
case .delete:
|
||||
request.httpMethod = resource.method.name
|
||||
break
|
||||
}
|
||||
|
||||
request.allHTTPHeaderFields = defaultHeaders
|
||||
|
||||
let configuration = URLSessionConfiguration.default
|
||||
configuration.httpAdditionalHeaders = defaultHeaders
|
||||
let session = URLSession(configuration: configuration)
|
||||
|
||||
let (data, response) = try await session.data(for: request)
|
||||
|
||||
guard let response = response as? HTTPURLResponse else {
|
||||
guard let httpResponse = response as? HTTPURLResponse else {
|
||||
throw NetworkError.invalidResponse
|
||||
}
|
||||
|
||||
if response.statusCode != 200 {
|
||||
switch response.statusCode {
|
||||
case 404:
|
||||
throw NetworkError.notFound
|
||||
case 500:
|
||||
throw NetworkError.serverError(response.description)
|
||||
default:
|
||||
throw NetworkError.invalidResponse
|
||||
}
|
||||
}
|
||||
try validate(response: httpResponse)
|
||||
|
||||
let decoder = JSONDecoder()
|
||||
decoder.keyDecodingStrategy = .convertFromSnakeCase
|
||||
decoder.keyDecodingStrategy = config.decodingStrategy
|
||||
|
||||
guard let result = try? decoder.decode(resource.modelType, from: data) else {
|
||||
do {
|
||||
return try decoder.decode(resource.modelType, from: data)
|
||||
} catch {
|
||||
throw NetworkError.decodingError
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private func validate(response: HTTPURLResponse) throws {
|
||||
switch response.statusCode {
|
||||
case 200...299:
|
||||
return
|
||||
case 400:
|
||||
throw NetworkError.badRequest
|
||||
case 404:
|
||||
throw NetworkError.notFound
|
||||
case 500:
|
||||
throw NetworkError.serverError("Internal Server Error")
|
||||
default:
|
||||
throw NetworkError.invalidResponse
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
18
Sources/Hermes/HermesConfiguration.swift
Normal file
18
Sources/Hermes/HermesConfiguration.swift
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// HermesConfiguration.swift
|
||||
//
|
||||
//
|
||||
// Created by Victor on 28/05/2024.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public struct HermesConfiguration {
|
||||
var defaultHeaders: [String: String] = [:]
|
||||
var decodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys
|
||||
|
||||
public init(defaultHeaders: [String: String] = [:], decodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) {
|
||||
self.defaultHeaders = defaultHeaders
|
||||
self.decodingStrategy = decodingStrategy
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user