Alamofire.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 `Session.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 `Session.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 `ParameterEncoder`, `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 `Session.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 `Session.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 be moved to a temporary location determined by Alamofire.
  93. ///
  94. /// - Parameters:
  95. /// - url: The `URLConvertible` value.
  96. /// - method: The `HTTPMethod`, `.get` by default.
  97. /// - parameters: The `Parameters`, `nil` by default.
  98. /// - encoding: The `ParameterEncoding`, `URLEncoding.default` by default.
  99. /// - headers: The `HTTPHeaders`, `nil` by default.
  100. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  101. /// - destination: The `DownloadRequest.Destination` closure used the determine the destination of the
  102. /// downloaded file. `nil` by default.
  103. ///
  104. /// - Returns: The created `DownloadRequest`.
  105. public static func download(_ url: URLConvertible,
  106. method: HTTPMethod = .get,
  107. parameters: Parameters? = nil,
  108. encoding: ParameterEncoding = URLEncoding.default,
  109. headers: HTTPHeaders? = nil,
  110. interceptor: RequestInterceptor? = nil,
  111. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  112. return Session.default.download(url,
  113. method: method,
  114. parameters: parameters,
  115. encoding: encoding,
  116. headers: headers,
  117. interceptor: interceptor,
  118. to: destination)
  119. }
  120. /// Creates a `DownloadRequest` using `Session.default` to download the contents of the specified `url` to the
  121. /// provided `destination` using the `method`, encodable `parameters`, `encoder`, and `headers` provided.
  122. ///
  123. /// - Note: If `destination` is not specified, the download will be moved to a temporary location determined by
  124. /// Alamofire.
  125. ///
  126. /// - Parameters:
  127. /// - url: The `URLConvertible` value.
  128. /// - method: The `HTTPMethod`, `.get` by default.
  129. /// - parameters: The `Encodable` parameters, `nil` by default.
  130. /// - encoder: The `ParameterEncoder`, `URLEncodedFormParameterEncoder.default` by default.
  131. /// - headers: The `HTTPHeaders`, `nil` by default.
  132. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  133. /// - destination: The `DownloadRequest.Destination` closure used the determine the destination of the
  134. /// downloaded file. `nil` by default.
  135. ///
  136. /// - Returns: The created `DownloadRequest`.
  137. public static func download<Parameters: Encodable>(_ url: URLConvertible,
  138. method: HTTPMethod = .get,
  139. parameters: Parameters? = nil,
  140. encoder: ParameterEncoder = URLEncodedFormParameterEncoder.default,
  141. headers: HTTPHeaders? = nil,
  142. interceptor: RequestInterceptor? = nil,
  143. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  144. return Session.default.download(url,
  145. method: method,
  146. parameters: parameters,
  147. encoder: encoder,
  148. headers: headers,
  149. interceptor: interceptor,
  150. to: destination)
  151. }
  152. // MARK: URLRequest
  153. /// Creates a `DownloadRequest` using `Session.default` to execute the specified `urlRequest` and download
  154. /// the result to the provided `destination`.
  155. ///
  156. /// - Parameters:
  157. /// - urlRequest: The `URLRequestConvertible` value.
  158. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  159. /// - destination: The `DownloadRequest.Destination` closure used the determine the destination of the
  160. /// downloaded file. `nil` by default.
  161. ///
  162. /// - Returns: The created `DownloadRequest`.
  163. public static func download(_ urlRequest: URLRequestConvertible,
  164. interceptor: RequestInterceptor? = nil,
  165. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  166. return Session.default.download(urlRequest, interceptor: interceptor, to: destination)
  167. }
  168. // MARK: Resume Data
  169. /// Creates a `DownloadRequest` using the `Session.default` from the `resumeData` produced from a previous
  170. /// `DownloadRequest` cancellation to retrieve the contents of the original request and save them to the `destination`.
  171. ///
  172. /// - Note: If `destination` is not specified, the download will be moved to a temporary location determined by
  173. /// Alamofire.
  174. ///
  175. /// - Note: 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),
  176. /// `resumeData` is broken on background URL session configurations. There's an underlying bug in the `resumeData`
  177. /// generation logic where the data is written incorrectly and will always fail to resume the download. For more
  178. /// information about the bug and possible workarounds, please refer to the [this Stack Overflow post](http://stackoverflow.com/a/39347461/1342462).
  179. ///
  180. /// - Parameters:
  181. /// - resumeData: The resume `Data`. This is an opaque blob produced by `URLSessionDownloadTask` when a task is
  182. /// cancelled. See [Apple's documentation](https://developer.apple.com/documentation/foundation/urlsessiondownloadtask/1411634-cancel)
  183. /// for more information.
  184. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  185. /// - destination: The `DownloadRequest.Destination` closure used to determine the destination of the downloaded
  186. /// file. `nil` by default.
  187. ///
  188. /// - Returns: The created `DownloadRequest`.
  189. public static func download(resumingWith resumeData: Data,
  190. interceptor: RequestInterceptor? = nil,
  191. to destination: DownloadRequest.Destination? = nil) -> DownloadRequest {
  192. return Session.default.download(resumingWith: resumeData, interceptor: interceptor, to: destination)
  193. }
  194. // MARK: - Upload Request
  195. // MARK: File
  196. /// Creates an `UploadRequest` using `Session.default` to upload the contents of the `fileURL` specified
  197. /// using the `url`, `method` and `headers` provided.
  198. ///
  199. /// - Parameters:
  200. /// - fileURL: The `URL` of the file to upload.
  201. /// - url: The `URLConvertible` value.
  202. /// - method: The `HTTPMethod`, `.post` by default.
  203. /// - headers: The `HTTPHeaders`, `nil` by default.
  204. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  205. ///
  206. /// - Returns: The created `UploadRequest`.
  207. public static func upload(_ fileURL: URL,
  208. to url: URLConvertible,
  209. method: HTTPMethod = .post,
  210. headers: HTTPHeaders? = nil,
  211. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  212. return Session.default.upload(fileURL, to: url, method: method, headers: headers, interceptor: interceptor)
  213. }
  214. /// Creates an `UploadRequest` using the `Session.default` to upload the contents of the `fileURL` specificed
  215. /// using the `urlRequest` provided.
  216. ///
  217. /// - Parameters:
  218. /// - fileURL: The `URL` of the file to upload.
  219. /// - urlRequest: The `URLRequestConvertible` value.
  220. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  221. ///
  222. /// - Returns: The created `UploadRequest`.
  223. public static func upload(_ fileURL: URL,
  224. with urlRequest: URLRequestConvertible,
  225. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  226. return Session.default.upload(fileURL, with: urlRequest, interceptor: interceptor)
  227. }
  228. // MARK: Data
  229. /// Creates an `UploadRequest` using `Session.default` to upload the contents of the `data` specified using
  230. /// the `url`, `method` and `headers` provided.
  231. ///
  232. /// - Parameters:
  233. /// - data: The `Data` to upload.
  234. /// - url: The `URLConvertible` value.
  235. /// - method: The `HTTPMethod`, `.post` by default.
  236. /// - headers: The `HTTPHeaders`, `nil` by default.
  237. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  238. /// - retryPolicies: The `RetryPolicy` types, `[]` by default.
  239. ///
  240. /// - Returns: The created `UploadRequest`.
  241. public static func upload(_ data: Data,
  242. to url: URLConvertible,
  243. method: HTTPMethod = .post,
  244. headers: HTTPHeaders? = nil,
  245. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  246. return Session.default.upload(data, to: url, method: method, headers: headers, interceptor: interceptor)
  247. }
  248. /// Creates an `UploadRequest` using `Session.default` to upload the contents of the `data` specified using the
  249. /// `urlRequest` provided.
  250. ///
  251. /// - Parameters:
  252. /// - data: The `Data` to upload.
  253. /// - urlRequest: The `URLRequestConvertible` value.
  254. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  255. ///
  256. /// - Returns: The created `UploadRequest`.
  257. public static func upload(_ data: Data,
  258. with urlRequest: URLRequestConvertible,
  259. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  260. return Session.default.upload(data, with: urlRequest, interceptor: interceptor)
  261. }
  262. // MARK: InputStream
  263. /// Creates an `UploadRequest` using `Session.default` to upload the content provided by the `stream` specified
  264. /// using the `url`, `method` and `headers` provided.
  265. ///
  266. /// - Parameters:
  267. /// - stream: The `InputStream` to upload.
  268. /// - url: The `URLConvertible` value.
  269. /// - method: The `HTTPMethod`, `.post` by default.
  270. /// - headers: The `HTTPHeaders`, `nil` by default.
  271. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  272. ///
  273. /// - Returns: The created `UploadRequest`.
  274. public static func upload(_ stream: InputStream,
  275. to url: URLConvertible,
  276. method: HTTPMethod = .post,
  277. headers: HTTPHeaders? = nil,
  278. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  279. return Session.default.upload(stream, to: url, method: method, headers: headers, interceptor: interceptor)
  280. }
  281. /// Creates an `UploadRequest` using `Session.default` to upload the content provided by the `stream`
  282. /// specified using the `urlRequest` specified.
  283. ///
  284. /// - Parameters:
  285. /// - stream: The `InputStream` to upload.
  286. /// - urlRequest: The `URLRequestConvertible` value.
  287. /// - interceptor: The `RequestInterceptor`, `nil` by default.
  288. ///
  289. /// - Returns: The created `UploadRequest`.
  290. public static func upload(_ stream: InputStream,
  291. with urlRequest: URLRequestConvertible,
  292. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  293. return Session.default.upload(stream, with: urlRequest, interceptor: interceptor)
  294. }
  295. // MARK: MultipartFormData
  296. /// Creates an `UploadRequest` for the multipart form data built using a closure and sent using the provided
  297. /// `URLRequest` components and `RequestInterceptor`.
  298. ///
  299. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cummulative
  300. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  301. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  302. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  303. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  304. /// used for larger payloads such as video content.
  305. ///
  306. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  307. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  308. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  309. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  310. /// technique was used.
  311. ///
  312. /// - Parameters:
  313. /// - multipartFormData: `MultipartFormData` building closure.
  314. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  315. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by default.
  316. /// - fileManager: `FileManager` to be used if the form data exceeds the memory threshold and is
  317. /// written to disk before being uploaded.
  318. /// - convertible: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  319. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  320. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  321. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  322. ///
  323. /// - Returns: The created `UploadRequest`.
  324. public static func upload(multipartFormData: @escaping (MultipartFormData) -> Void,
  325. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  326. fileManager: FileManager = .default,
  327. to url: URLConvertible,
  328. method: HTTPMethod = .post,
  329. headers: HTTPHeaders? = nil,
  330. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  331. return Session.default.upload(multipartFormData: multipartFormData,
  332. usingThreshold: encodingMemoryThreshold,
  333. fileManager: fileManager,
  334. to: url,
  335. method: method,
  336. headers: headers,
  337. interceptor: interceptor)
  338. }
  339. /// Creates an `UploadRequest` using a `MultipartFormData` building closure, the provided `URLRequestConvertible`
  340. /// value, and a `RequestInterceptor`.
  341. ///
  342. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cummulative
  343. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  344. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  345. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  346. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  347. /// used for larger payloads such as video content.
  348. ///
  349. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  350. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  351. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  352. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  353. /// technique was used.
  354. ///
  355. /// - Parameters:
  356. /// - multipartFormData: `MultipartFormData` building closure.
  357. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  358. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by default.
  359. /// - fileManager: `FileManager` to be used if the form data exceeds the memory threshold and is
  360. /// written to disk before being uploaded.
  361. /// - request: `URLRequestConvertible` value to be used to create the `URLRequest`.
  362. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  363. ///
  364. /// - Returns: The created `UploadRequest`.
  365. public static func upload(multipartFormData: @escaping (MultipartFormData) -> Void,
  366. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  367. fileManager: FileManager = .default,
  368. with request: URLRequestConvertible,
  369. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  370. return Session.default.upload(multipartFormData: multipartFormData,
  371. usingThreshold: encodingMemoryThreshold,
  372. fileManager: fileManager,
  373. with: request,
  374. interceptor: interceptor)
  375. }
  376. /// Creates an `UploadRequest` for the prebuilt `MultipartFormData` value using the provided `URLRequest` components
  377. /// and `RequestInterceptor`.
  378. ///
  379. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cummulative
  380. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  381. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  382. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  383. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  384. /// used for larger payloads such as video content.
  385. ///
  386. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  387. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  388. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  389. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  390. /// technique was used.
  391. ///
  392. /// - Parameters:
  393. /// - multipartFormData: `MultipartFormData` instance to upload.
  394. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  395. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by default.
  396. /// - url: `URLConvertible` value to be used as the `URLRequest`'s `URL`.
  397. /// - method: `HTTPMethod` for the `URLRequest`. `.post` by default.
  398. /// - headers: `HTTPHeaders` value to be added to the `URLRequest`. `nil` by default.
  399. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  400. ///
  401. /// - Returns: The created `UploadRequest`.
  402. public static func upload(multipartFormData: MultipartFormData,
  403. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  404. to url: URLConvertible,
  405. method: HTTPMethod = .post,
  406. headers: HTTPHeaders? = nil,
  407. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  408. return Session.default.upload(multipartFormData: multipartFormData,
  409. usingThreshold: encodingMemoryThreshold,
  410. to: url,
  411. method: method,
  412. headers: headers,
  413. interceptor: interceptor)
  414. }
  415. /// Creates an `UploadRequest` for the prebuilt `MultipartFormData` value using the providing `URLRequestConvertible`
  416. /// value and `RequestInterceptor`.
  417. ///
  418. /// It is important to understand the memory implications of uploading `MultipartFormData`. If the cummulative
  419. /// payload is small, encoding the data in-memory and directly uploading to a server is the by far the most
  420. /// efficient approach. However, if the payload is too large, encoding the data in-memory could cause your app to
  421. /// be terminated. Larger payloads must first be written to disk using input and output streams to keep the memory
  422. /// footprint low, then the data can be uploaded as a stream from the resulting file. Streaming from disk MUST be
  423. /// used for larger payloads such as video content.
  424. ///
  425. /// The `encodingMemoryThreshold` parameter allows Alamofire to automatically determine whether to encode in-memory
  426. /// or stream from disk. If the content length of the `MultipartFormData` is below the `encodingMemoryThreshold`,
  427. /// encoding takes place in-memory. If the content length exceeds the threshold, the data is streamed to disk
  428. /// during the encoding process. Then the result is uploaded as data or as a stream depending on which encoding
  429. /// technique was used.
  430. ///
  431. /// - Parameters:
  432. /// - multipartFormData: `MultipartFormData` instance to upload.
  433. /// - encodingMemoryThreshold: Byte threshold used to determine whether the form data is encoded into memory or
  434. /// onto disk before being uploaded. `MultipartFormData.encodingMemoryThreshold` by
  435. /// default.
  436. /// - request: `URLRequestConvertible` value to be used to create the `URLRequest`.
  437. /// - interceptor: `RequestInterceptor` value to be used by the returned `DataRequest`. `nil` by default.
  438. ///
  439. /// - Returns: The created `UploadRequest`.
  440. public static func upload(multipartFormData: MultipartFormData,
  441. usingThreshold encodingMemoryThreshold: UInt64 = MultipartFormData.encodingMemoryThreshold,
  442. with request: URLRequestConvertible,
  443. interceptor: RequestInterceptor? = nil) -> UploadRequest {
  444. return Session.default.upload(multipartFormData: multipartFormData,
  445. usingThreshold: encodingMemoryThreshold,
  446. with: request,
  447. interceptor: interceptor)
  448. }
  449. }