Created
January 30, 2017 04:03
-
-
Save jshier/f356bfb5b6dc634b67b14ddb75bd7279 to your computer and use it in GitHub Desktop.
Custom Alamofire 4 ResponseSerializer
This file contains hidden or 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
struct CloudFlareResponseSerializer<T: Decodable>: DataResponseSerializerProtocol where T == T.DecodedType { | |
var serializeResponse: (URLRequest?, HTTPURLResponse?, Data?, Error?) -> Result<T> = { (request, response, data, error) in | |
guard error == nil else { return .failure(CloudFlareError.network(error: error!)) } | |
let jsonSerializationResult = DataRequest.jsonResponseSerializer().serializeResponse(request, response, data, nil) | |
guard case let .success(responseJSON) = jsonSerializationResult else { | |
return .failure(CloudFlareError.serialization(error: jsonSerializationResult.error!)) | |
} | |
let decodedCloudFlareResponse = CloudFlareResponse.decode(JSON(responseJSON)) | |
guard case let .success(cloudFlareResponse) = decodedCloudFlareResponse else { | |
return .failure(CloudFlareError.decoding(error: decodedCloudFlareResponse.error!)) | |
} | |
guard cloudFlareResponse.isSuccess else { return .failure(CloudFlareError.response(response: cloudFlareResponse)) } | |
let decodedResponseValue = T.decode(cloudFlareResponse.result) | |
guard case let .success(responseValue) = decodedResponseValue else { | |
return .failure(CloudFlareError.decoding(error: decodedResponseValue.error!)) | |
} | |
return .success(responseValue) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment