You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -119,13 +119,39 @@ Old `performWithSuccess(_:failure:)` method is deprecated and will be removed in
119
119
Since we no longer hide Alamofire from developer, we are now able to provide perform method with `Alamofire.Response` completion block, which can be used in several useful ways, for example observing request timeline:
120
120
121
121
```swift
122
-
request.perform(completion: { response in
122
+
request.perform(completion: { response in
123
123
print(response.result) // Alamofire.Result
124
124
print(response.timeline) // Alamofire.Timeline
125
125
print(response.response) // NSHTTPURLResponse?
126
126
})
127
127
```
128
128
129
+
### ResponseParseable protocol
130
+
131
+
`ResponseParseable` protocol received major overhaul in this release. Previously, we used associatedtype ModelType to define type of parsed response. It was not very obvious, and required declaring all generic constraints as `Model.ModelType`. In 1.0.0, we changed protocol to contain simple throwing initializer:
132
+
133
+
```swift
134
+
publicprotocolResponseParseable {
135
+
init(data: NSData) throws
136
+
}
137
+
```
138
+
139
+
This way we are allowing to use any kind of parser, not only JSON, and any other mapper as well. For example, here's how SwiftyJSON extension is implemented:
That's really all you have to do to use a custom mapper with TRON.
154
+
129
155
### Miscellaneous API improvements
130
156
131
157
Previously, `MultipartAPIRequest` was a subclass of `APIRequest`, which allowed to define it's variable as `APIRequest`, and potentially could lead to runtime crash with improper use. Now, both `MultipartAPIRequest` and `APIRequest` subclass `BaseRequest`, thus preventing this use case.
@@ -134,4 +160,4 @@ Previously, `MultipartAPIRequest` was a subclass of `APIRequest`, which allowed
134
160
135
161
`EventDispatcher` class, `TRON.dispatcher` and `APIRequest.dispatcher` properties are replaced with `processingQueue` and `resultDeliveryQueue` properties on `TRON` and `APIRequest`. `processingQueue` property determines on which queue should response parsing proceed, and `resultDeliveryQueue` determines on which queue completion blocks should be called.
136
162
137
-
`RxSwift` extension `rxMultipartResult()` method on `MultipartAPIRequest` now returns Observable<ModelType> instead of tuple of observables.
163
+
`RxSwift` extension `rxMultipartResult()` method on `MultipartAPIRequest` now returns Observable<Model> instead of tuple of observables.
Copy file name to clipboardExpand all lines: README.md
+23-25
Original file line number
Diff line number
Diff line change
@@ -42,8 +42,8 @@ request.perform(success: { user in
42
42
43
43
## Requirements
44
44
45
-
- XCode 7.3+
46
-
- Swift 2.2+
45
+
- XCode 7.3
46
+
- Swift 2.2
47
47
- iOS 8
48
48
49
49
## Installation
@@ -96,7 +96,7 @@ public protocol NSURLBuildable {
96
96
}
97
97
```
98
98
99
-
By default, `TRON` uses `URLBuilder` class, that simply appends relative path to base URL, which is sufficient in most cases. You can customize url building process globally by changing `urlBuilder` property on `Tron` or locally, for a single request by modifying `urlBuilder` property on `APIRequest`.
99
+
By default, `TRON` uses `URLBuilder` class, that simply appends relative path to base URL, which is sufficient in most cases. You can customize url building process globally by changing `urlBuilder` property on `TRON` or locally, for a single request by modifying `urlBuilder` property on `APIRequest`.
100
100
101
101
### HeaderBuildable
102
102
@@ -116,7 +116,7 @@ public enum AuthorizationRequirement {
116
116
}
117
117
```
118
118
119
-
It represents scenarios where user is not authorized, user is authorized, but authentication is not required, and a case, where request requires authentication.
119
+
It represents scenarios where user is not authorized, user is authorized, but authorization is not required, and a case, where request requires authorization.
120
120
121
121
By default, `TRON` uses `HeaderBuilder` class, which adds "Accept":"application/json" header to your requests.
122
122
@@ -149,15 +149,25 @@ request.perform(success: { result in }, failure: { error in })?.progress { bytes
149
149
150
150
## Response parsing
151
151
152
-
Generic `APIRequest` implementation allows us to define expected response type before request is even sent. It also allows us to setup basic parsing rules, which is where `SwiftyJSON`comes in. We define a simple `JSONDecodable` protocol, that allows us to create models of specific type:
152
+
Generic `APIRequest` implementation allows us to define expected response type before request is even sent. It also allows us to setup basic parsing rules, which is where `ResponseParseable` protocol comes in.
153
153
154
154
```swift
155
-
publicprotocolJSONDecodable {
156
-
init(json: JSON)
155
+
publicprotocolResponseParseable {
156
+
init(data: NSData) throws
157
157
}
158
158
```
159
159
160
-
To parse your response from the server, all you need to do is to create `JSONDecodable` conforming type, for example:
160
+
As you can see, protocol accepts NSData in initializer, which means anything can be parsed - JSON, or XML or something else.
161
+
162
+
`TRON` also provides `JSONDecodable` protocol, that allows us to parse models using SwiftyJSON:
163
+
164
+
```swift
165
+
publicprotocolJSONDecodable: ResponseParseable {
166
+
init(json: JSON) throws
167
+
}
168
+
```
169
+
170
+
To parse your response from the server using `SwiftyJSON`, all you need to do is to create `JSONDecodable` conforming type, for example:
161
171
162
172
```swift
163
173
classUser: JSONDecodable {
@@ -180,7 +190,7 @@ request.perform(success: { user in
180
190
})
181
191
```
182
192
183
-
There are also default implementations of `JSONDecodable` protocol for Swift built-in types like String, Int, Float, Double and Bool, so you can easily do something like this:
193
+
There are also default implementations of `JSONDecodable` protocol for Swift built-in types like Array, String, Int, Float, Double and Bool, so you can easily do something like this:
184
194
185
195
```swift
186
196
let request : APIRequest<String,MyAppError> = tron.request(path: "status")
@@ -189,25 +199,15 @@ request.perform(success: { status in
189
199
})
190
200
```
191
201
192
-
You can also use `EmptyResponse` struct in cases where you don't care about actual response, or response from the server is empty(<>).
202
+
You can also use `EmptyResponse` struct in cases where you don't care about actual response.
193
203
194
204
## Custom mappers
195
205
196
-
Instead of `JSONDecodable`, all generic constraints on TRON accept `ResponseParseable` protocol, that can be easily implemented for your mapper.
197
-
198
-
By default, we are using SwiftyJSON, and adding protocol default implementations on it.
206
+
All generic constraints on TRON accept `ResponseParseable` protocol, that can be easily implemented for your mapper.
199
207
200
-
**Note** Custom mappers are supported only when installing framework from CocoaPods due to inability of Carthage to split framework to subspecs.
208
+
We are providing code examples on how to do this with two most mappers available in Swift - Unbox and ObjectMapper.
201
209
202
-
To use custom mapper, use Core podspec of TRON:
203
-
204
-
```ruby
205
-
pod 'TRON/Core'
206
-
```
207
-
208
-
Then add your custom mapper protocol extension. We are providing code examples on how to do this with two most popular mappers available in Swift - Argo and ObjectMapper.
209
-
210
-
[Playground with Argo ResponseParseable implementation](https://github.com/MLSDev/TRON/blob/master/Custom%20mappers/Argo.playground/Contents.swift)
210
+
[Playground with Unbox ResponseParseable implementation](https://github.com/MLSDev/TRON/blob/master/Custom%20mappers/Unbox.playground/Contents.swift)
211
211
212
212
[Playground with ObjectMapper ResponseParseable implementation](https://github.com/MLSDev/TRON/blob/master/Custom%20mappers/ObjectMapper.playground/Contents.swift)
213
213
@@ -443,8 +443,6 @@ We are dedicated to building best possible tool for interacting with RESTful web
443
443
444
444
`TRON` was heavily inspired by [Moya framework](https://github.com/Moya/Moya) and [LevelUPSDK](https://github.com/TheLevelUp/levelup-sdk-ios/blob/master/Source/API/Client/LUAPIClient.h)
445
445
446
-
There are also alternative JSON mappers available, such as [Unbox](https://github.com/JohnSundell/Unbox)
447
-
448
446
## License
449
447
450
448
`TRON` is released under the MIT license. See LICENSE for details.
0 commit comments