Request implementation for Swift
This implementation is based on Paylike/JS-Request
SPM:
// dependencies:
.package(url: "[email protected]:paylike/swift-request.git", .upToNextMajor(from: "0.3.0"))
// target:
.product(name: "PaylikeRequest", package: "swift-request")
Cocoapods: https://cocoapods.org/pods/PaylikeRequest
pod 'PaylikeRequest'
import PaylikeRequest
// ...
// optionally logging function can be overwritten
let httpClient = PaylikeHTTPClient(log: { item in
print(item) // Item is encodable
})
let options = RequestOptions(
withData: ["foo": "bar"]
)
// completion handler version
httpClient.sendRequest(
to: URL(string: "http://localhost:8080/bar")!,
withOptions: options
) { result in
// handle result in callback style
}
// Async version
Task {
let response = try await httpClient.sendRequest(
to: URL(string: "http://localhost:8080/bar")!,
withOptions: options
)
}
// A more simple usage with options default parameters:
// ...
Task {
let response = try await httpClient.sendRequest(
to: URL(string: "http://localhost:8080/bar")!
)
}