Files
suixinkan_uikit/suixinkan/Core/Networking/APIRequest.swift

54 lines
1.4 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// APIRequest.swift
// suixinkan
//
import Foundation
/// HTTP
enum HTTPMethod: String {
case get = "GET"
case post = "POST"
case put = "PUT"
case delete = "DELETE"
}
/// API Header
nonisolated struct APIRequest<Response: Decodable> {
var method: HTTPMethod
var path: String
var queryItems: [URLQueryItem]
var headers: [String: String]
var body: AnyEncodable?
/// API AnyEncodable
init<Body: Encodable>(
method: HTTPMethod,
path: String,
queryItems: [URLQueryItem] = [],
headers: [String: String] = [:],
body: Body? = Optional<EmptyPayload>.none
) {
self.method = method
self.path = path
self.queryItems = queryItems
self.headers = headers
self.body = body.map(AnyEncodable.init)
}
}
/// Encodable APIRequest
nonisolated struct AnyEncodable: Encodable, Sendable {
private let encodeValue: @Sendable (Encoder) throws -> Void
init<Value: Encodable>(_ value: Value) {
encodeValue = { encoder in
try value.encode(to: encoder)
}
}
func encode(to encoder: Encoder) throws {
try encodeValue(encoder)
}
}