|
| 1 | +# ConveyPI |
| 2 | +[]() []() [](LICENSE.md) [](https://app.bitrise.io/app/9bd0d2e769e903f9) |
| 3 | + |
| 4 | +This library allows easy [HTTP](https://tools.ietf.org/html/rfc7231) requests in [Swift](https://swift.org) against [REST](https://en.wikipedia.org/wiki/Representational_state_transfer)-style [APIs](https://en.wikipedia.org/wiki/Application_programming_interface) with [JSON](https://www.json.org/) formatting by supporting [codable](https://developer.apple.com/documentation/swift/codable) bodies and [promised](https://github.com/mxcl/PromiseKit) responses. |
| 5 | + |
| 6 | +## Etymology |
| 7 | +ConveyPI (`/kənˈveɪ-piː-aɪ/`) is a contraction of **Convey** (*to carry, bring, or take from one place to another*) and **API** (*Application Programming Interface*). |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +ConveyPI has the method |
| 12 | +```swift |
| 13 | +func request<T, U, E>(method: APIMethod, |
| 14 | + baseURL: URL, |
| 15 | + resource: String, |
| 16 | + headers: [String: String]?, |
| 17 | + params: [String: Any]?, |
| 18 | + body: T?, |
| 19 | + error: E.Type, |
| 20 | + decorator: ((inout URLRequest) -> Void)?) -> Promise<U> |
| 21 | +``` |
| 22 | +where `T: Encodable, U: Decodable, E: (Error & Decodable)` at its core. |
| 23 | + |
| 24 | +This method allows you to request a resource from an API specifying the |
| 25 | +- method (*e.g. `GET`*), |
| 26 | +- baseURL, |
| 27 | +- resource URI (*e.g. `/users/42`*), |
| 28 | +- http headers as a dictionary, |
| 29 | +- query params as a dictionary, |
| 30 | +- request body (any type that conforms to `Encodable`), |
| 31 | +- an error struct (`Decodable`) your API might respond with and, |
| 32 | +- a decorator to access/alter the `URLRequest` that gets fired underneath |
| 33 | + |
| 34 | +and getting the response with a type (`U`) conforming to `Decodable`. All of the error handling (*status code, empty response, etc.*) and parsing is done for you. |
| 35 | + |
| 36 | +### Requesting a resource |
| 37 | + |
| 38 | +Request a resource by specifying |
| 39 | + |
| 40 | +```swift |
| 41 | +struct User: Codable { |
| 42 | + let id: Int |
| 43 | + let name: String |
| 44 | +} |
| 45 | + |
| 46 | +let api = ConveyPI() |
| 47 | +let baseURL = URL(string: "https://jsonplaceholder.typicode.com")! |
| 48 | +firstly { () -> Promise<User> in |
| 49 | + api.request(method: .GET, baseURL: baseURL, resource: "/users/1", error: ConveyPIError.self) |
| 50 | +}.done { user in |
| 51 | + print(user) // User(id: 1, name: "Leanne Graham") |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### Specifying an error |
| 56 | + |
| 57 | +If your API has an error JSON it is responsing with, just define your error response and hand it in: |
| 58 | + |
| 59 | +```swift |
| 60 | +struct MyAPIError: Error, Codable { |
| 61 | + let code: Int |
| 62 | + let message: String |
| 63 | +} |
| 64 | + |
| 65 | +firstly { () -> Promise<User> in |
| 66 | + api.request(method: .GET, baseURL: baseURL, resource: "/users/1", error: MyAPIError.self) |
| 67 | +}.done { user in |
| 68 | + // [...] |
| 69 | +}.catch { error in |
| 70 | + switch error { |
| 71 | + case let error as MyAPIError: print(error.code) |
| 72 | + default: break // Request error, network down, etc. |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +## Install |
| 78 | + |
| 79 | +### Cocoapods |
| 80 | + |
| 81 | +```ruby |
| 82 | +pod 'ConveyPI' |
| 83 | +``` |
| 84 | + |
| 85 | + |
| 86 | +## Acknowledgments |
| 87 | + |
| 88 | +This uses [mxcl/PromiseKit](https://github.com/mxcl/PromiseKit) as a dependency. |
0 commit comments