HTTPFluent provides a fluent interface over HTTP, primarily designed to work with APIs. HTTPFluent supports three styles: callback, async (with Swift >= 5.5) and Combine (on Apple platforms).
HTTPFluent is available only via Swift Package Manager.
HTTPFluent is extremely intuitive to use, so a few examples will suffice:
letid=2349713letjwt="xyz123"letrequest=URLClient(url:"https://myapi.com").path("user", id).authorization(bearer: jwt).post(json:User(name:"Don Quixote"))
// Callback style
request.receive(json:User.self){ result indo{letuser=try result.get()}catch{
// Oops, no user
}}
// Async style
letuser=tryawait request.receive(json:User.self)
// Combine style
request.receivePublisher(json:User.self).sink{ completion in
// Handle completion
} receiveValue:{ user in
// Do something with user
}.store(in:&cancellables)HTTPFluent can also be used to generate a URLRequest without invoking it.
leturlRequest=URLClient(url:"https://myapi.com").path("user", id).authorization(bearer: jwt).put(data: data) // Here we put raw data instead of JSON.
.requestHTTPFluent uses immutable state. Each step in the chain to build the URLRequest copies a URLRequestBuilder struct. All operations are thus additive, encouraging reuse.
// Set up the shared information about the request.
letfluent=URLClient(url:"https://myapi.com").authorization(bearer: jwt).path("user")
// This adds the value of id as a path element, so the result is
// the path /user/123 or whatever the value of id is.
letpostWithId= fluent.path(id).post(json:User.self)letuser=tryawait postWithId.receive(json:User.self)