Alamofire.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // Alamofire.swift
  2. //
  3. // Copyright (c) 2014–2015 Alamofire Software Foundation (http://alamofire.org/)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import Foundation
  23. /// Alamofire errors
  24. public let AlamofireErrorDomain = "com.alamofire.error"
  25. public let AlamofireInputStreamReadFailed = -6000
  26. public let AlamofireOutputStreamWriteFailed = -6001
  27. // MARK: - URLStringConvertible
  28. /**
  29. Types adopting the `URLStringConvertible` protocol can be used to construct URL strings, which are then used to construct URL requests.
  30. */
  31. public protocol URLStringConvertible {
  32. /**
  33. A URL that conforms to RFC 2396.
  34. Methods accepting a `URLStringConvertible` type parameter parse it according to RFCs 1738 and 1808.
  35. See http://tools.ietf.org/html/rfc2396
  36. See http://tools.ietf.org/html/rfc1738
  37. See http://tools.ietf.org/html/rfc1808
  38. */
  39. var URLString: String { get }
  40. }
  41. extension String: URLStringConvertible {
  42. public var URLString: String {
  43. return self
  44. }
  45. }
  46. extension NSURL: URLStringConvertible {
  47. public var URLString: String {
  48. return absoluteString!
  49. }
  50. }
  51. extension NSURLComponents: URLStringConvertible {
  52. public var URLString: String {
  53. return URL!.URLString
  54. }
  55. }
  56. extension NSURLRequest: URLStringConvertible {
  57. public var URLString: String {
  58. return URL!.URLString
  59. }
  60. }
  61. // MARK: - URLRequestConvertible
  62. /**
  63. Types adopting the `URLRequestConvertible` protocol can be used to construct URL requests.
  64. */
  65. public protocol URLRequestConvertible {
  66. /// The URL request.
  67. var URLRequest: NSURLRequest { get }
  68. }
  69. extension NSURLRequest: URLRequestConvertible {
  70. public var URLRequest: NSURLRequest {
  71. return self
  72. }
  73. }
  74. // MARK: - Convenience
  75. func URLRequest(method: Method, URLString: URLStringConvertible, headers: [String: String]? = nil) -> NSMutableURLRequest {
  76. let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: URLString.URLString)!)
  77. mutableURLRequest.HTTPMethod = method.rawValue
  78. if let headers = headers {
  79. for (headerField, headerValue) in headers {
  80. mutableURLRequest.setValue(headerValue, forHTTPHeaderField: headerField)
  81. }
  82. }
  83. return mutableURLRequest
  84. }
  85. // MARK: - Request Methods
  86. /**
  87. Creates a request using the shared manager instance for the specified method, URL string, parameters, and parameter encoding.
  88. :param: method The HTTP method.
  89. :param: URLString The URL string.
  90. :param: parameters The parameters. `nil` by default.
  91. :param: encoding The parameter encoding. `.URL` by default.
  92. :param: headers The HTTP headers. `nil` by default.
  93. :returns: The created request.
  94. */
  95. public func request(method: Method, URLString: URLStringConvertible, parameters: [String: AnyObject]? = nil, encoding: ParameterEncoding = .URL, headers: [String: String]? = nil) -> Request {
  96. return Manager.sharedInstance.request(method, URLString, parameters: parameters, encoding: encoding, headers: headers)
  97. }
  98. /**
  99. Creates a request using the shared manager instance for the specified URL request.
  100. If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  101. :param: URLRequest The URL request
  102. :returns: The created request.
  103. */
  104. public func request(URLRequest: URLRequestConvertible) -> Request {
  105. return Manager.sharedInstance.request(URLRequest.URLRequest)
  106. }
  107. // MARK: - Upload Methods
  108. // MARK: File
  109. /**
  110. Creates an upload request using the shared manager instance for the specified method, URL string, and file.
  111. :param: method The HTTP method.
  112. :param: URLString The URL string.
  113. :param: headers The HTTP headers. `nil` by default.
  114. :param: file The file to upload.
  115. :returns: The created upload request.
  116. */
  117. public func upload(method: Method, URLString: URLStringConvertible, headers: [String: String]? = nil, #file: NSURL) -> Request {
  118. return Manager.sharedInstance.upload(method, URLString, headers: headers, file: file)
  119. }
  120. /**
  121. Creates an upload request using the shared manager instance for the specified URL request and file.
  122. :param: URLRequest The URL request.
  123. :param: file The file to upload.
  124. :returns: The created upload request.
  125. */
  126. public func upload(URLRequest: URLRequestConvertible, #file: NSURL) -> Request {
  127. return Manager.sharedInstance.upload(URLRequest, file: file)
  128. }
  129. // MARK: Data
  130. /**
  131. Creates an upload request using the shared manager instance for the specified method, URL string, and data.
  132. :param: method The HTTP method.
  133. :param: URLString The URL string.
  134. :param: headers The HTTP headers. `nil` by default.
  135. :param: data The data to upload.
  136. :returns: The created upload request.
  137. */
  138. public func upload(method: Method, URLString: URLStringConvertible, headers: [String: String]? = nil, #data: NSData) -> Request {
  139. return Manager.sharedInstance.upload(method, URLString, headers: headers, data: data)
  140. }
  141. /**
  142. Creates an upload request using the shared manager instance for the specified URL request and data.
  143. :param: URLRequest The URL request.
  144. :param: data The data to upload.
  145. :returns: The created upload request.
  146. */
  147. public func upload(URLRequest: URLRequestConvertible, #data: NSData) -> Request {
  148. return Manager.sharedInstance.upload(URLRequest, data: data)
  149. }
  150. // MARK: Stream
  151. /**
  152. Creates an upload request using the shared manager instance for the specified method, URL string, and stream.
  153. :param: method The HTTP method.
  154. :param: URLString The URL string.
  155. :param: headers The HTTP headers. `nil` by default.
  156. :param: stream The stream to upload.
  157. :returns: The created upload request.
  158. */
  159. public func upload(method: Method, URLString: URLStringConvertible, headers: [String: String]? = nil, #stream: NSInputStream) -> Request {
  160. return Manager.sharedInstance.upload(method, URLString, headers: headers, stream: stream)
  161. }
  162. /**
  163. Creates an upload request using the shared manager instance for the specified URL request and stream.
  164. :param: URLRequest The URL request.
  165. :param: stream The stream to upload.
  166. :returns: The created upload request.
  167. */
  168. public func upload(URLRequest: URLRequestConvertible, #stream: NSInputStream) -> Request {
  169. return Manager.sharedInstance.upload(URLRequest, stream: stream)
  170. }
  171. // MARK: MultipartFormData
  172. /**
  173. Creates an upload request using the shared manager instance for the specified method and URL string.
  174. :param: method The HTTP method.
  175. :param: URLString The URL string.
  176. :param: headers The HTTP headers. `nil` by default.
  177. :param: multipartFormData The closure used to append body parts to the `MultipartFormData`.
  178. :param: encodingMemoryThreshold The encoding memory threshold in bytes. `MultipartFormDataEncodingMemoryThreshold`
  179. by default.
  180. :param: encodingCompletion The closure called when the `MultipartFormData` encoding is complete.
  181. */
  182. public func upload(
  183. method: Method,
  184. #URLString: URLStringConvertible,
  185. headers: [String: String]? = nil,
  186. #multipartFormData: MultipartFormData -> Void,
  187. encodingMemoryThreshold: UInt64 = Manager.MultipartFormDataEncodingMemoryThreshold,
  188. #encodingCompletion: (Manager.MultipartFormDataEncodingResult -> Void)?)
  189. {
  190. return Manager.sharedInstance.upload(
  191. method,
  192. URLString,
  193. headers: headers,
  194. multipartFormData: multipartFormData,
  195. encodingMemoryThreshold: encodingMemoryThreshold,
  196. encodingCompletion: encodingCompletion
  197. )
  198. }
  199. /**
  200. Creates an upload request using the shared manager instance for the specified method and URL string.
  201. :param: URLRequest The URL request.
  202. :param: multipartFormData The closure used to append body parts to the `MultipartFormData`.
  203. :param: encodingMemoryThreshold The encoding memory threshold in bytes. `MultipartFormDataEncodingMemoryThreshold`
  204. by default.
  205. :param: encodingCompletion The closure called when the `MultipartFormData` encoding is complete.
  206. */
  207. public func upload(
  208. URLRequest: URLRequestConvertible,
  209. #multipartFormData: MultipartFormData -> Void,
  210. encodingMemoryThreshold: UInt64 = Manager.MultipartFormDataEncodingMemoryThreshold,
  211. #encodingCompletion: (Manager.MultipartFormDataEncodingResult -> Void)?)
  212. {
  213. return Manager.sharedInstance.upload(
  214. URLRequest,
  215. multipartFormData: multipartFormData,
  216. encodingMemoryThreshold: encodingMemoryThreshold,
  217. encodingCompletion: encodingCompletion
  218. )
  219. }
  220. // MARK: - Download Methods
  221. // MARK: URL Request
  222. /**
  223. Creates a download request using the shared manager instance for the specified method and URL string.
  224. :param: method The HTTP method.
  225. :param: URLString The URL string.
  226. :param: headers The HTTP headers. `nil` by default.
  227. :param: destination The closure used to determine the destination of the downloaded file.
  228. :returns: The created download request.
  229. */
  230. public func download(method: Method, URLString: URLStringConvertible, headers: [String: String]? = nil, #destination: Request.DownloadFileDestination) -> Request {
  231. return Manager.sharedInstance.download(method, URLString, headers: headers, destination: destination)
  232. }
  233. /**
  234. Creates a download request using the shared manager instance for the specified URL request.
  235. :param: URLRequest The URL request.
  236. :param: destination The closure used to determine the destination of the downloaded file.
  237. :returns: The created download request.
  238. */
  239. public func download(URLRequest: URLRequestConvertible, #destination: Request.DownloadFileDestination) -> Request {
  240. return Manager.sharedInstance.download(URLRequest, destination: destination)
  241. }
  242. // MARK: Resume Data
  243. /**
  244. Creates a request using the shared manager instance for downloading from the resume data produced from a previous request cancellation.
  245. :param: resumeData The resume data. This is an opaque data blob produced by `NSURLSessionDownloadTask` when a task is cancelled. See `NSURLSession -downloadTaskWithResumeData:` for additional information.
  246. :param: destination The closure used to determine the destination of the downloaded file.
  247. :returns: The created download request.
  248. */
  249. public func download(resumeData data: NSData, #destination: Request.DownloadFileDestination) -> Request {
  250. return Manager.sharedInstance.download(data, destination: destination)
  251. }