Skip to content

Instantly share code, notes, and snippets.

@benbahrenburg
Forked from tmspzz/ParameterEncodingExt.swift
Created February 16, 2016 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benbahrenburg/94a008609c2166f38c8e to your computer and use it in GitHub Desktop.
Save benbahrenburg/94a008609c2166f38c8e to your computer and use it in GitHub Desktop.
Alamofire-GZIP-ParameterEncoding
// 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