Compare commits
1 Commits
aaf8706300
...
92707810ec
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
92707810ec |
@@ -14,11 +14,6 @@ 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 {
|
||||
@@ -76,8 +71,7 @@ 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
|
||||
}
|
||||
@@ -85,11 +79,7 @@ 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] {
|
||||
var headers = [
|
||||
"Content-Type": "application/json",
|
||||
@@ -104,11 +94,7 @@ public final class Hermes {
|
||||
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 {
|
||||
@@ -134,32 +120,21 @@ 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 {
|
||||
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
|
||||
}
|
||||
return try decoder.decode(resource.modelType, from: data)
|
||||
} catch {
|
||||
for middleware in middlewares {
|
||||
middleware.processResponse(nil, nil, error: error)
|
||||
}
|
||||
throw error
|
||||
throw NetworkError.decodingError
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user