Merge branch 'release/1.3.0'
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] {
|
private var defaultHeaders: [String: String] {
|
||||||
var headers = ["Content-Type": "application/json"]
|
var headers = ["Content-Type": "application/json"]
|
||||||
let defaults = UserDefaults.standard
|
if let token = UserDefaults.standard.string(forKey: "authToken") {
|
||||||
guard let token = defaults.string(forKey: "authToken") else {
|
headers["Authorization"] = "Bearer \(token)"
|
||||||
return headers
|
|
||||||
}
|
}
|
||||||
|
headers.merge(config.defaultHeaders) { (_, new) in new }
|
||||||
headers["Authorization"] = "Bearer \(token)"
|
|
||||||
|
|
||||||
return headers
|
return headers
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
switch resource.method {
|
switch resource.method {
|
||||||
case .get(let queryItems):
|
case .get(let queryItems):
|
||||||
|
if let queryItems = queryItems {
|
||||||
var components = URLComponents(url: resource.url, resolvingAgainstBaseURL: false)
|
var components = URLComponents(url: resource.url, resolvingAgainstBaseURL: false)
|
||||||
components?.queryItems = queryItems
|
components?.queryItems = queryItems
|
||||||
guard let url = components?.url else {
|
request.url = components?.url
|
||||||
throw NetworkError.badRequest
|
|
||||||
}
|
}
|
||||||
|
|
||||||
request = URLRequest(url: url)
|
|
||||||
|
|
||||||
case .post(let data),
|
case .post(let data),
|
||||||
.put(let data),
|
.put(let data),
|
||||||
.patch(let data):
|
.patch(let data):
|
||||||
request.httpMethod = resource.method.name
|
|
||||||
request.httpBody = data
|
request.httpBody = data
|
||||||
|
|
||||||
case .delete:
|
case .delete:
|
||||||
request.httpMethod = resource.method.name
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
request.allHTTPHeaderFields = defaultHeaders
|
||||||
|
|
||||||
let configuration = URLSessionConfiguration.default
|
let configuration = URLSessionConfiguration.default
|
||||||
configuration.httpAdditionalHeaders = defaultHeaders
|
configuration.httpAdditionalHeaders = defaultHeaders
|
||||||
let session = URLSession(configuration: configuration)
|
let session = URLSession(configuration: configuration)
|
||||||
|
|
||||||
let (data, response) = try await session.data(for: request)
|
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
|
throw NetworkError.invalidResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
if response.statusCode != 200 {
|
try validate(response: httpResponse)
|
||||||
switch response.statusCode {
|
|
||||||
case 404:
|
|
||||||
throw NetworkError.notFound
|
|
||||||
case 500:
|
|
||||||
throw NetworkError.serverError(response.description)
|
|
||||||
default:
|
|
||||||
throw NetworkError.invalidResponse
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let decoder = JSONDecoder()
|
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
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +1,24 @@
|
|||||||
import XCTest
|
|
||||||
@testable import Hermes
|
@testable import Hermes
|
||||||
|
import XCTest
|
||||||
|
|
||||||
final class HermesTests: XCTestCase {
|
final class HermesTests: XCTestCase {
|
||||||
|
override func setUp() {
|
||||||
|
super.setUp()
|
||||||
|
configureHermes()
|
||||||
|
}
|
||||||
|
|
||||||
|
private func configureHermes() {
|
||||||
|
let defaultHeaders = ["Custom-Header": "HeaderValue"]
|
||||||
|
let decodingStrategy = JSONDecoder.KeyDecodingStrategy.convertFromSnakeCase
|
||||||
|
let config = HermesConfiguration(defaultHeaders: defaultHeaders, decodingStrategy: decodingStrategy)
|
||||||
|
Hermes.shared.configure(config)
|
||||||
|
}
|
||||||
|
|
||||||
func testGetRequest() async throws {
|
func testGetRequest() async throws {
|
||||||
let hermes = Hermes()
|
|
||||||
let queryParams = [URLQueryItem(name: "foo", value: "bar")]
|
let queryParams = [URLQueryItem(name: "foo", value: "bar")]
|
||||||
let resource = Resource(url: URL(string: "https://httpbin.org/get")!, method: .get(queryParams), modelType: GetResponse.self)
|
let resource = Resource(url: URL(string: "https://httpbin.org/get")!, method: .get(queryParams), modelType: GetResponse.self)
|
||||||
|
|
||||||
let getResponse = try await hermes.load(resource)
|
let getResponse = try await Hermes.shared.load(resource)
|
||||||
|
|
||||||
XCTAssertEqual(getResponse.url, "https://httpbin.org/get?foo=bar")
|
XCTAssertEqual(getResponse.url, "https://httpbin.org/get?foo=bar")
|
||||||
XCTAssertEqual(getResponse.args.count, 1)
|
XCTAssertEqual(getResponse.args.count, 1)
|
||||||
@@ -16,52 +26,48 @@ final class HermesTests: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testPostRequest() async throws {
|
func testPostRequest() async throws {
|
||||||
let hermes = Hermes()
|
|
||||||
let postDatas = try? JSONEncoder().encode(["foo": "bar"])
|
let postDatas = try? JSONEncoder().encode(["foo": "bar"])
|
||||||
let resource = Resource(url: URL(string: "https://httpbin.org/post")!, method: .post(postDatas), modelType: PostResponse.self)
|
let resource = Resource(url: URL(string: "https://httpbin.org/post")!, method: .post(postDatas), modelType: PostResponse.self)
|
||||||
|
|
||||||
let postResponse = try await hermes.load(resource)
|
let postResponse = try await Hermes.shared.load(resource)
|
||||||
|
|
||||||
XCTAssertEqual(postResponse.url, "https://httpbin.org/post")
|
XCTAssertEqual(postResponse.url, "https://httpbin.org/post")
|
||||||
XCTAssertEqual(postResponse.args.count, 0)
|
XCTAssertEqual(postResponse.args.count, 0)
|
||||||
XCTAssertNotNil(postResponse.json)
|
XCTAssertNotNil(postResponse.json)
|
||||||
XCTAssertEqual(postResponse.json!.count, 1)
|
XCTAssertEqual(postResponse.json?.count, 1)
|
||||||
XCTAssertGreaterThan(postResponse.headers.count, 0)
|
XCTAssertGreaterThan(postResponse.headers.count, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testPutRequest() async throws {
|
func testPutRequest() async throws {
|
||||||
let hermes = Hermes()
|
|
||||||
let postDatas = try? JSONEncoder().encode(["foo": "bar"])
|
let postDatas = try? JSONEncoder().encode(["foo": "bar"])
|
||||||
let resource = Resource(url: URL(string: "https://httpbin.org/put")!, method: .put(postDatas), modelType: PostResponse.self)
|
let resource = Resource(url: URL(string: "https://httpbin.org/put")!, method: .put(postDatas), modelType: PostResponse.self)
|
||||||
|
|
||||||
let putResponse = try await hermes.load(resource)
|
let putResponse = try await Hermes.shared.load(resource)
|
||||||
|
|
||||||
XCTAssertEqual(putResponse.url, "https://httpbin.org/put")
|
XCTAssertEqual(putResponse.url, "https://httpbin.org/put")
|
||||||
XCTAssertEqual(putResponse.args.count, 0)
|
XCTAssertEqual(putResponse.args.count, 0)
|
||||||
XCTAssertNotNil(putResponse.json)
|
XCTAssertNotNil(putResponse.json)
|
||||||
XCTAssertEqual(putResponse.json!.count, 1)
|
XCTAssertEqual(putResponse.json?.count, 1)
|
||||||
XCTAssertGreaterThan(putResponse.headers.count, 0)
|
XCTAssertGreaterThan(putResponse.headers.count, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testPatchRequest() async throws {
|
func testPatchRequest() async throws {
|
||||||
let hermes = Hermes()
|
|
||||||
let postDatas = try? JSONEncoder().encode(["foo": "bar"])
|
let postDatas = try? JSONEncoder().encode(["foo": "bar"])
|
||||||
let resource = Resource(url: URL(string: "https://httpbin.org/patch")!, method: .patch(postDatas), modelType: PostResponse.self)
|
let resource = Resource(url: URL(string: "https://httpbin.org/patch")!, method: .patch(postDatas), modelType: PostResponse.self)
|
||||||
|
|
||||||
let patchResponse = try await hermes.load(resource)
|
let patchResponse = try await Hermes.shared.load(resource)
|
||||||
|
|
||||||
XCTAssertEqual(patchResponse.url, "https://httpbin.org/patch")
|
XCTAssertEqual(patchResponse.url, "https://httpbin.org/patch")
|
||||||
XCTAssertEqual(patchResponse.args.count, 0)
|
XCTAssertEqual(patchResponse.args.count, 0)
|
||||||
XCTAssertNotNil(patchResponse.json)
|
XCTAssertNotNil(patchResponse.json)
|
||||||
XCTAssertEqual(patchResponse.json!.count, 1)
|
XCTAssertEqual(patchResponse.json?.count, 1)
|
||||||
XCTAssertGreaterThan(patchResponse.headers.count, 0)
|
XCTAssertGreaterThan(patchResponse.headers.count, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testDeleteRequest() async throws {
|
func testDeleteRequest() async throws {
|
||||||
let hermes = Hermes()
|
|
||||||
let resource = Resource(url: URL(string: "https://httpbin.org/delete")!, method: .delete, modelType: PostResponse.self)
|
let resource = Resource(url: URL(string: "https://httpbin.org/delete")!, method: .delete, modelType: PostResponse.self)
|
||||||
|
|
||||||
let deleteResponse = try await hermes.load(resource)
|
let deleteResponse = try await Hermes.shared.load(resource)
|
||||||
|
|
||||||
XCTAssertEqual(deleteResponse.url, "https://httpbin.org/delete")
|
XCTAssertEqual(deleteResponse.url, "https://httpbin.org/delete")
|
||||||
XCTAssertEqual(deleteResponse.args.count, 0)
|
XCTAssertEqual(deleteResponse.args.count, 0)
|
||||||
@@ -70,35 +76,41 @@ final class HermesTests: XCTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testNotFound() async throws {
|
func testNotFound() async throws {
|
||||||
let hermes = Hermes()
|
|
||||||
let resource = Resource(url: URL(string: "https://httpbin.org/status/404")!, method: .get(), modelType: GetResponse.self)
|
let resource = Resource(url: URL(string: "https://httpbin.org/status/404")!, method: .get(), modelType: GetResponse.self)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let _ = try await hermes.load(resource)
|
let _ = try await Hermes.shared.load(resource)
|
||||||
|
XCTFail("Expected to throw NetworkError.notFound")
|
||||||
} catch NetworkError.notFound {
|
} catch NetworkError.notFound {
|
||||||
XCTAssert(true)
|
XCTAssert(true)
|
||||||
|
} catch {
|
||||||
|
XCTFail("Unexpected error: \(error)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testServerError() async throws {
|
func testServerError() async throws {
|
||||||
let hermes = Hermes()
|
|
||||||
let resource = Resource(url: URL(string: "https://httpbin.org/status/500")!, method: .get(), modelType: GetResponse.self)
|
let resource = Resource(url: URL(string: "https://httpbin.org/status/500")!, method: .get(), modelType: GetResponse.self)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let _ = try await hermes.load(resource)
|
let _ = try await Hermes.shared.load(resource)
|
||||||
|
XCTFail("Expected to throw NetworkError.serverError")
|
||||||
} catch NetworkError.serverError(_) {
|
} catch NetworkError.serverError(_) {
|
||||||
XCTAssert(true)
|
XCTAssert(true)
|
||||||
|
} catch {
|
||||||
|
XCTFail("Unexpected error: \(error)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testDecodeError() async throws {
|
func testDecodeError() async throws {
|
||||||
let hermes = Hermes()
|
|
||||||
let resource = Resource(url: URL(string: "https://httpbin.org/get")!, method: .get(), modelType: DummyResponse.self)
|
let resource = Resource(url: URL(string: "https://httpbin.org/get")!, method: .get(), modelType: DummyResponse.self)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
let _ = try await hermes.load(resource)
|
let _ = try await Hermes.shared.load(resource)
|
||||||
|
XCTFail("Expected to throw NetworkError.decodingError")
|
||||||
} catch NetworkError.decodingError {
|
} catch NetworkError.decodingError {
|
||||||
XCTAssert(true)
|
XCTAssert(true)
|
||||||
|
} catch {
|
||||||
|
XCTFail("Unexpected error: \(error)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user