Alamofire.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. //
  2. // Alamofire.swift
  3. //
  4. // Copyright (c) 2014-2018 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. /// Global namespace containing API for the `default` `Session` instance.
  26. public enum AF {
  27. // MARK: - Data Request
  28. /// Creates a `DataRequest` using `SessionManager.default` to retrive the contents of the specified `url`
  29. /// using the `method`, `parameters`, `encoding`, and `headers` provided.
  30. ///
  31. /// - Parameters:
  32. /// - url: The `URLConvertible` value.
  33. /// - method: The `HTTPMethod`, `.get` by default.
  34. /// - parameters: The `Parameters`, `nil` by default.
  35. /// - encoding: The `ParameterEncoding`, `URLEncoding.default` by default.
  36. /// - headers: The `HTTPHeaders`, `nil` by default.
  37. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  38. ///
  39. /// - Returns: The created `DataRequest`.
  40. public static func request(_ url: URLConvertible,
  41. method: HTTPMethod = .get,
  42. parameters: Parameters? = nil,
  43. encoding: ParameterEncoding = URLEncoding.default,
  44. headers: HTTPHeaders? = nil,
  45. interceptor: RequestInterceptor? = nil) -> DataRequest {
  46. return Session.default.request(url,
  47. method: method,
  48. parameters: parameters,
  49. encoding: encoding,
  50. headers: headers,
  51. interceptor: interceptor)
  52. }
  53. /// Creates a `DataRequest` using `SessionManager.default` to retrive the contents of the specified `url`
  54. /// using the `method`, `parameters`, `encoding`, and `headers` provided.
  55. ///
  56. /// - Parameters:
  57. /// - url: The `URLConvertible` value.
  58. /// - method: The `HTTPMethod`, `.get` by default.
  59. /// - parameters: The `Encodable` parameters, `nil` by default.
  60. /// - encoding: The `ParameterEncoding`, `URLEncodedFormParameterEncoder.default` by default.
  61. /// - headers: The `HTTPHeaders`, `nil` by default.
  62. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  63. ///
  64. /// - Returns: The created `DataRequest`.
  65. public static func request<Parameters: Encodable>(_ url: URLConvertible,
  66. method: HTTPMethod = .get,
  67. parameters: Parameters? = nil,
  68. encoder: ParameterEncoder = URLEncodedFormParameterEncoder.default,
  69. headers: HTTPHeaders? = nil,
  70. interceptor: RequestInterceptor? = nil) -> DataRequest {
  71. return Session.default.request(url,
  72. method: method,
  73. parameters: parameters,
  74. encoder: encoder,
  75. headers: headers,
  76. interceptor: interceptor)
  77. }
  78. /// Creates a `DataRequest` using `SessionManager.default` to execute the specified `urlRequest`.
  79. ///
  80. /// - Parameters:
  81. /// - urlRequest: The `URLRequestConvertible` value.
  82. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  83. ///
  84. /// - Returns: The created `DataRequest`.
  85. public static func request(_ urlRequest: URLRequestConvertible, interceptor: RequestInterceptor? = nil) -> DataRequest {
  86. return Session.default.request(urlRequest, interceptor: interceptor)
  87. }
  88. // MARK: - Download Request
  89. /// Creates a `DownloadRequest` using `SessionManager.default` to download the contents of the specified `url` to
  90. /// the provided `destination` using the `method`, `parameters`, `encoding`, and `headers` provided.
  91. ///
  92. /// If `destination` is not specified, the download will remain at the temporary location determined by the
  93. /// underlying `URLSession`.
  94. ///
  95. /// - Parameters:
  96. /// - url: The `URLConvertible` value.
  97. /// - method: The `HTTPMethod`, `.get` by default.
  98. /// - parameters: The `Parameters`, `nil` by default.
  99. /// - encoding: The `ParameterEncoding`, `URLEncoding.default` by default.
  100. /// - headers: The `HTTPHeaders`, `nil` by default.
  101. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  102. /// - destination: The `DownloadRequest.Destination` closure used the determine the destination of the
  103. /// downloaded file. `nil` by default.
  104. ///
  105. /// - Returns: The created `DownloadRequest`.
  106. public static func download(_ url: URLConvertible,
  107. method: HTTPMethod = .get,
  108. parameters: Parameters? = nil,
  109. encoding: ParameterEncoding = URLEncoding.default,
  110. headers: HTTPHeaders? = nil,
  111. interceptor: RequestInterceptor? = nil,
  112. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  113. return Session.default.download(url,
  114. method: method,
  115. parameters: parameters,
  116. encoding: encoding,
  117. headers: headers,
  118. interceptor: interceptor,
  119. to: destination)
  120. }
  121. /// Creates a `DownloadRequest` using `SessionManager.default` to download the contents of the specified `url` to
  122. /// the provided `destination` using the `method`, encodable `parameters`, `encoder`, and `headers` provided.
  123. ///
  124. /// If `destination` is not specified, the download will remain at the temporary location determined by the
  125. /// underlying `URLSession`.
  126. ///
  127. /// - Parameters:
  128. /// - url: The `URLConvertible` value.
  129. /// - method: The `HTTPMethod`, `.get` by default.
  130. /// - parameters: The `Encodable` parameters, `nil` by default.
  131. /// - encoder: The `ParameterEncoder`, `URLEncodedFormParameterEncoder.default` by default.
  132. /// - headers: The `HTTPHeaders`, `nil` by default.
  133. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  134. /// - destination: The `DownloadRequest.Destination` closure used the determine the destination of the
  135. /// downloaded file. `nil` by default.
  136. ///
  137. /// - Returns: The created `DownloadRequest`.
  138. public static func download<Parameters: Encodable>(_ url: URLConvertible,
  139. method: HTTPMethod = .get,
  140. parameters: Parameters? = nil,
  141. encoder: ParameterEncoder = URLEncodedFormParameterEncoder.default,
  142. headers: HTTPHeaders? = nil,
  143. interceptor: RequestInterceptor? = nil,
  144. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  145. return Session.default.download(url,
  146. method: method,
  147. parameters: parameters,
  148. encoder: encoder,
  149. headers: headers,
  150. interceptor: interceptor,
  151. to: destination)
  152. }
  153. // MARK: URLRequest
  154. /// Creates a `DownloadRequest` using `SessionManager.default` to execute the specified `urlRequest` and download
  155. /// the result to the provided `destination`.
  156. ///
  157. /// - Parameters:
  158. /// - urlRequest: The `URLRequestConvertible` value.
  159. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  160. /// - destination: The `DownloadRequest.Destination` closure used the determine the destination of the
  161. /// downloaded file. `nil` by default.
  162. ///
  163. /// - Returns: The created `DownloadRequest`.
  164. public static func download(_ urlRequest: URLRequestConvertible,
  165. interceptor: RequestInterceptor? = nil,
  166. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  167. return Session.default.download(urlRequest, interceptor: interceptor, to: destination)
  168. }
  169. // MARK: Resume Data
  170. /// Creates a `DownloadRequest` using the `SessionManager.default` from the `resumeData` produced from a previous
  171. /// `DownloadRequest` cancellation to retrieve the contents of the original request and save them to the `destination`.
  172. ///
  173. /// If `destination` is not specified, the contents will remain in the temporary location determined by the
  174. /// underlying URL session.
  175. ///
  176. /// On some versions of all Apple platforms (iOS 10 - 10.2, macOS 10.12 - 10.12.2, tvOS 10 - 10.1, watchOS 3 - 3.1.1),
  177. /// `resumeData` is broken on background URL session configurations. There's an underlying bug in the `resumeData`
  178. /// generation logic where the data is written incorrectly and will always fail to resume the download. For more
  179. /// information about the bug and possible workarounds, please refer to the [this Stack Overflow post](http://stackoverflow.com/a/39347461/1342462).
  180. ///
  181. /// - Parameters:
  182. /// - resumeData: The resume `Data`. This is an opaque blob produced by `URLSessionDownloadTask` when a task is
  183. /// cancelled. See [Apple's documentation](https://developer.apple.com/documentation/foundation/urlsessiondownloadtask/1411634-cancel)
  184. /// for more information.
  185. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  186. /// - destination: The `DownloadRequest.Destination` closure used to determine the destination of the downloaded
  187. /// file. `nil` by default.
  188. ///
  189. /// - Returns: The created `DownloadRequest`.
  190. public static func download(resumingWith resumeData: Data,
  191. interceptor: RequestInterceptor? = nil,
  192. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  193. return Session.default.download(resumingWith: resumeData, interceptor: interceptor, to: destination)
  194. }
  195. // MARK: - Upload Request
  196. // MARK: File
  197. /// Creates an `UploadRequest` using `SessionManager.default` to upload the contents of the `fileURL` specified
  198. /// using the `url`, `method` and `headers` provided.
  199. ///
  200. /// - Parameters:
  201. /// - fileURL: The `URL` of the file to upload.
  202. /// - url: The `URLConvertible` value.
  203. /// - method: The `HTTPMethod`, `.post` by default.
  204. /// - headers: The `HTTPHeaders`, `nil` by default.
  205. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  206. ///
  207. /// - Returns: The created `UploadRequest`.
  208. public static func upload(_ fileURL: URL,
  209. to url: URLConvertible,
  210. method: HTTPMethod = .post,
  211. headers: HTTPHeaders? = nil,
  212. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  213. return Session.default.upload(fileURL, to: url, method: method, headers: headers, interceptor: interceptor)
  214. }
  215. /// Creates an `UploadRequest` using the `SessionManager.default` to upload the contents of the `fileURL` specificed
  216. /// using the `urlRequest` provided.
  217. ///
  218. /// - Parameters:
  219. /// - fileURL: The `URL` of the file to upload.
  220. /// - urlRequest: The `URLRequestConvertible` value.
  221. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  222. ///
  223. /// - Returns: The created `UploadRequest`.
  224. public static func upload(_ fileURL: URL,
  225. with urlRequest: URLRequestConvertible,
  226. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  227. return Session.default.upload(fileURL, with: urlRequest, interceptor: interceptor)
  228. }
  229. // MARK: Data
  230. /// Creates an `UploadRequest` using `SessionManager.default` to upload the contents of the `data` specified using
  231. /// the `url`, `method` and `headers` provided.
  232. ///
  233. /// - Parameters:
  234. /// - data: The `Data` to upload.
  235. /// - url: The `URLConvertible` value.
  236. /// - method: The `HTTPMethod`, `.post` by default.
  237. /// - headers: The `HTTPHeaders`, `nil` by default.
  238. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  239. /// - retryPolicies: The `RetryPolicy` types, `[]` by default.
  240. ///
  241. /// - Returns: The created `UploadRequest`.
  242. public static func upload(_ data: Data,
  243. to url: URLConvertible,
  244. method: HTTPMethod = .post,
  245. headers: HTTPHeaders? = nil,
  246. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  247. return Session.default.upload(data, to: url, method: method, headers: headers, interceptor: interceptor)
  248. }
  249. /// Creates an `UploadRequest` using `SessionManager.default` to upload the contents of the `data` specified using
  250. /// the `urlRequest` provided.
  251. ///
  252. /// - Parameters:
  253. /// - data: The `Data` to upload.
  254. /// - urlRequest: The `URLRequestConvertible` value.
  255. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  256. ///
  257. /// - Returns: The created `UploadRequest`.
  258. public static func upload(_ data: Data,
  259. with urlRequest: URLRequestConvertible,
  260. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  261. return Session.default.upload(data, with: urlRequest, interceptor: interceptor)
  262. }
  263. // MARK: InputStream
  264. /// Creates an `UploadRequest` using `SessionManager.default` to upload the content provided by the `stream`
  265. /// specified using the `url`, `method` and `headers` provided.
  266. ///
  267. /// - Parameters:
  268. /// - stream: The `InputStream` to upload.
  269. /// - url: The `URLConvertible` value.
  270. /// - method: The `HTTPMethod`, `.post` by default.
  271. /// - headers: The `HTTPHeaders`, `nil` by default.
  272. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  273. ///
  274. /// - Returns: The created `UploadRequest`.
  275. public static func upload(_ stream: InputStream,
  276. to url: URLConvertible,
  277. method: HTTPMethod = .post,
  278. headers: HTTPHeaders? = nil,
  279. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  280. return Session.default.upload(stream, to: url, method: method, headers: headers, interceptor: interceptor)
  281. }
  282. /// Creates an `UploadRequest` using `SessionManager.default` to upload the content provided by the `stream`
  283. /// specified using the `urlRequest` specified.
  284. ///
  285. /// - Parameters:
  286. /// - stream: The `InputStream` to upload.
  287. /// - urlRequest: The `URLRequestConvertible` value.
  288. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  289. ///
  290. /// - Returns: The created `UploadRequest`.
  291. public static func upload(_ stream: InputStream,
  292. with urlRequest: URLRequestConvertible,
  293. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  294. return Session.default.upload(stream, with: urlRequest, interceptor: interceptor)
  295. }
  296. // MARK: MultipartFormData
  297. /// Encodes `multipartFormData` using `encodingMemoryThreshold` and uploads the result using `SessionManager.default`
  298. /// with the `url`, `method`, and `headers` provided.
  299. ///
  300. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cummulative
  301. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  302. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  303. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  304. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  305. /// used for larger payloads such as video content.
  306. ///
  307. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  308. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  309. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  310. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  311. /// technique was used.
  312. ///
  313. /// - Parameters:
  314. /// - multipartFormData: The closure used to append body parts to the `MultipartFormData`.
  315. /// - encodingMemoryThreshold: The encoding memory threshold in bytes. `10_000_000` bytes by default.
  316. /// - fileManager: The `FileManager` instance to use to manage streaming and encoding.
  317. /// - url: The `URLConvertible` value.
  318. /// - method: The `HTTPMethod`, `.post` by default.
  319. /// - headers: The `HTTPHeaders`, `nil` by default.
  320. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  321. ///
  322. /// - Returns: The created `UploadRequest`.
  323. public static func upload(multipartFormData: @escaping (MultipartFormData) -> Void,
  324. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  325. fileManager: FileManager = .default,
  326. to url: URLConvertible,
  327. method: HTTPMethod = .post,
  328. headers: HTTPHeaders? = nil,
  329. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  330. return Session.default.upload(multipartFormData: multipartFormData,
  331. usingThreshold: encodingMemoryThreshold,
  332. fileManager: fileManager,
  333. to: url,
  334. method: method,
  335. headers: headers,
  336. interceptor: interceptor)
  337. }
  338. /// Encodes `multipartFormData` using `encodingMemoryThreshold` and uploads the result using `SessionManager.default`
  339. /// using the `urlRequest` provided.
  340. ///
  341. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cummulative
  342. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  343. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  344. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  345. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  346. /// used for larger payloads such as video content.
  347. ///
  348. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  349. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  350. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  351. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  352. /// technique was used.
  353. ///
  354. /// - Parameters:
  355. /// - multipartFormData: The closure used to append body parts to the `MultipartFormData`.
  356. /// - encodingMemoryThreshold: The encoding memory threshold in bytes. `10_000_000` bytes by default.
  357. /// - urlRequest: The `URLRequestConvertible` value.
  358. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  359. ///
  360. /// - Returns: The `UploadRequest` created.
  361. @discardableResult
  362. public static func upload(multipartFormData: MultipartFormData,
  363. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  364. with urlRequest: URLRequestConvertible,
  365. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  366. return Session.default.upload(multipartFormData: multipartFormData,
  367. usingThreshold: encodingMemoryThreshold,
  368. with: urlRequest,
  369. interceptor: interceptor)
  370. }
  371. }