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
HTTPClientto use it.Implement a request body structure that conforms to the
Encodableprotocol. (Optional)Implement a response body structure that conforms to the
Decodableprotocol.Implement a request structure that conforms to the
Requestprotocol.Create an instance of the
HTTPClientclass and call therequestmethod.Contribution
I would be happy if you contribute :)
Stats