Repositorio del curso CCOM4030 el semestre B91 del proyecto Trolley

Alamofire.swift 32KB

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