import HTTPClient
final class VersatileAPIClient {
static let shared = VersatileAPIClient()
private let httpClient = HTTPClient(baseURLString: "https://example.com/api/")
}
extension VersatileAPIClient: VersatileRepository {
func registerUser(name: String, description: String) async throws -> UserID {
let requestBody = RegisterUserRequestBody(name: name, description: description)
let responseBody = try await httpClient.request(RegisterUserRequest(), requestBody: requestBody)
return responseBody.convertToUserID()
}
}
do {
let userID = try await VersatileAPIClient.shared.registerUser(name: "Uhooi", description: "Green monster.")
// Do something.
} catch {
// Do error handling.
print(error)
}
swift-http-client
Communicate via HTTP easily in Swift.
Table of Contents
System requirements
Installation
Swift Package Manager (Recommended)
Package
You can add this package to
Package.swift
, include it in your target dependencies.Xcode
You can add this package on Xcode. See documentation.
CocoaPods
This library is available through CocoaPods. To install it, simply add the following line to your
Podfile
:Carthage
This library is available through Carthage. To install it, simply add the following line to your
Cartfile
:How to use
You can just import
HTTPClient
to use it.Implement a request body structure that conforms to the
Encodable
protocol. (Optional)Implement a response body structure that conforms to the
Decodable
protocol.Implement a request structure that conforms to the
Request
protocol.Create an instance of the
HTTPClient
class and call therequest
method.Contribution
I would be happy if you contribute :)
Stats