AsyncHTTP

1.1.0

Swift networking with async/await
tevelee/AsyncHTTP

What's New

1.1.0

2022-07-29T13:52:13Z
  • Adopted primary associated types: allows opaque type constraints such as some Loader<URLRequest, HTTPURLResponse>
  • Advanced rethrows mechanism: load() only throws if there is an explicitly throwing operator in the loader chain

AsyncHTTP

Generic networking library written using Swift async/await

let request = try HTTPRequest().configured { request in
    request.path = "endpoint"
    request.method = .post
    request.body = try .json(["a": "b"])
    request[header: .accept] = .application.json.appending(.characterSet, value: .utf8)
    request[header: .authorization] = .bearer(token: "token")
    request.addQueryParameter(name: "q", value: "search")
    request.serverEnvironment = .production
    request.timeout = 60
    request.retryStrategy = .immediately(maximumNumberOfAttempts: 3)
}

let loader: some HTTPLoader = URLSession.shared.httpLoader()
    .applyServerEnvironment()
    .applyTimeout(default: 30)
    .applyRetryStrategy()
    .deduplicate()
    .throttle(maximumNumberOfRequests: 2)

let response: HTTPResponse = try await loader.load(request)
let body: MyResponseStruct = try response.jsonBody()

print(request.formatted())
print(response.formatted())

Description

  • Swift Tools 5.5.0
View More Packages from this Author

Dependencies

Last updated: Sun Nov 10 2024 19:54:50 GMT-1000 (Hawaii-Aleutian Standard Time)