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
typealias UserCompletionBlock = (user: User?, error: NSError?) -> () | |
func currentUser(withCompletionBlock completionBlock: UserCompletionBlock) { | |
Alamofire.request(Router.CurrentUser) | |
.responseObject(typeOfError: ExampleAPIError.self) { (response: Response<User, NSError>) in | |
switch response.result { | |
case .Success(let user): | |
completionBlock(user: user, error: nil) | |
case .Failure(let error): | |
completionBlock(user: nil, error: error) | |
} |
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
import Foundation | |
import Alamofire | |
import ObjectMapper | |
let DefaultErrorMessage = "ObjectMapper failed to serialize response." | |
enum ExampleErrorCode: Int { | |
case DataSerializationError = -6004 | |
case EndpointError = 404 | |
} |
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
switch response.statusCode { | |
case 200..<400: | |
break | |
default: | |
if let errorParsed = Mapper<E>().map(JSONToMap) { | |
let failureReason = errorParsed.message | |
let error = newError(.EndpointError, failureReason: | |
failureReason ?? DefaultErrorMessage) | |
return .Failure(error) | |
} |
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
public static func ObjectMapperSerializer<T: Mappable, E: APIError>(keyPath: String?, mapToObject object: T? = nil, typeOfError errorType: E.Type) -> 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 = newError(.DataSerializationError, failureReason: failureReason) | |
return .Failure(error) |
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
import Foundation | |
import ObjectMapper | |
public protocol APIError: Mappable { | |
var type: String? { get set } | |
var message: String? { get set } | |
} | |
public class ExampleAPIError: APIError { | |
public var type: String? |
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
{ | |
"error": { | |
"type": "endpoint_error", | |
"message": "Email not invited" | |
} | |
} |
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
public static func ObjectMapperSerializer<T: Mappable>(keyPath: String?, mapToObject object: T? = nil, context: MapContext? = nil) -> 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 = newError(.DataSerializationFailed, failureReason: failureReason) | |
return .Failure(error) |
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
let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/2ee8f34d21e8febfdefb2b3a403f18a43818d70a/sample_keypath_json" | |
let expectation = expectationWithDescription("\(URL)") | |
Alamofire.request(.GET, URL).responseObject(keyPath: "data") { (response: Response<WeatherResponse, NSError>) in | |
expectation.fulfill() | |
let weatherResponse = response.result.value | |
print(weatherResponse?.location) | |
if let threeDayForecast = weatherResponse?.threeDayForecast { |
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
DispatchQueue.global().async { | |
// Background thread | |
DispatchQueue.main.async { | |
// UI Updates | |
} | |
} |
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
import UIKit | |
import Shimmer | |
class PlaceholderCell: UITableViewCell { | |
@IBOutlet var nameShimmerView: FBShimmeringView! | |
@IBOutlet var emailShimmerView: FBShimmeringView! | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
NewerOlder