https://github.com/prosumma/httpfluent
A fluent interface over REST written in Swift
https://github.com/prosumma/httpfluent
http rest rest-client swift
Last synced: about 1 year ago
JSON representation
A fluent interface over REST written in Swift
- Host: GitHub
- URL: https://github.com/prosumma/httpfluent
- Owner: Prosumma
- License: mit
- Created: 2020-04-03T22:16:58.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-04-15T06:51:46.000Z (almost 3 years ago)
- Last Synced: 2025-01-29T11:46:16.661Z (about 1 year ago)
- Topics: http, rest, rest-client, swift
- Language: Swift
- Homepage:
- Size: 84 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# HTTPFluent
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).
## Integration
HTTPFluent is available only via Swift Package Manager.
## Usage
HTTPFluent is extremely intuitive to use, so a few examples will suffice:
```swift
let id = 2349713
let jwt = "xyz123"
let request = 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 in
do {
let user = try result.get()
} catch {
// Oops, no user
}
}
// Async style
let user = try await 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.
```swift
let urlRequest = URLClient(url: "https://myapi.com")
.path("user", id)
.authorization(bearer: jwt)
.put(data: data) // Here we put raw data instead of JSON.
.request
```
HTTPFluent uses immutable state. Each step in the chain to build the `URLRequest` copies a `URLRequestBuilder` struct. All operations are thus additive, encouraging reuse.
```swift
// Set up the shared information about the request.
let fluent = 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.
let postWithId = fluent.path(id).post(json: User.self)
let user = try await postWithId.receive(json: User.self)
```