Alamofire.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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` `SessionManager` 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. /// - Returns: The created `DataRequest`.
  38. @discardableResult
  39. public static func request(_ url: URLConvertible,
  40. method: HTTPMethod = .get,
  41. parameters: Parameters? = nil,
  42. encoding: ParameterEncoding = URLEncoding.default,
  43. headers: HTTPHeaders? = nil) -> DataRequest {
  44. return Session.default.request(url,
  45. method: method,
  46. parameters: parameters,
  47. encoding: encoding,
  48. headers: headers)
  49. }
  50. /// Creates a `DataRequest` using `SessionManager.default` to execute the specified `urlRequest`.
  51. ///
  52. /// - Parameter urlRequest: The `URLRequestConvertible` value.
  53. /// - Returns: The created `DataRequest`.
  54. @discardableResult
  55. public static func request(_ urlRequest: URLRequestConvertible) -> DataRequest {
  56. return Session.default.request(urlRequest)
  57. }
  58. // MARK: - Download Request
  59. // MARK: URL
  60. // TODO: Update docs when download temp file issue resolved.
  61. /// Creates a `DownloadRequest` using `SessionManager.default` to download the contents of the specified `url` to
  62. /// the provided `destination` using the `method`, `parameters`, `encoding`, and `headers` provided.
  63. ///
  64. /// If `destination` is not specified, the download will remain at the temporary location determined by the
  65. /// underlying `URLSession`.
  66. ///
  67. /// - Parameters:
  68. /// - url: The `URLConvertible` value.
  69. /// - method: The `HTTPMethod`, `.get` by default.
  70. /// - parameters: The `Parameters`, `nil` by default.
  71. /// - encoding: The `ParameterEncoding`, `URLEncoding.default` by default.
  72. /// - headers: The `HTTPHeaders`, `nil` by default.
  73. /// - destination: The `DownloadRequest.Destination` closure used the determine the destination of the downloaded
  74. /// file. `nil` by default.
  75. /// - Returns: The created `DownloadRequest`.
  76. @discardableResult
  77. public static func download(_ url: URLConvertible,
  78. method: HTTPMethod = .get,
  79. parameters: Parameters? = nil,
  80. encoding: ParameterEncoding = URLEncoding.default,
  81. headers: HTTPHeaders? = nil,
  82. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  83. return Session.default.download(url,
  84. method: method,
  85. parameters: parameters,
  86. encoding: encoding,
  87. headers: headers,
  88. to: destination)
  89. }
  90. // MARK: URLRequest
  91. /// Creates a `DownloadRequest` using `SessionManager.default` to execute the specified `urlRequest` and download
  92. /// the result to the provided `destination`.
  93. ///
  94. /// - Parameters:
  95. /// - urlRequest: The `URLRequestConvertible` value.
  96. /// - destination: The `DownloadRequest.Destination` closure used the determine the destination of the downloaded
  97. /// file. `nil` by default.
  98. /// - Returns: The created `DownloadRequest`.
  99. @discardableResult
  100. public static func download(_ urlRequest: URLRequestConvertible,
  101. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  102. return Session.default.download(urlRequest, to: destination)
  103. }
  104. // MARK: Resume Data
  105. /// Creates a `DownloadRequest` using the `SessionManager.default` from the `resumeData` produced from a previous
  106. /// `DownloadRequest` cancellation to retrieve the contents of the original request and save them to the `destination`.
  107. ///
  108. /// If `destination` is not specified, the contents will remain in the temporary location determined by the
  109. /// underlying URL session.
  110. ///
  111. /// 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),
  112. /// `resumeData` is broken on background URL session configurations. There's an underlying bug in the `resumeData`
  113. /// generation logic where the data is written incorrectly and will always fail to resume the download. For more
  114. /// information about the bug and possible workarounds, please refer to the [this Stack Overflow post](http://stackoverflow.com/a/39347461/1342462).
  115. ///
  116. /// - Parameters:
  117. /// - resumeData: The resume `Data`. This is an opaque blob produced by `URLSessionDownloadTask` when a task is
  118. /// cancelled. See [Apple's documentation](https://developer.apple.com/documentation/foundation/urlsessiondownloadtask/1411634-cancel)
  119. /// for more information.
  120. /// - destination: The `DownloadRequest.Destination` closure used to determine the destination of the downloaded
  121. /// file. `nil` by default.
  122. /// - Returns: The created `DownloadRequest`.
  123. @discardableResult
  124. public static func download(resumingWith resumeData: Data,
  125. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  126. return Session.default.download(resumingWith: resumeData, to: destination)
  127. }
  128. // MARK: - Upload Request
  129. // MARK: File
  130. /// Creates an `UploadRequest` using `SessionManager.default` to upload the contents of the `fileURL` specified
  131. /// using the `url`, `method` and `headers` provided.
  132. ///
  133. /// - Parameters:
  134. /// - fileURL: The `URL` of the file to upload.
  135. /// - url: The `URLConvertible` value.
  136. /// - method: The `HTTPMethod`, `.post` by default.
  137. /// - headers: The `HTTPHeaders`, `nil` by default.
  138. /// - Returns: The created `UploadRequest`.
  139. @discardableResult
  140. public static func upload(_ fileURL: URL,
  141. to url: URLConvertible,
  142. method: HTTPMethod = .post,
  143. headers: HTTPHeaders? = nil) -> UploadRequest {
  144. return Session.default.upload(fileURL, to: url, method: method, headers: headers)
  145. }
  146. /// Creates an `UploadRequest` using the `SessionManager.default` to upload the contents of the `fileURL` specificed
  147. /// using the `urlRequest` provided.
  148. ///
  149. /// - Parameters:
  150. /// - fileURL: The `URL` of the file to upload.
  151. /// - urlRequest: The `URLRequestConvertible` value.
  152. /// - Returns: The created `UploadRequest`.
  153. @discardableResult
  154. public static func upload(_ fileURL: URL, with urlRequest: URLRequestConvertible) -> UploadRequest {
  155. return Session.default.upload(fileURL, with: urlRequest)
  156. }
  157. // MARK: Data
  158. /// Creates an `UploadRequest` using `SessionManager.default` to upload the contents of the `data` specified using
  159. /// the `url`, `method` and `headers` provided.
  160. ///
  161. /// - Parameters:
  162. /// - data: The `Data` to upload.
  163. /// - url: The `URLConvertible` value.
  164. /// - method: The `HTTPMethod`, `.post` by default.
  165. /// - headers: The `HTTPHeaders`, `nil` by default.
  166. /// - Returns: The created `UploadRequest`.
  167. @discardableResult
  168. public static func upload(_ data: Data,
  169. to url: URLConvertible,
  170. method: HTTPMethod = .post,
  171. headers: HTTPHeaders? = nil) -> UploadRequest {
  172. return Session.default.upload(data, to: url, method: method, headers: headers)
  173. }
  174. /// Creates an `UploadRequest` using `SessionManager.default` to upload the contents of the `data` specified using
  175. /// the `urlRequest` provided.
  176. ///
  177. /// - Parameters:
  178. /// - data: The `Data` to upload.
  179. /// - urlRequest: The `URLRequestConvertible` value.
  180. /// - Returns: The created `UploadRequest`.
  181. @discardableResult
  182. public static func upload(_ data: Data, with urlRequest: URLRequestConvertible) -> UploadRequest {
  183. return Session.default.upload(data, with: urlRequest)
  184. }
  185. // MARK: InputStream
  186. /// Creates an `UploadRequest` using `SessionManager.default` to upload the content provided by the `stream`
  187. /// specified using the `url`, `method` and `headers` provided.
  188. ///
  189. /// - Parameters:
  190. /// - stream: The `InputStream` to upload.
  191. /// - url: The `URLConvertible` value.
  192. /// - method: The `HTTPMethod`, `.post` by default.
  193. /// - headers: The `HTTPHeaders`, `nil` by default.
  194. /// - Returns: The created `UploadRequest`.
  195. @discardableResult
  196. public static func upload(_ stream: InputStream,
  197. to url: URLConvertible,
  198. method: HTTPMethod = .post,
  199. headers: HTTPHeaders? = nil) -> UploadRequest {
  200. return Session.default.upload(stream, to: url, method: method, headers: headers)
  201. }
  202. /// Creates an `UploadRequest` using `SessionManager.default` to upload the content provided by the `stream`
  203. /// specified using the `urlRequest` specified.
  204. ///
  205. /// - Parameters:
  206. /// - stream: The `InputStream` to upload.
  207. /// - urlRequest: The `URLRequestConvertible` value.
  208. /// - Returns: The created `UploadRequest`.
  209. @discardableResult
  210. public static func upload(_ stream: InputStream, with urlRequest: URLRequestConvertible) -> UploadRequest {
  211. return Session.default.upload(stream, with: urlRequest)
  212. }
  213. // MARK: MultipartFormData
  214. /// Encodes `multipartFormData` using `encodingMemoryThreshold` and uploads the result using `SessionManager.default`
  215. /// with the `url`, `method`, and `headers` provided.
  216. ///
  217. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cummulative
  218. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  219. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  220. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  221. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  222. /// used for larger payloads such as video content.
  223. ///
  224. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  225. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  226. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  227. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  228. /// technique was used.
  229. ///
  230. /// - Parameters:
  231. /// - multipartFormData: The closure used to append body parts to the `MultipartFormData`.
  232. /// - encodingMemoryThreshold: The encoding memory threshold in bytes. `10_000_000` bytes by default.
  233. /// - url: The `URLConvertible` value.
  234. /// - method: The `HTTPMethod`, `.post` by default.
  235. /// - headers: The `HTTPHeaders`, `nil` by default.
  236. /// - Returns: The created `UploadRequest`.
  237. @discardableResult
  238. public static func upload(multipartFormData: @escaping (MultipartFormData) -> Void,
  239. usingThreshold encodingMemoryThreshold: UInt64 = MultipartUpload.encodingMemoryThreshold,
  240. to url: URLConvertible,
  241. method: HTTPMethod = .post,
  242. headers: HTTPHeaders? = nil) -> UploadRequest {
  243. return Session.default.upload(multipartFormData: multipartFormData,
  244. usingThreshold: encodingMemoryThreshold,
  245. to: url,
  246. method: method,
  247. headers: headers)
  248. }
  249. /// Encodes `multipartFormData` using `encodingMemoryThreshold` and uploads the result using `SessionManager.default`
  250. /// using the `urlRequest` provided.
  251. ///
  252. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cummulative
  253. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  254. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  255. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  256. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  257. /// used for larger payloads such as video content.
  258. ///
  259. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  260. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  261. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  262. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  263. /// technique was used.
  264. ///
  265. /// - Parameters:
  266. /// - multipartFormData: The closure used to append body parts to the `MultipartFormData`.
  267. /// - encodingMemoryThreshold: The encoding memory threshold in bytes. `10_000_000` bytes by default.
  268. /// - urlRequest: The `URLRequestConvertible` value.
  269. /// - Returns: The `UploadRequest` created.
  270. @discardableResult
  271. public static func upload(multipartFormData: @escaping (MultipartFormData) -> Void,
  272. usingThreshold encodingMemoryThreshold: UInt64 = MultipartUpload.encodingMemoryThreshold,
  273. with urlRequest: URLRequestConvertible) -> UploadRequest {
  274. return Session.default.upload(multipartFormData: multipartFormData,
  275. usingThreshold: encodingMemoryThreshold,
  276. with: urlRequest)
  277. }
  278. }