Compare commits
1 Commits
aaf8706300
...
92707810ec
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
92707810ec |
@@ -14,11 +14,6 @@ public enum NetworkError: Error {
|
|||||||
case notFound
|
case notFound
|
||||||
}
|
}
|
||||||
|
|
||||||
public protocol HermesMiddleware {
|
|
||||||
func processRequest(_ request: inout URLRequest)
|
|
||||||
func processResponse(_ data: Data?, _ response: URLResponse?, error: Error?)
|
|
||||||
}
|
|
||||||
|
|
||||||
extension NetworkError: LocalizedError {
|
extension NetworkError: LocalizedError {
|
||||||
public var errorDescription: String? {
|
public var errorDescription: String? {
|
||||||
switch self {
|
switch self {
|
||||||
@@ -76,8 +71,7 @@ public struct Resource<T: Codable> {
|
|||||||
public final class Hermes {
|
public final class Hermes {
|
||||||
public static let shared = Hermes()
|
public static let shared = Hermes()
|
||||||
private var config: HermesConfiguration
|
private var config: HermesConfiguration
|
||||||
private var middlewares: [HermesMiddleware] = []
|
|
||||||
|
|
||||||
private init(config: HermesConfiguration = HermesConfiguration()) {
|
private init(config: HermesConfiguration = HermesConfiguration()) {
|
||||||
self.config = config
|
self.config = config
|
||||||
}
|
}
|
||||||
@@ -85,11 +79,7 @@ public final class Hermes {
|
|||||||
public func configure(_ config: HermesConfiguration) {
|
public func configure(_ config: HermesConfiguration) {
|
||||||
self.config = config
|
self.config = config
|
||||||
}
|
}
|
||||||
|
|
||||||
public func addMiddleware(_ middleware: HermesMiddleware) {
|
|
||||||
middlewares.append(middleware)
|
|
||||||
}
|
|
||||||
|
|
||||||
private var defaultHeaders: [String: String] {
|
private var defaultHeaders: [String: String] {
|
||||||
var headers = [
|
var headers = [
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -104,11 +94,7 @@ public final class Hermes {
|
|||||||
public func load<T: Codable>(_ resource: Resource<T>) async throws -> T {
|
public func load<T: Codable>(_ resource: Resource<T>) async throws -> T {
|
||||||
var request = URLRequest(url: resource.url)
|
var request = URLRequest(url: resource.url)
|
||||||
request.httpMethod = resource.method.name
|
request.httpMethod = resource.method.name
|
||||||
|
|
||||||
for middleware in middlewares {
|
|
||||||
middleware.processRequest(&request)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch resource.method {
|
switch resource.method {
|
||||||
case .get(let queryItems):
|
case .get(let queryItems):
|
||||||
if let queryItems = queryItems {
|
if let queryItems = queryItems {
|
||||||
@@ -134,32 +120,21 @@ public final class Hermes {
|
|||||||
|
|
||||||
let session = URLSession(configuration: URLSessionConfiguration.default)
|
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 {
|
do {
|
||||||
let (data, response) = try await session.data(for: request)
|
return try decoder.decode(resource.modelType, from: data)
|
||||||
|
|
||||||
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 {
|
} catch {
|
||||||
for middleware in middlewares {
|
throw NetworkError.decodingError
|
||||||
middleware.processResponse(nil, nil, error: error)
|
|
||||||
}
|
|
||||||
throw error
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user