Alamofire.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // Alamofire.swift
  2. //
  3. // Copyright (c) 2014–2016 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. // MARK: - URLStringConvertible
  24. /**
  25. Types adopting the `URLStringConvertible` protocol can be used to construct URL strings, which are then used to
  26. construct URL requests.
  27. */
  28. public protocol URLStringConvertible {
  29. /**
  30. A URL that conforms to RFC 2396.
  31. Methods accepting a `URLStringConvertible` type parameter parse it according to RFCs 1738 and 1808.
  32. See https://tools.ietf.org/html/rfc2396
  33. See https://tools.ietf.org/html/rfc1738
  34. See https://tools.ietf.org/html/rfc1808
  35. */
  36. var URLString: String { get }
  37. }
  38. extension String: URLStringConvertible {
  39. public var URLString: String {
  40. return self
  41. }
  42. }
  43. extension NSURL: URLStringConvertible {
  44. public var URLString: String {
  45. return absoluteString
  46. }
  47. }
  48. extension NSURLComponents: URLStringConvertible {
  49. public var URLString: String {
  50. return URL!.URLString
  51. }
  52. }
  53. extension NSURLRequest: URLStringConvertible {
  54. public var URLString: String {
  55. return URL!.URLString
  56. }
  57. }
  58. // MARK: - URLRequestConvertible
  59. /**
  60. Types adopting the `URLRequestConvertible` protocol can be used to construct URL requests.
  61. */
  62. public protocol URLRequestConvertible {
  63. /// The URL request.
  64. var URLRequest: NSMutableURLRequest { get }
  65. }
  66. extension NSURLRequest: URLRequestConvertible {
  67. public var URLRequest: NSMutableURLRequest {
  68. return self.mutableCopy() as! NSMutableURLRequest
  69. }
  70. }
  71. // MARK: - Convenience
  72. func URLRequest(
  73. method: Method,
  74. _ URLString: URLStringConvertible,
  75. headers: [String: String]? = nil)
  76. -> NSMutableURLRequest
  77. {
  78. let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: URLString.URLString)!)
  79. mutableURLRequest.HTTPMethod = method.rawValue
  80. if let headers = headers {
  81. for (headerField, headerValue) in headers {
  82. mutableURLRequest.setValue(headerValue, forHTTPHeaderField: headerField)
  83. }
  84. }
  85. return mutableURLRequest
  86. }
  87. // MARK: - Request Methods
  88. /**
  89. Creates a request using the shared manager instance for the specified method, URL string, parameters, and
  90. parameter encoding.
  91. - parameter method: The HTTP method.
  92. - parameter URLString: The URL string.
  93. - parameter parameters: The parameters. `nil` by default.
  94. - parameter encoding: The parameter encoding. `.URL` by default.
  95. - parameter headers: The HTTP headers. `nil` by default.
  96. - returns: The created request.
  97. */
  98. public func request(
  99. method: Method,
  100. _ URLString: URLStringConvertible,
  101. parameters: [String: AnyObject]? = nil,
  102. encoding: ParameterEncoding = .URL,
  103. headers: [String: String]? = nil)
  104. -> Request
  105. {
  106. return Manager.sharedInstance.request(
  107. method,
  108. URLString,
  109. parameters: parameters,
  110. encoding: encoding,
  111. headers: headers
  112. )
  113. }
  114. /**
  115. Creates a request using the shared manager instance for the specified URL request.
  116. If `startRequestsImmediately` is `true`, the request will have `resume()` called before being returned.
  117. - parameter URLRequest: The URL request
  118. - returns: The created request.
  119. */
  120. public func request(URLRequest: URLRequestConvertible) -> Request {
  121. return Manager.sharedInstance.request(URLRequest.URLRequest)
  122. }
  123. // MARK: - Upload Methods
  124. // MARK: File
  125. /**
  126. Creates an upload request using the shared manager instance for the specified method, URL string, and file.
  127. - parameter method: The HTTP method.
  128. - parameter URLString: The URL string.
  129. - parameter headers: The HTTP headers. `nil` by default.
  130. - parameter file: The file to upload.
  131. - returns: The created upload request.
  132. */
  133. public func upload(
  134. method: Method,
  135. _ URLString: URLStringConvertible,
  136. headers: [String: String]? = nil,
  137. file: NSURL)
  138. -> Request
  139. {
  140. return Manager.sharedInstance.upload(method, URLString, headers: headers, file: file)
  141. }
  142. /**
  143. Creates an upload request using the shared manager instance for the specified URL request and file.
  144. - parameter URLRequest: The URL request.
  145. - parameter file: The file to upload.
  146. - returns: The created upload request.
  147. */
  148. public func upload(URLRequest: URLRequestConvertible, file: NSURL) -> Request {
  149. return Manager.sharedInstance.upload(URLRequest, file: file)
  150. }
  151. // MARK: Data
  152. /**
  153. Creates an upload request using the shared manager instance for the specified method, URL string, and data.
  154. - parameter method: The HTTP method.
  155. - parameter URLString: The URL string.
  156. - parameter headers: The HTTP headers. `nil` by default.
  157. - parameter data: The data to upload.
  158. - returns: The created upload request.
  159. */
  160. public func upload(
  161. method: Method,
  162. _ URLString: URLStringConvertible,
  163. headers: [String: String]? = nil,
  164. data: NSData)
  165. -> Request
  166. {
  167. return Manager.sharedInstance.upload(method, URLString, headers: headers, data: data)
  168. }
  169. /**
  170. Creates an upload request using the shared manager instance for the specified URL request and data.
  171. - parameter URLRequest: The URL request.
  172. - parameter data: The data to upload.
  173. - returns: The created upload request.
  174. */
  175. public func upload(URLRequest: URLRequestConvertible, data: NSData) -> Request {
  176. return Manager.sharedInstance.upload(URLRequest, data: data)
  177. }
  178. // MARK: Stream
  179. /**
  180. Creates an upload request using the shared manager instance for the specified method, URL string, and stream.
  181. - parameter method: The HTTP method.
  182. - parameter URLString: The URL string.
  183. - parameter headers: The HTTP headers. `nil` by default.
  184. - parameter stream: The stream to upload.
  185. - returns: The created upload request.
  186. */
  187. public func upload(
  188. method: Method,
  189. _ URLString: URLStringConvertible,
  190. headers: [String: String]? = nil,
  191. stream: NSInputStream)
  192. -> Request
  193. {
  194. return Manager.sharedInstance.upload(method, URLString, headers: headers, stream: stream)
  195. }
  196. /**
  197. Creates an upload request using the shared manager instance for the specified URL request and stream.
  198. - parameter URLRequest: The URL request.
  199. - parameter stream: The stream to upload.
  200. - returns: The created upload request.
  201. */
  202. public func upload(URLRequest: URLRequestConvertible, stream: NSInputStream) -> Request {
  203. return Manager.sharedInstance.upload(URLRequest, stream: stream)
  204. }
  205. // MARK: MultipartFormData
  206. /**
  207. Creates an upload request using the shared manager instance for the specified method and URL string.
  208. - parameter method: The HTTP method.
  209. - parameter URLString: The URL string.
  210. - parameter headers: The HTTP headers. `nil` by default.
  211. - parameter multipartFormData: The closure used to append body parts to the `MultipartFormData`.
  212. - parameter encodingMemoryThreshold: The encoding memory threshold in bytes.
  213. `MultipartFormDataEncodingMemoryThreshold` by default.
  214. - parameter encodingCompletion: The closure called when the `MultipartFormData` encoding is complete.
  215. */
  216. public func upload(
  217. method: Method,
  218. _ URLString: URLStringConvertible,
  219. headers: [String: String]? = nil,
  220. multipartFormData: MultipartFormData -> Void,
  221. encodingMemoryThreshold: UInt64 = Manager.MultipartFormDataEncodingMemoryThreshold,
  222. encodingCompletion: (Manager.MultipartFormDataEncodingResult -> Void)?)
  223. {
  224. return Manager.sharedInstance.upload(
  225. method,
  226. URLString,
  227. headers: headers,
  228. multipartFormData: multipartFormData,
  229. encodingMemoryThreshold: encodingMemoryThreshold,
  230. encodingCompletion: encodingCompletion
  231. )
  232. }
  233. /**
  234. Creates an upload request using the shared manager instance for the specified method and URL string.
  235. - parameter URLRequest: The URL request.
  236. - parameter multipartFormData: The closure used to append body parts to the `MultipartFormData`.
  237. - parameter encodingMemoryThreshold: The encoding memory threshold in bytes.
  238. `MultipartFormDataEncodingMemoryThreshold` by default.
  239. - parameter encodingCompletion: The closure called when the `MultipartFormData` encoding is complete.
  240. */
  241. public func upload(
  242. URLRequest: URLRequestConvertible,
  243. multipartFormData: MultipartFormData -> Void,
  244. encodingMemoryThreshold: UInt64 = Manager.MultipartFormDataEncodingMemoryThreshold,
  245. encodingCompletion: (Manager.MultipartFormDataEncodingResult -> Void)?)
  246. {
  247. return Manager.sharedInstance.upload(
  248. URLRequest,
  249. multipartFormData: multipartFormData,
  250. encodingMemoryThreshold: encodingMemoryThreshold,
  251. encodingCompletion: encodingCompletion
  252. )
  253. }
  254. // MARK: - Download Methods
  255. // MARK: URL Request
  256. /**
  257. Creates a download request using the shared manager instance for the specified method and URL string.
  258. - parameter method: The HTTP method.
  259. - parameter URLString: The URL string.
  260. - parameter parameters: The parameters. `nil` by default.
  261. - parameter encoding: The parameter encoding. `.URL` by default.
  262. - parameter headers: The HTTP headers. `nil` by default.
  263. - parameter destination: The closure used to determine the destination of the downloaded file.
  264. - returns: The created download request.
  265. */
  266. public func download(
  267. method: Method,
  268. _ URLString: URLStringConvertible,
  269. parameters: [String: AnyObject]? = nil,
  270. encoding: ParameterEncoding = .URL,
  271. headers: [String: String]? = nil,
  272. destination: Request.DownloadFileDestination)
  273. -> Request
  274. {
  275. return Manager.sharedInstance.download(
  276. method,
  277. URLString,
  278. parameters: parameters,
  279. encoding: encoding,
  280. headers: headers,
  281. destination: destination
  282. )
  283. }
  284. /**
  285. Creates a download request using the shared manager instance for the specified URL request.
  286. - parameter URLRequest: The URL request.
  287. - parameter destination: The closure used to determine the destination of the downloaded file.
  288. - returns: The created download request.
  289. */
  290. public func download(URLRequest: URLRequestConvertible, destination: Request.DownloadFileDestination) -> Request {
  291. return Manager.sharedInstance.download(URLRequest, destination: destination)
  292. }
  293. // MARK: Resume Data
  294. /**
  295. Creates a request using the shared manager instance for downloading from the resume data produced from a
  296. previous request cancellation.
  297. - parameter resumeData: The resume data. This is an opaque data blob produced by `NSURLSessionDownloadTask`
  298. when a task is cancelled. See `NSURLSession -downloadTaskWithResumeData:` for additional
  299. information.
  300. - parameter destination: The closure used to determine the destination of the downloaded file.
  301. - returns: The created download request.
  302. */
  303. public func download(resumeData data: NSData, destination: Request.DownloadFileDestination) -> Request {
  304. return Manager.sharedInstance.download(data, destination: destination)
  305. }