Skip to content

Instantly share code, notes, and snippets.

@catalinaturlea
Created February 19, 2016 12:04
Show Gist options
  • Select an option

  • Save catalinaturlea/4e962dabe814a1f4e815 to your computer and use it in GitHub Desktop.

Select an option

Save catalinaturlea/4e962dabe814a1f4e815 to your computer and use it in GitHub Desktop.
upload method wrapper
// MARK: MultipartFormData
/**
Creates an upload request using the shared manager instance for the specified method and URL string.
- parameter method: The HTTP method.
- parameter URLString: The URL string.
- parameter headers: The HTTP headers. `nil` by default.
- parameter multipartFormData: The closure used to append body parts to the `MultipartFormData`.
- parameter success: Block to be called in case of successful execution of the request.
- parameter failure: Block to be called in case of errors during the execution of the request.
*/
public class func upload(
method: RequestMethod,
_ URLString: String,
headers: [String: String]? = nil,
multipartFormData: (Void -> [BodyPart]),
success: ((request: NSURLRequest?, response: NSHTTPURLResponse?, json: [NSObject: AnyObject]?) -> ()),
failure: ((request: NSURLRequest?, response: NSHTTPURLResponse?, error: NSError?) -> ())) {
let method = translateMethod(method)
Manager.sharedInstance.upload(method, URLString, headers: headers, multipartFormData: { (formData) -> Void in
let bodyParts = multipartFormData()
for part in bodyParts {
if let fileName = part.fileName, mimeType = part.mimeType {
formData.appendBodyPart(data: part.data, name: part.name, fileName: fileName, mimeType: mimeType)
} else {
formData.appendBodyPart(data: part.data, name: part.name)
}
}
}) { (result) -> Void in
switch result {
case .Success(let uploadRequest, _, _):
uploadRequest.responseData({ (response) -> Void in
parseResponse(response, success: success, failure: failure)
})
case .Failure(_):
failure(request: nil, response: nil, error: NSError(domain: "Encoding error", code: 0, userInfo: nil))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment