Alamofire.swift 12 KB

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