Merge pull request #50 from TouchInstinct/feature/mapArray
Feature/map array
This commit is contained in:
commit
d82b51ec67
|
|
@ -1,6 +1,6 @@
|
|||
Pod::Spec.new do |s|
|
||||
s.name = "LeadKit"
|
||||
s.version = "0.4.2"
|
||||
s.version = "0.4.3"
|
||||
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"
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -52,6 +52,19 @@ public extension Reactive where Base: Alamofire.SessionManager {
|
|||
.flatMap { $0.validate().rx.apiResponse(mappingQueue: mappingQueue) }
|
||||
}
|
||||
|
||||
/// Method which 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
|
||||
/// - Returns: Observable with HTTP URL Response and array of target objects
|
||||
func responseModel<T: ImmutableMappable>(requestParameters: ApiRequestParameters,
|
||||
mappingQueue: DispatchQueue = DispatchQueue.global())
|
||||
-> Observable<(response: HTTPURLResponse, models: [T])> {
|
||||
|
||||
return apiRequest(requestParameters: requestParameters)
|
||||
.flatMap { $0.validate().rx.apiResponse(mappingQueue: mappingQueue) }
|
||||
}
|
||||
|
||||
/// Method which executes request and serializes response into target object
|
||||
///
|
||||
/// - Parameter requestParameters: api parameters to pass Alamofire
|
||||
|
|
@ -65,4 +78,17 @@ public extension Reactive where Base: Alamofire.SessionManager {
|
|||
.flatMap { $0.validate().rx.apiResponse(mappingQueue: mappingQueue) }
|
||||
}
|
||||
|
||||
/// Method which 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
|
||||
/// - Returns: Observable with HTTP URL Response and array of target objects
|
||||
func responseObservableModel<T: ObservableMappable>(requestParameters: ApiRequestParameters,
|
||||
mappingQueue: DispatchQueue = DispatchQueue.global())
|
||||
-> Observable<(response: HTTPURLResponse, models: [T])> where T.ModelType == T {
|
||||
|
||||
return apiRequest(requestParameters: requestParameters)
|
||||
.flatMap { $0.validate().rx.apiResponse(mappingQueue: mappingQueue) }
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,6 +42,21 @@ public extension Reactive where Base: DataRequest {
|
|||
}
|
||||
}
|
||||
|
||||
/// Method which 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
|
||||
func apiResponse<T: ImmutableMappable>(mappingQueue: DispatchQueue = DispatchQueue.global())
|
||||
-> Observable<(response: HTTPURLResponse, models: [T])> {
|
||||
|
||||
return responseJSONOnQueue(mappingQueue)
|
||||
.map { resp, value in
|
||||
let jsonArray = try cast(value) as [[String: Any]]
|
||||
|
||||
return (resp, try Mapper<T>().mapArray(JSONArray: jsonArray))
|
||||
}
|
||||
}
|
||||
|
||||
/// Method which serializes response into target object
|
||||
///
|
||||
/// - Parameter mappingQueue: The dispatch queue to use for mapping
|
||||
|
|
@ -58,6 +73,26 @@ public extension Reactive where Base: DataRequest {
|
|||
}
|
||||
}
|
||||
|
||||
/// Method which 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
|
||||
func apiResponse<T: ObservableMappable>(mappingQueue: DispatchQueue = DispatchQueue.global())
|
||||
-> Observable<(response: HTTPURLResponse, models: [T])> where T.ModelType == T {
|
||||
|
||||
return responseJSONOnQueue(mappingQueue)
|
||||
.flatMap { resp, value -> Observable<(response: HTTPURLResponse, models: [T])> in
|
||||
let jsonArray = try cast(value) as [[String: Any]]
|
||||
|
||||
let createFromList = jsonArray.map {
|
||||
T.createFrom(map: Map(mappingType: .fromJSON, JSON: $0))
|
||||
}
|
||||
|
||||
return Observable.zip(createFromList) { $0 }
|
||||
.map { (resp, $0) }
|
||||
}
|
||||
}
|
||||
|
||||
internal func responseJSONOnQueue(_ queue: DispatchQueue) -> Observable<(HTTPURLResponse, Any)> {
|
||||
return responseResult(queue: queue, responseSerializer: DataRequest.jsonResponseSerializer(options: .allowFragments))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue