Last active
October 24, 2017 10:43
-
-
Save tmspzz/923f1c1de2f00514ed12 to your computer and use it in GitHub Desktop.
Alamofire-GZIP-ParameterEncoding
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
// Actual gzipping from https://github.com/1024jp/NSData-GZIP | |
// Example: ParameterEncoding.JSON.gzipped | |
infix operator • { associativity left } | |
func • <A, B, C>(f: B -> C, g: A -> B) -> A -> C { | |
return { x in f(g(x)) } | |
} | |
extension ParameterEncoding { | |
var gzipped:ParameterEncoding { | |
return gzip(self) | |
} | |
private func gzip(encoding:ParameterEncoding) -> ParameterEncoding { | |
let gzipEncoding = self.gzipOrError • encoding.encode | |
return ParameterEncoding.Custom(gzipEncoding) | |
} | |
private func gzipOrError(request:NSURLRequest, error:NSError?) -> (NSMutableURLRequest, NSError?) { | |
let mutableRequest = request.mutableCopy() as! NSMutableURLRequest | |
if error != nil { | |
return (mutableRequest, error) | |
} | |
var gzipEncodingError: NSError? = nil | |
do { | |
let gzippedData = try mutableRequest.HTTPBody?.gzippedData() | |
mutableRequest.HTTPBody = gzippedData | |
if mutableRequest.HTTPBody != nil { | |
mutableRequest.setValue("gzip", forHTTPHeaderField: "Content-Encoding") | |
} | |
} catch { | |
gzipEncodingError = error as NSError | |
} | |
return (mutableRequest, gzipEncodingError) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you update this to the last version of alamofire?