Merge pull request #109 from TouchInstinct/feature/response_primitive

Ability to map primitive response types (`String`, `Int`, `[String]`, etc.).
This commit is contained in:
Ivan Smolin 2017-12-05 16:22:36 +03:00 committed by GitHub
commit 224acadf7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 13 deletions

View File

@ -1,5 +1,9 @@
# Changelog
### 0.6.6
- **Add**: Ability to map primitive response types (`String`, `Int`, `[String]`, etc.).
### 0.6.5
- **Add**: Ability to handle responses with non-default status codes.

View File

@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "LeadKit"
s.version = "0.6.5"
s.version = "0.6.6"
s.summary = "iOS framework with a bunch of tools for rapid development"
s.homepage = "https://github.com/TouchInstinct/LeadKit"
s.license = "Apache License, Version 2.0"

View File

@ -50,7 +50,7 @@ public extension Reactive where Base: Alamofire.SessionManager {
.map { $0.validate(statusCode: acceptableStatusCodes) }
}
/// Method which executes request and serializes response into target object
/// Method that executes request and serializes response into target object
///
/// - Parameter requestParameters: api parameters to pass Alamofire
/// - Parameter mappingQueue: The dispatch queue to use for mapping
@ -64,7 +64,7 @@ public extension Reactive where Base: Alamofire.SessionManager {
.flatMap { $0.rx.apiResponse(mappingQueue: mappingQueue) }
}
/// Method which executes request and serializes response into array of target objects
/// Method that executes request and serializes response into array of target objects
///
/// - Parameter requestParameters: api parameters to pass Alamofire
/// - Parameter mappingQueue: The dispatch queue to use for mapping
@ -78,7 +78,21 @@ public extension Reactive where Base: Alamofire.SessionManager {
.flatMap { $0.rx.apiResponse(mappingQueue: mappingQueue) }
}
/// Method which executes request and serializes response into target object
/// Method that executes request and serializes response into target type
///
/// - Parameter requestParameters: api parameters to pass Alamofire
/// - Parameter mappingQueue: The dispatch queue to use for mapping
/// - Returns: Observable with HTTP URL Response and target object
func responseObject<T>(requestParameters: ApiRequestParameters,
mappingQueue: DispatchQueue = .global(),
acceptableStatusCodes: [Int] = Base.defaultAcceptableStatusCodes)
-> Observable<(response: HTTPURLResponse, object: T)> {
return apiRequest(requestParameters: requestParameters, acceptableStatusCodes: acceptableStatusCodes)
.flatMap { $0.rx.apiResponse(mappingQueue: mappingQueue) }
}
/// Method that executes request and serializes response into target object
///
/// - Parameter requestParameters: api parameters to pass Alamofire
/// - Parameter mappingQueue: The dispatch queue to use for mapping
@ -92,7 +106,7 @@ public extension Reactive where Base: Alamofire.SessionManager {
.flatMap { $0.rx.observableApiResponse(mappingQueue: mappingQueue) }
}
/// Method which executes request and serializes response into array of target objects
/// Method that executes request and serializes response into array of target objects
///
/// - Parameter requestParameters: api parameters to pass Alamofire
/// - Parameter mappingQueue: The dispatch queue to use for mapping

View File

@ -29,7 +29,9 @@ typealias ServerResponse = (response: HTTPURLResponse, result: Any)
public extension Reactive where Base: DataRequest {
/// Method which serializes response into target object
private typealias JSON = [String: Any]
/// Method that serializes response into target object
///
/// - Parameter mappingQueue: The dispatch queue to use for mapping
/// - Returns: Observable with HTTP URL Response and target object
@ -38,13 +40,13 @@ public extension Reactive where Base: DataRequest {
return responseJSONOnQueue(mappingQueue)
.tryMapResult { resp, value in
let json = try cast(value) as [String: Any]
let json = try cast(value) as JSON
return (resp, try T(JSON: json))
}
}
/// Method which serializes response into array of target objects
/// Method that serializes response into array of target objects
///
/// - Parameter mappingQueue: The dispatch queue to use for mapping
/// - Returns: Observable with HTTP URL Response and array of target objects
@ -53,13 +55,26 @@ public extension Reactive where Base: DataRequest {
return responseJSONOnQueue(mappingQueue)
.tryMapResult { resp, value in
let jsonArray = try cast(value) as [[String: Any]]
let jsonArray = try cast(value) as [JSON]
return (resp, try Mapper<T>().mapArray(JSONArray: jsonArray))
}
}
/// Method which serializes response into target object
/// Method that serializes response into target type
///
/// - Parameter mappingQueue: The dispatch queue to use for mapping
/// - Returns: Observable with HTTP URL Response and target object
func apiResponse<T>(mappingQueue: DispatchQueue = .global())
-> Observable<(response: HTTPURLResponse, object: T)> {
return responseJSONOnQueue(mappingQueue)
.tryMapResult { resp, value in
return (resp, try cast(value) as T)
}
}
/// Method that serializes response into target object
///
/// - Parameter mappingQueue: The dispatch queue to use for mapping
/// - Returns: Observable with HTTP URL Response and target object
@ -68,14 +83,14 @@ public extension Reactive where Base: DataRequest {
return responseJSONOnQueue(mappingQueue)
.tryMapObservableResult { resp, value in
let json = try cast(value) as [String: Any]
let json = try cast(value) as JSON
return T.createFrom(map: Map(mappingType: .fromJSON, JSON: json))
.map { (resp, $0) }
}
}
/// Method which serializes response into array of target objects
/// Method that serializes response into array of target objects
///
/// - Parameter mappingQueue: The dispatch queue to use for mapping
/// - Returns: Observable with HTTP URL Response and array of target objects
@ -84,7 +99,7 @@ public extension Reactive where Base: DataRequest {
return responseJSONOnQueue(mappingQueue)
.tryMapObservableResult { resp, value in
let jsonArray = try cast(value) as [[String: Any]]
let jsonArray = try cast(value) as [JSON]
let createFromList = jsonArray.map {
T.createFrom(map: Map(mappingType: .fromJSON, JSON: $0))