✨ Add GET, POST & DELETE methods
This commit is contained in:
103
Sources/Hermes/Hermes.swift
Normal file
103
Sources/Hermes/Hermes.swift
Normal file
@@ -0,0 +1,103 @@
|
||||
//
|
||||
// hermes.swift
|
||||
//
|
||||
// Created by victor bodinaud on 11/01/2024.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public enum NetworkError: Error {
|
||||
case badRequest
|
||||
case serverError(String)
|
||||
case decodingError
|
||||
case invalidResponse
|
||||
}
|
||||
|
||||
extension NetworkError: LocalizedError {
|
||||
public var errorDescription: String? {
|
||||
switch self {
|
||||
case .badRequest:
|
||||
return NSLocalizedString("Unable to perform request", comment: "badRequestError")
|
||||
case .serverError(let errorMessage):
|
||||
print(errorMessage)
|
||||
return NSLocalizedString(errorMessage, comment: "serverError")
|
||||
case .decodingError:
|
||||
return NSLocalizedString("Unable to decode successfully", comment: "decodingError")
|
||||
case .invalidResponse:
|
||||
return NSLocalizedString("Invalid response", comment: "invalidResponse")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum HTTPMethod {
|
||||
case get([URLQueryItem])
|
||||
case post(Data?)
|
||||
case delete
|
||||
|
||||
var name: String {
|
||||
switch self {
|
||||
case .get:
|
||||
return "GET"
|
||||
case .post:
|
||||
return "POST"
|
||||
case .delete:
|
||||
return "DELETE"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct Resource<T: Codable> {
|
||||
let url: URL
|
||||
var method: HTTPMethod = .get([])
|
||||
var modelType: T.Type
|
||||
|
||||
public init(url: URL, method: HTTPMethod, modelType: T.Type) {
|
||||
self.url = url
|
||||
self.method = method
|
||||
self.modelType = modelType
|
||||
}
|
||||
}
|
||||
|
||||
public struct Hermes {
|
||||
|
||||
public init() { }
|
||||
|
||||
public func load<T: Codable>(_ resource: Resource<T>) async throws -> T {
|
||||
|
||||
var request = URLRequest(url: resource.url)
|
||||
|
||||
switch resource.method {
|
||||
case .get(let queryItems):
|
||||
var components = URLComponents(url: resource.url, resolvingAgainstBaseURL: false)
|
||||
components?.queryItems = queryItems
|
||||
guard let url = components?.url else {
|
||||
throw NetworkError.badRequest
|
||||
}
|
||||
|
||||
request = URLRequest(url: url)
|
||||
|
||||
case .post(let data):
|
||||
request.httpMethod = resource.method.name
|
||||
request.httpBody = data
|
||||
|
||||
case .delete:
|
||||
request.httpMethod = resource.method.name
|
||||
}
|
||||
|
||||
let configuration = URLSessionConfiguration.default
|
||||
configuration.httpAdditionalHeaders = ["Content-Type": "application/json"]
|
||||
let session = URLSession(configuration: configuration)
|
||||
|
||||
let (data, response) = try await session.data(for: request)
|
||||
|
||||
guard let _ = response as? HTTPURLResponse else {
|
||||
throw NetworkError.invalidResponse
|
||||
}
|
||||
|
||||
guard let result = try? JSONDecoder().decode(resource.modelType, from: data) else {
|
||||
throw NetworkError.decodingError
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user