Created
January 16, 2016 04:06
-
-
Save jasminsuljic/916d1f3e7e3a5d4bf5ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Extension.swift | |
// | |
// Created by Jasmin Suljic on 11/01/16. | |
// Copyright © 2016 Leftor d.o.o. All rights reserved. | |
// | |
import Foundation | |
import RxSwift | |
import RxBlocking | |
import ObjectMapper | |
import Alamofire | |
import AlamofireObjectMapper | |
extension Request{ | |
public static func GenericResponseSerializer<T:Mappable>( | |
keyPath: String? = nil, | |
options: NSJSONReadingOptions = .AllowFragments | |
) | |
-> ResponseSerializer<T, NSError> | |
{ | |
return ResponseSerializer { request, response, data, error in | |
guard error == nil else { | |
return .Failure(error!) | |
} | |
guard let _ = data else { | |
let failureReason = "Data could not be serialized. Input data was nil." | |
let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason) | |
return .Failure(error) | |
} | |
let JSONResponseSerializer = Request.JSONResponseSerializer(options: .AllowFragments) | |
let result = JSONResponseSerializer.serializeResponse(request, response, data, error) | |
let JSONToMap: AnyObject? | |
if let keyPath = keyPath where keyPath.isEmpty == false { | |
JSONToMap = result.value?.valueForKeyPath(keyPath) | |
} else { | |
JSONToMap = result.value | |
} | |
if let parsedObject = Mapper<T>().map(JSONToMap){ | |
return .Success(parsedObject) | |
} | |
let failureReason = "ObjectMapper failed to serialize response." | |
let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason) | |
return .Failure(error) | |
} | |
} | |
} | |
/** | |
Creates an observable of the returned decoded JSON. | |
- parameter method: Alamofire method object | |
- parameter URLString: An object adopting `URLStringConvertible` | |
- parameter parameters: A dictionary containing all necessary options | |
- parameter encoding: The kind of encoding used to process parameters | |
- parameter header: A dictionary containing all the addional headers | |
- returns: An observable of the decoded JSON as `T` | |
*/ | |
public func JSON<T:Mappable>(method: Alamofire.Method, | |
_ URLString: URLStringConvertible, | |
parameters: [String: AnyObject]? = nil, | |
encoding: ParameterEncoding = .URL, | |
headers: [String: String]? = nil) | |
-> Observable<T> | |
{ | |
return Manager.sharedInstance.rx_object( | |
method, | |
URLString, | |
parameters: parameters, | |
encoding: encoding, | |
headers: headers | |
) | |
} | |
extension Manager{ | |
/** | |
Creates an observable of the data decoded from JSON and processed as `T:Mappable`. | |
- parameter URLRequest: An object adopting `URLRequestConvertible` | |
- parameter mapTo: An Type from T | |
- parameter parameters: A dictionary containing all necessary options | |
- parameter encoding: The kind of encoding used to process parameters | |
- parameter header: A dictionary containing all the addional headers | |
- returns: An observable of `T` | |
*/ | |
public func rx_object<T:Mappable>(method: Alamofire.Method, | |
_ URLString: URLStringConvertible, | |
parameters: [String: AnyObject]? = nil, | |
encoding: ParameterEncoding = .URL, | |
headers: [String: String]? = nil | |
) | |
-> Observable<T> | |
{ | |
return rx_request( | |
method, | |
URLString, | |
parameters: parameters, | |
encoding: encoding, | |
headers: headers | |
).flatMap { $0.rx_object() } | |
} | |
} | |
extension Request{ | |
/** | |
Returns an `Observable` of a serialized JSON for the current request. | |
- parameter options: Reading options for JSON decoding process, **default:** `.AllowFragments` | |
- returns: An instance of `Observable<T>` | |
*/ | |
public func rx_object<T:Mappable>(options: NSJSONReadingOptions = .AllowFragments) -> Observable<T> { | |
return rx_result(responseSerializer: Request.GenericResponseSerializer(options: options)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage example: https://gist.github.com/jasminsuljic/56a56c980d390802a144