|
1 | | -# NetworkMapper |
| 1 | +# NetworkMapper |
| 2 | + |
| 3 | +A framework to map JSON responses to swift objects, based on |
| 4 | +[Alamofire](https://github.com/Alamofire/Alamofire) and |
| 5 | +[Unbox](https://github.com/JohnSundell/Unbox). |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +### Cocoapods |
| 10 | +```ruby |
| 11 | +pod 'NetworkMapper', '~> 0.0.3' |
| 12 | +``` |
| 13 | + |
| 14 | +## Usage |
| 15 | +NetworkMapper makes use of protocols to implement functionality. The protocols |
| 16 | +only require you to define instance variables for the method, url and parameters |
| 17 | +of the request. |
| 18 | + |
| 19 | +### Basic Request |
| 20 | +You can make basic requests that don't map the response by conforming to |
| 21 | +`NetworkRequest`. You can use `responseJSON` or `responseData` to retrieve the |
| 22 | +response from the server. |
| 23 | + |
| 24 | +```swift |
| 25 | +struct ExampleRequest: NetworkRequest { |
| 26 | + let method: HTTPMethod = .get |
| 27 | + let url: URL = URL(string: "https://example.org/example/user") |
| 28 | + let parameters: [String:Any]? = nil |
| 29 | +} |
| 30 | + |
| 31 | +ExampleRequest().responseJSON { response in |
| 32 | + switch response.result { |
| 33 | + case .failure(let error): |
| 34 | + // process error |
| 35 | + case .success(let json): |
| 36 | + // process response |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +### Mapped Request |
| 42 | +A mapped request is slightly more complicated. Mapped requests require you to |
| 43 | +create an object that conforms to `NetworkObjectRequest` and |
| 44 | +`NetworkObjectResponse`. |
| 45 | + |
| 46 | +The response protocol requires you to conform to the `Unboxable` protocol, see |
| 47 | +it's [documentation](https://github.com/JohnSundell/Unbox#unbox) for more |
| 48 | +information. The response object is treated as the root object of the JSON |
| 49 | +response. Currently arrays as a root object is not supported. |
| 50 | + |
| 51 | +The request protocol adds on to `NetworkRequest` by adding an associatedtype, |
| 52 | +which specifies the type of response object. On completion of the request, the |
| 53 | +JSON object will be mapped to the response object. There is one other function, |
| 54 | +`responseDecoded` which can be optionally implemented, the default |
| 55 | +implementation of which does nothing. |
| 56 | + |
| 57 | +```swift |
| 58 | +struct ExampleObjectRequest: NetworkObjectRequest { |
| 59 | + typealias ResponseType = ExampleObjectResponse |
| 60 | + |
| 61 | + let method: HTTPMethod = .get |
| 62 | + let url: URL = URL(string: "https://example.org/example/users") |
| 63 | + let parameters: [String:Any]? = nil |
| 64 | +} |
| 65 | + |
| 66 | +struct ExampleObjectResponse: NetworkObjectResponse { |
| 67 | + let users: [User] |
| 68 | + let pageNumber: Int |
| 69 | + |
| 70 | + init(unboxer: Unboxable) throws { |
| 71 | + self.users = try unboxer.unbox(key: "users") |
| 72 | + self.pageNumber = try unboxer.unbox(keyPath: "pagination.page_number") |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +struct User: Unboxable { |
| 77 | + let name: String |
| 78 | + let age: Int |
| 79 | + |
| 80 | + init(unboxer: Unboxable) throws { |
| 81 | + self.name = try unboxer.unbox(key: "name") |
| 82 | + self.name = try unboxer.unbox(key: "age") |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +ExampleObjectRequest().responseObject { response in |
| 87 | + switch response.result { |
| 88 | + case .failure(let error): |
| 89 | + // process error |
| 90 | + case .success(let object): |
| 91 | + // process response |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
0 commit comments