Compare commits
1 Commits
98423afa91
...
feature/re
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aaf8706300 |
@@ -14,6 +14,11 @@ public enum NetworkError: Error {
|
||||
case notFound
|
||||
}
|
||||
|
||||
public protocol HermesMiddleware {
|
||||
func processRequest(_ request: inout URLRequest)
|
||||
func processResponse(_ data: Data?, _ response: URLResponse?, error: Error?)
|
||||
}
|
||||
|
||||
extension NetworkError: LocalizedError {
|
||||
public var errorDescription: String? {
|
||||
switch self {
|
||||
@@ -71,7 +76,8 @@ public struct Resource<T: Codable> {
|
||||
public final class Hermes {
|
||||
public static let shared = Hermes()
|
||||
private var config: HermesConfiguration
|
||||
|
||||
private var middlewares: [HermesMiddleware] = []
|
||||
|
||||
private init(config: HermesConfiguration = HermesConfiguration()) {
|
||||
self.config = config
|
||||
}
|
||||
@@ -79,15 +85,30 @@ public final class Hermes {
|
||||
public func configure(_ config: HermesConfiguration) {
|
||||
self.config = config
|
||||
}
|
||||
|
||||
|
||||
public func addMiddleware(_ middleware: HermesMiddleware) {
|
||||
middlewares.append(middleware)
|
||||
}
|
||||
|
||||
private var defaultHeaders: [String: String] {
|
||||
return config.defaultHeaders
|
||||
var headers = [
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json"
|
||||
]
|
||||
|
||||
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
|
||||
|
||||
|
||||
for middleware in middlewares {
|
||||
middleware.processRequest(&request)
|
||||
}
|
||||
|
||||
switch resource.method {
|
||||
case .get(let queryItems):
|
||||
if let queryItems = queryItems {
|
||||
@@ -113,21 +134,32 @@ public final class Hermes {
|
||||
|
||||
let session = URLSession(configuration: URLSessionConfiguration.default)
|
||||
|
||||
let (data, response) = try await session.data(for: request)
|
||||
|
||||
guard let httpResponse = response as? HTTPURLResponse else {
|
||||
throw NetworkError.invalidResponse
|
||||
}
|
||||
|
||||
try validate(response: httpResponse)
|
||||
|
||||
let decoder = JSONDecoder()
|
||||
decoder.keyDecodingStrategy = config.decodingStrategy
|
||||
|
||||
do {
|
||||
return try decoder.decode(resource.modelType, from: data)
|
||||
let (data, response) = try await session.data(for: request)
|
||||
|
||||
for middleware in middlewares {
|
||||
middleware.processResponse(data, response, error: nil)
|
||||
}
|
||||
|
||||
guard let httpResponse = response as? HTTPURLResponse else {
|
||||
throw NetworkError.invalidResponse
|
||||
}
|
||||
|
||||
try validate(response: httpResponse)
|
||||
|
||||
let decoder = JSONDecoder()
|
||||
decoder.keyDecodingStrategy = config.decodingStrategy
|
||||
|
||||
do {
|
||||
return try decoder.decode(resource.modelType, from: data)
|
||||
} catch {
|
||||
throw NetworkError.decodingError
|
||||
}
|
||||
} catch {
|
||||
throw NetworkError.decodingError
|
||||
for middleware in middlewares {
|
||||
middleware.processResponse(nil, nil, error: error)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ final class HermesTests: XCTestCase {
|
||||
}
|
||||
|
||||
private func configureHermes() {
|
||||
let defaultHeaders = ["Content-Type": "application/json"]
|
||||
let defaultHeaders = ["Custom-Header": "HeaderValue"]
|
||||
let decodingStrategy = JSONDecoder.KeyDecodingStrategy.convertFromSnakeCase
|
||||
let config = HermesConfiguration(defaultHeaders: defaultHeaders, decodingStrategy: decodingStrategy)
|
||||
Hermes.shared.configure(config)
|
||||
|
||||
Reference in New Issue
Block a user