Compare commits
2 Commits
ed88a7f103
...
f4163a56ea
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f4163a56ea | ||
|
|
4c415365a2 |
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
.DS_Store
|
||||||
|
/.build
|
||||||
|
/Packages
|
||||||
|
xcuserdata/
|
||||||
|
DerivedData/
|
||||||
|
.swiftpm/configuration/registries.json
|
||||||
|
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
|
||||||
|
.netrc
|
||||||
27
Package.swift
Normal file
27
Package.swift
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
// swift-tools-version: 5.9
|
||||||
|
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
||||||
|
|
||||||
|
import PackageDescription
|
||||||
|
|
||||||
|
let package = Package(
|
||||||
|
name: "Hermes",
|
||||||
|
platforms: [
|
||||||
|
.macOS(.v13),
|
||||||
|
.iOS(.v15)
|
||||||
|
],
|
||||||
|
products: [
|
||||||
|
// Products define the executables and libraries a package produces, making them visible to other packages.
|
||||||
|
.library(
|
||||||
|
name: "Hermes",
|
||||||
|
targets: ["Hermes"]),
|
||||||
|
],
|
||||||
|
targets: [
|
||||||
|
// Targets are the basic building blocks of a package, defining a module or a test suite.
|
||||||
|
// Targets can depend on other targets in this package and products from dependencies.
|
||||||
|
.target(
|
||||||
|
name: "Hermes"),
|
||||||
|
.testTarget(
|
||||||
|
name: "HermesTests",
|
||||||
|
dependencies: ["Hermes"]),
|
||||||
|
]
|
||||||
|
)
|
||||||
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
|
||||||
|
}
|
||||||
|
}
|
||||||
12
Tests/HermesTests/HermesTests.swift
Normal file
12
Tests/HermesTests/HermesTests.swift
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import XCTest
|
||||||
|
@testable import Hermes
|
||||||
|
|
||||||
|
final class HermesTests: XCTestCase {
|
||||||
|
func testExample() throws {
|
||||||
|
// XCTest Documentation
|
||||||
|
// https://developer.apple.com/documentation/xctest
|
||||||
|
|
||||||
|
// Defining Test Cases and Test Methods
|
||||||
|
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user