|
1 | 1 | # Pinata |
| 2 | + |
2 | 3 | Simple declarative HTTP API framework |
| 4 | + |
| 5 | +## Basic Usage |
| 6 | + |
| 7 | +### Building a request |
| 8 | +First step is to build a request. You make requests by providing extension on top of `Request` type: |
| 9 | + |
| 10 | +```swift |
| 11 | +extension Request { |
| 12 | + static let func login(_ body: UserBody) -> Self where Output == UserResponse { |
| 13 | + .post("login", body: body) |
| 14 | + } |
| 15 | +} |
| 16 | +``` |
| 17 | + |
| 18 | +And... voila! We defined a `login(_:)` request which will request login endpoint by sending a `UserBody` and waiting for a `UserResponse`. Now it's time to use it. |
| 19 | + |
| 20 | +You can also use an enum to define your Request path: |
| 21 | + |
| 22 | +```swift |
| 23 | +enum MyAppEndpoint: String, Path { |
| 24 | + case login |
| 25 | +} |
| 26 | + |
| 27 | +extension Request { |
| 28 | + static let func login(_ body: UserBody) -> Self where Output == UserResponse { |
| 29 | + .post(MyAppEndpoint.login, body: body) |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### Sending a request |
| 35 | + |
| 36 | +To send a request use a `Session` instance. `Session` is somewhat similar to `URLSession` but providing additional functionalities. |
| 37 | + |
| 38 | +```swift |
| 39 | + |
| 40 | +let session = Session(baseURL: URL(string: "https://github.com")!, encoder: JSONEncoder(), decoder: JSONDecoder()) |
| 41 | + |
| 42 | +session.publisher(for: .login(UserBody(username: "pjechris", password: "MyPassword"))) |
| 43 | + |
| 44 | +``` |
| 45 | + |
| 46 | +You can now use the returned publisher however you want. Its result is similar to what you have received with `URLSession.shared.dataTaskPublisher(for: ...).decode(type: UserResponse.self, decoder: JSONDecoder())`. |
| 47 | + |
| 48 | +A few words about Session: |
| 49 | + |
| 50 | +- `baseURL` will be prepended to all call endpoints |
| 51 | +- You can skip encoder and decoder if you use JSON |
| 52 | +- You can provide a custom `URLSession` instance if ever needed |
| 53 | + |
| 54 | +## Interceptor |
| 55 | + |
| 56 | +Protocol `Interceptor` enable powerful request interceptions. This include authentication, logging, request retrying, etc... |
| 57 | + |
| 58 | +### `RequestInterceptor` |
| 59 | + |
| 60 | +`RequestInterceptor` allow to adapt a or retry a request whenever it failed: |
| 61 | + |
| 62 | +- `adaptRequest` method is called before making a request and allow you to transform it adding headers, changing path, ... |
| 63 | +- `rescueRequestError` is called whenever the request fail. You'll have a chance to retry the request. This can be used to re-authenticate the user for instance |
| 64 | + |
| 65 | +### `ResponseInterceptor` |
| 66 | + |
| 67 | +`ResponseInterceptor` is dedicated to intercept and server responses: |
| 68 | + |
| 69 | +- `adaptResponse` change the server output |
| 70 | +- `receivedResponse` notify about the server final response (a valid output or error) |
0 commit comments