Alamofire.swift 12 KB

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