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