DownloadRequest.swift 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. //
  2. // DownloadRequest.swift
  3. //
  4. // Copyright (c) 2014-2024 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. /// `Request` subclass which downloads `Data` to a file on disk using `URLSessionDownloadTask`.
  26. public final class DownloadRequest: Request, @unchecked Sendable {
  27. /// A set of options to be executed prior to moving a downloaded file from the temporary `URL` to the destination
  28. /// `URL`.
  29. public struct Options: OptionSet, Sendable {
  30. /// Specifies that intermediate directories for the destination URL should be created.
  31. public static let createIntermediateDirectories = Options(rawValue: 1 << 0)
  32. /// Specifies that any previous file at the destination `URL` should be removed.
  33. public static let removePreviousFile = Options(rawValue: 1 << 1)
  34. public let rawValue: Int
  35. public init(rawValue: Int) {
  36. self.rawValue = rawValue
  37. }
  38. }
  39. // MARK: Destination
  40. /// A closure executed once a `DownloadRequest` has successfully completed in order to determine where to move the
  41. /// temporary file written to during the download process. The closure takes two arguments: the temporary file URL
  42. /// and the `HTTPURLResponse`, and returns two values: the file URL where the temporary file should be moved and
  43. /// the options defining how the file should be moved.
  44. ///
  45. /// - Note: Downloads from a local `file://` `URL`s do not use the `Destination` closure, as those downloads do not
  46. /// return an `HTTPURLResponse`. Instead the file is merely moved within the temporary directory.
  47. public typealias Destination = @Sendable (_ temporaryURL: URL,
  48. _ response: HTTPURLResponse) -> (destinationURL: URL, options: Options)
  49. /// Creates a download file destination closure which uses the default file manager to move the temporary file to a
  50. /// file URL in the first available directory with the specified search path directory and search path domain mask.
  51. ///
  52. /// - Parameters:
  53. /// - directory: The search path directory. `.documentDirectory` by default.
  54. /// - domain: The search path domain mask. `.userDomainMask` by default.
  55. /// - options: `DownloadRequest.Options` used when moving the downloaded file to its destination. None by
  56. /// default.
  57. /// - Returns: The `Destination` closure.
  58. public class func suggestedDownloadDestination(for directory: FileManager.SearchPathDirectory = .documentDirectory,
  59. in domain: FileManager.SearchPathDomainMask = .userDomainMask,
  60. options: Options = []) -> Destination {
  61. { temporaryURL, response in
  62. let directoryURLs = FileManager.default.urls(for: directory, in: domain)
  63. let url = directoryURLs.first?.appendingPathComponent(response.suggestedFilename!) ?? temporaryURL
  64. return (url, options)
  65. }
  66. }
  67. /// Default `Destination` used by Alamofire to ensure all downloads persist. This `Destination` prepends
  68. /// `Alamofire_` to the automatically generated download name and moves it within the temporary directory. Files
  69. /// with this destination must be additionally moved if they should survive the system reclamation of temporary
  70. /// space.
  71. static let defaultDestination: Destination = { url, _ in
  72. (defaultDestinationURL(url), [])
  73. }
  74. /// Default `URL` creation closure. Creates a `URL` in the temporary directory with `Alamofire_` prepended to the
  75. /// provided file name.
  76. static let defaultDestinationURL: @Sendable (URL) -> URL = { url in
  77. let filename = "Alamofire_\(url.lastPathComponent)"
  78. let destination = url.deletingLastPathComponent().appendingPathComponent(filename)
  79. return destination
  80. }
  81. // MARK: Downloadable
  82. /// Type describing the source used to create the underlying `URLSessionDownloadTask`.
  83. public enum Downloadable {
  84. /// Download should be started from the `URLRequest` produced by the associated `URLRequestConvertible` value.
  85. case request(any URLRequestConvertible)
  86. /// Download should be started from the associated resume `Data` value.
  87. case resumeData(Data)
  88. }
  89. // MARK: Mutable State
  90. /// Type containing all mutable state for `DownloadRequest` instances.
  91. private struct DownloadRequestMutableState {
  92. /// Possible resume `Data` produced when cancelling the instance.
  93. var resumeData: Data?
  94. /// `URL` to which `Data` is being downloaded.
  95. var fileURL: URL?
  96. }
  97. /// Protected mutable state specific to `DownloadRequest`.
  98. private let mutableDownloadState = Protected(DownloadRequestMutableState())
  99. /// If the download is resumable and is eventually cancelled or fails, this value may be used to resume the download
  100. /// using the `download(resumingWith data:)` API.
  101. ///
  102. /// - Note: For more information about `resumeData`, see [Apple's documentation](https://developer.apple.com/documentation/foundation/urlsessiondownloadtask/1411634-cancel).
  103. public var resumeData: Data? {
  104. #if !canImport(FoundationNetworking) // If we not using swift-corelibs-foundation.
  105. return mutableDownloadState.read(\.resumeData) ?? error?.downloadResumeData
  106. #else
  107. return mutableDownloadState.read(\.resumeData)
  108. #endif
  109. }
  110. /// If the download is successful, the `URL` where the file was downloaded.
  111. public var fileURL: URL? { mutableDownloadState.read(\.fileURL) }
  112. // MARK: Initial State
  113. /// `Downloadable` value used for this instance.
  114. public let downloadable: Downloadable
  115. /// The `Destination` to which the downloaded file is moved.
  116. let destination: Destination
  117. /// Creates a `DownloadRequest` using the provided parameters.
  118. ///
  119. /// - Parameters:
  120. /// - id: `UUID` used for the `Hashable` and `Equatable` implementations. `UUID()` by default.
  121. /// - downloadable: `Downloadable` value used to create `URLSessionDownloadTasks` for the instance.
  122. /// - underlyingQueue: `DispatchQueue` on which all internal `Request` work is performed.
  123. /// - serializationQueue: `DispatchQueue` on which all serialization work is performed. By default targets
  124. /// `underlyingQueue`, but can be passed another queue from a `Session`.
  125. /// - eventMonitor: `EventMonitor` called for event callbacks from internal `Request` actions.
  126. /// - interceptor: `RequestInterceptor` used throughout the request lifecycle.
  127. /// - shouldAutomaticallyResume: Whether the instance should resume after the first response handler is added.
  128. /// - delegate: `RequestDelegate` that provides an interface to actions not performed by the `Request`
  129. /// - destination: `Destination` closure used to move the downloaded file to its final location.
  130. init(id: UUID = UUID(),
  131. downloadable: Downloadable,
  132. underlyingQueue: DispatchQueue,
  133. serializationQueue: DispatchQueue,
  134. eventMonitor: (any EventMonitor)?,
  135. interceptor: (any RequestInterceptor)?,
  136. shouldAutomaticallyResume: Bool?,
  137. delegate: any RequestDelegate,
  138. destination: @escaping Destination) {
  139. self.downloadable = downloadable
  140. self.destination = destination
  141. super.init(id: id,
  142. underlyingQueue: underlyingQueue,
  143. serializationQueue: serializationQueue,
  144. eventMonitor: eventMonitor,
  145. interceptor: interceptor,
  146. shouldAutomaticallyResume: shouldAutomaticallyResume,
  147. delegate: delegate)
  148. }
  149. override func reset() {
  150. super.reset()
  151. mutableDownloadState.write {
  152. $0.resumeData = nil
  153. $0.fileURL = nil
  154. }
  155. }
  156. /// Called when a download has finished.
  157. ///
  158. /// - Parameters:
  159. /// - task: `URLSessionTask` that finished the download.
  160. /// - result: `Result` of the automatic move to `destination`.
  161. func didFinishDownloading(using task: URLSessionTask, with result: Result<URL, AFError>) {
  162. eventMonitor?.request(self, didFinishDownloadingUsing: task, with: result)
  163. switch result {
  164. case let .success(url): mutableDownloadState.write { $0.fileURL = url }
  165. case let .failure(error): self.error = error
  166. }
  167. }
  168. /// Updates the `downloadProgress` using the provided values.
  169. ///
  170. /// - Parameters:
  171. /// - bytesWritten: Total bytes written so far.
  172. /// - totalBytesExpectedToWrite: Total bytes expected to write.
  173. func updateDownloadProgress(bytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
  174. downloadProgress.totalUnitCount = totalBytesExpectedToWrite
  175. downloadProgress.completedUnitCount += bytesWritten
  176. downloadProgressHandler?.queue.async { self.downloadProgressHandler?.handler(self.downloadProgress) }
  177. }
  178. override func task(for request: URLRequest, using session: URLSession) -> URLSessionTask {
  179. session.downloadTask(with: request)
  180. }
  181. /// Creates a `URLSessionTask` from the provided resume data.
  182. ///
  183. /// - Parameters:
  184. /// - data: `Data` used to resume the download.
  185. /// - session: `URLSession` used to create the `URLSessionTask`.
  186. ///
  187. /// - Returns: The `URLSessionTask` created.
  188. public func task(forResumeData data: Data, using session: URLSession) -> URLSessionTask {
  189. session.downloadTask(withResumeData: data)
  190. }
  191. /// Cancels the instance. Once cancelled, a `DownloadRequest` can no longer be resumed or suspended.
  192. ///
  193. /// - Note: This method will NOT produce resume data. If you wish to cancel and produce resume data, use
  194. /// `cancel(producingResumeData:)` or `cancel(byProducingResumeData:)`.
  195. ///
  196. /// - Returns: The instance.
  197. @discardableResult
  198. override public func cancel() -> Self {
  199. cancel(producingResumeData: false)
  200. }
  201. /// Cancels the instance, optionally producing resume data. Once cancelled, a `DownloadRequest` can no longer be
  202. /// resumed or suspended.
  203. ///
  204. /// - Note: If `producingResumeData` is `true`, the `resumeData` property will be populated with any resume data, if
  205. /// available.
  206. ///
  207. /// - Returns: The instance.
  208. @discardableResult
  209. public func cancel(producingResumeData shouldProduceResumeData: Bool) -> Self {
  210. cancel(optionallyProducingResumeData: shouldProduceResumeData ? { @Sendable _ in } : nil)
  211. }
  212. /// Cancels the instance while producing resume data. Once cancelled, a `DownloadRequest` can no longer be resumed
  213. /// or suspended.
  214. ///
  215. /// - Note: The resume data passed to the completion handler will also be available on the instance's `resumeData`
  216. /// property.
  217. ///
  218. /// - Parameter completionHandler: The completion handler that is called when the download has been successfully
  219. /// cancelled. It is not guaranteed to be called on a particular queue, so you may
  220. /// want use an appropriate queue to perform your work.
  221. ///
  222. /// - Returns: The instance.
  223. @preconcurrency
  224. @discardableResult
  225. public func cancel(byProducingResumeData completionHandler: @escaping @Sendable (_ data: Data?) -> Void) -> Self {
  226. cancel(optionallyProducingResumeData: completionHandler)
  227. }
  228. /// Internal implementation of cancellation that optionally takes a resume data handler. If no handler is passed,
  229. /// cancellation is performed without producing resume data.
  230. ///
  231. /// - Parameter completionHandler: Optional resume data handler.
  232. ///
  233. /// - Returns: The instance.
  234. private func cancel(optionallyProducingResumeData completionHandler: (@Sendable (_ resumeData: Data?) -> Void)?) -> Self {
  235. mutableState.write { mutableState in
  236. guard mutableState.state.canTransitionTo(.cancelled) else { return }
  237. mutableState.state = .cancelled
  238. underlyingQueue.async { self.didCancel() }
  239. guard let task = mutableState.tasks.last as? URLSessionDownloadTask, task.state != .completed else {
  240. underlyingQueue.async { self.finish() }
  241. return
  242. }
  243. if let completionHandler {
  244. // Resume to ensure metrics are gathered.
  245. task.resume()
  246. task.cancel { resumeData in
  247. self.mutableDownloadState.write { $0.resumeData = resumeData }
  248. self.underlyingQueue.async { self.didCancelTask(task) }
  249. completionHandler(resumeData)
  250. }
  251. } else {
  252. // Resume to ensure metrics are gathered.
  253. task.resume()
  254. task.cancel()
  255. self.underlyingQueue.async { self.didCancelTask(task) }
  256. }
  257. }
  258. return self
  259. }
  260. /// Validates the request, using the specified closure.
  261. ///
  262. /// - Note: If validation fails, subsequent calls to response handlers will have an associated error.
  263. ///
  264. /// - Parameter validation: `Validation` closure to validate the response.
  265. ///
  266. /// - Returns: The instance.
  267. @discardableResult
  268. public func validate(_ validation: @escaping Validation) -> Self {
  269. let validator: @Sendable () -> Void = { [unowned self] in
  270. guard error == nil, let response else { return }
  271. let result = validation(request, response, fileURL)
  272. if case let .failure(error) = result {
  273. self.error = error.asAFError(or: .responseValidationFailed(reason: .customValidationFailed(error: error)))
  274. }
  275. eventMonitor?.request(self,
  276. didValidateRequest: request,
  277. response: response,
  278. fileURL: fileURL,
  279. withResult: result)
  280. }
  281. validators.write { $0.append(validator) }
  282. return self
  283. }
  284. // MARK: - Response Serialization
  285. /// Adds a handler to be called once the request has finished.
  286. ///
  287. /// - Parameters:
  288. /// - queue: The queue on which the completion handler is dispatched. `.main` by default.
  289. /// - completionHandler: The code to be executed once the request has finished.
  290. ///
  291. /// - Returns: The request.
  292. @preconcurrency
  293. @discardableResult
  294. public func response(queue: DispatchQueue = .main,
  295. completionHandler: @escaping @Sendable (AFDownloadResponse<URL?>) -> Void)
  296. -> Self {
  297. appendResponseSerializer {
  298. // Start work that should be on the serialization queue.
  299. let result = AFResult<URL?>(value: self.fileURL, error: self.error)
  300. // End work that should be on the serialization queue.
  301. self.underlyingQueue.async {
  302. let response = DownloadResponse(request: self.request,
  303. response: self.response,
  304. fileURL: self.fileURL,
  305. resumeData: self.resumeData,
  306. metrics: self.metrics,
  307. serializationDuration: 0,
  308. result: result)
  309. self.eventMonitor?.request(self, didParseResponse: response)
  310. self.responseSerializerDidComplete { queue.async { completionHandler(response) } }
  311. }
  312. }
  313. return self
  314. }
  315. private func _response<Serializer: DownloadResponseSerializerProtocol>(queue: DispatchQueue = .main,
  316. responseSerializer: Serializer,
  317. completionHandler: @escaping @Sendable (AFDownloadResponse<Serializer.SerializedObject>) -> Void)
  318. -> Self {
  319. appendResponseSerializer {
  320. // Start work that should be on the serialization queue.
  321. let start = ProcessInfo.processInfo.systemUptime
  322. let result: AFResult<Serializer.SerializedObject> = Result {
  323. try responseSerializer.serializeDownload(request: self.request,
  324. response: self.response,
  325. fileURL: self.fileURL,
  326. error: self.error)
  327. }.mapError { error in
  328. error.asAFError(or: .responseSerializationFailed(reason: .customSerializationFailed(error: error)))
  329. }
  330. let end = ProcessInfo.processInfo.systemUptime
  331. // End work that should be on the serialization queue.
  332. self.underlyingQueue.async {
  333. let response = DownloadResponse(request: self.request,
  334. response: self.response,
  335. fileURL: self.fileURL,
  336. resumeData: self.resumeData,
  337. metrics: self.metrics,
  338. serializationDuration: end - start,
  339. result: result)
  340. self.eventMonitor?.request(self, didParseResponse: response)
  341. guard let serializerError = result.failure, let delegate = self.delegate else {
  342. self.responseSerializerDidComplete { queue.async { completionHandler(response) } }
  343. return
  344. }
  345. delegate.retryResult(for: self, dueTo: serializerError) { retryResult in
  346. var didComplete: (@Sendable () -> Void)?
  347. defer {
  348. if let didComplete {
  349. self.responseSerializerDidComplete { queue.async { didComplete() } }
  350. }
  351. }
  352. switch retryResult {
  353. case .doNotRetry:
  354. didComplete = { completionHandler(response) }
  355. case let .doNotRetryWithError(retryError):
  356. let result: AFResult<Serializer.SerializedObject> = .failure(retryError.asAFError(orFailWith: "Received retryError was not already AFError"))
  357. let response = DownloadResponse(request: self.request,
  358. response: self.response,
  359. fileURL: self.fileURL,
  360. resumeData: self.resumeData,
  361. metrics: self.metrics,
  362. serializationDuration: end - start,
  363. result: result)
  364. didComplete = { completionHandler(response) }
  365. case .retry, .retryWithDelay:
  366. delegate.retryRequest(self, withDelay: retryResult.delay)
  367. }
  368. }
  369. }
  370. }
  371. return self
  372. }
  373. /// Adds a handler to be called once the request has finished.
  374. ///
  375. /// - Note: This handler will read the entire downloaded file into memory, use with caution.
  376. ///
  377. /// - Parameters:
  378. /// - queue: The queue on which the completion handler is dispatched. `.main` by default.
  379. /// - responseSerializer: The response serializer responsible for serializing the request, response, and data
  380. /// contained in the destination `URL`.
  381. /// - completionHandler: The code to be executed once the request has finished.
  382. ///
  383. /// - Returns: The request.
  384. @discardableResult
  385. public func response<Serializer: DownloadResponseSerializerProtocol>(queue: DispatchQueue = .main,
  386. responseSerializer: Serializer,
  387. completionHandler: @escaping @Sendable (AFDownloadResponse<Serializer.SerializedObject>) -> Void)
  388. -> Self {
  389. _response(queue: queue, responseSerializer: responseSerializer, completionHandler: completionHandler)
  390. }
  391. /// Adds a handler to be called once the request has finished.
  392. ///
  393. /// - Note: This handler will read the entire downloaded file into memory, use with caution.
  394. ///
  395. /// - Parameters:
  396. /// - queue: The queue on which the completion handler is dispatched. `.main` by default.
  397. /// - responseSerializer: The response serializer responsible for serializing the request, response, and data
  398. /// contained in the destination `URL`.
  399. /// - completionHandler: The code to be executed once the request has finished.
  400. ///
  401. /// - Returns: The request.
  402. @discardableResult
  403. public func response<Serializer: ResponseSerializer>(queue: DispatchQueue = .main,
  404. responseSerializer: Serializer,
  405. completionHandler: @escaping @Sendable (AFDownloadResponse<Serializer.SerializedObject>) -> Void)
  406. -> Self {
  407. _response(queue: queue, responseSerializer: responseSerializer, completionHandler: completionHandler)
  408. }
  409. /// Adds a handler using a `URLResponseSerializer` to be called once the request is finished.
  410. ///
  411. /// - Parameters:
  412. /// - queue: The queue on which the completion handler is called. `.main` by default.
  413. /// - completionHandler: A closure to be executed once the request has finished.
  414. ///
  415. /// - Returns: The request.
  416. @preconcurrency
  417. @discardableResult
  418. public func responseURL(queue: DispatchQueue = .main,
  419. completionHandler: @escaping @Sendable (AFDownloadResponse<URL>) -> Void) -> Self {
  420. response(queue: queue, responseSerializer: URLResponseSerializer(), completionHandler: completionHandler)
  421. }
  422. /// Adds a handler using a `DataResponseSerializer` to be called once the request has finished.
  423. ///
  424. /// - Note: This handler will read the entire downloaded file into memory, use with caution.
  425. ///
  426. /// - Parameters:
  427. /// - queue: The queue on which the completion handler is called. `.main` by default.
  428. /// - dataPreprocessor: `DataPreprocessor` which processes the received `Data` before calling the
  429. /// `completionHandler`. `PassthroughPreprocessor()` by default.
  430. /// - emptyResponseCodes: HTTP status codes for which empty responses are always valid. `[204, 205]` by default.
  431. /// - emptyRequestMethods: `HTTPMethod`s for which empty responses are always valid. `[.head]` by default.
  432. /// - completionHandler: A closure to be executed once the request has finished.
  433. ///
  434. /// - Returns: The request.
  435. @preconcurrency
  436. @discardableResult
  437. public func responseData(queue: DispatchQueue = .main,
  438. dataPreprocessor: any DataPreprocessor = DataResponseSerializer.defaultDataPreprocessor,
  439. emptyResponseCodes: Set<Int> = DataResponseSerializer.defaultEmptyResponseCodes,
  440. emptyRequestMethods: Set<HTTPMethod> = DataResponseSerializer.defaultEmptyRequestMethods,
  441. completionHandler: @escaping @Sendable (AFDownloadResponse<Data>) -> Void) -> Self {
  442. response(queue: queue,
  443. responseSerializer: DataResponseSerializer(dataPreprocessor: dataPreprocessor,
  444. emptyResponseCodes: emptyResponseCodes,
  445. emptyRequestMethods: emptyRequestMethods),
  446. completionHandler: completionHandler)
  447. }
  448. /// Adds a handler using a `StringResponseSerializer` to be called once the request has finished.
  449. ///
  450. /// - Note: This handler will read the entire downloaded file into memory, use with caution.
  451. ///
  452. /// - Parameters:
  453. /// - queue: The queue on which the completion handler is dispatched. `.main` by default.
  454. /// - dataPreprocessor: `DataPreprocessor` which processes the received `Data` before calling the
  455. /// `completionHandler`. `PassthroughPreprocessor()` by default.
  456. /// - encoding: The string encoding. Defaults to `nil`, in which case the encoding will be determined
  457. /// from the server response, falling back to the default HTTP character set, `ISO-8859-1`.
  458. /// - emptyResponseCodes: HTTP status codes for which empty responses are always valid. `[204, 205]` by default.
  459. /// - emptyRequestMethods: `HTTPMethod`s for which empty responses are always valid. `[.head]` by default.
  460. /// - completionHandler: A closure to be executed once the request has finished.
  461. ///
  462. /// - Returns: The request.
  463. @preconcurrency
  464. @discardableResult
  465. public func responseString(queue: DispatchQueue = .main,
  466. dataPreprocessor: any DataPreprocessor = StringResponseSerializer.defaultDataPreprocessor,
  467. encoding: String.Encoding? = nil,
  468. emptyResponseCodes: Set<Int> = StringResponseSerializer.defaultEmptyResponseCodes,
  469. emptyRequestMethods: Set<HTTPMethod> = StringResponseSerializer.defaultEmptyRequestMethods,
  470. completionHandler: @escaping @Sendable (AFDownloadResponse<String>) -> Void) -> Self {
  471. response(queue: queue,
  472. responseSerializer: StringResponseSerializer(dataPreprocessor: dataPreprocessor,
  473. encoding: encoding,
  474. emptyResponseCodes: emptyResponseCodes,
  475. emptyRequestMethods: emptyRequestMethods),
  476. completionHandler: completionHandler)
  477. }
  478. /// Adds a handler using a `JSONResponseSerializer` to be called once the request has finished.
  479. ///
  480. /// - Note: This handler will read the entire downloaded file into memory, use with caution.
  481. ///
  482. /// - Parameters:
  483. /// - queue: The queue on which the completion handler is dispatched. `.main` by default.
  484. /// - dataPreprocessor: `DataPreprocessor` which processes the received `Data` before calling the
  485. /// `completionHandler`. `PassthroughPreprocessor()` by default.
  486. /// - emptyResponseCodes: HTTP status codes for which empty responses are always valid. `[204, 205]` by default.
  487. /// - emptyRequestMethods: `HTTPMethod`s for which empty responses are always valid. `[.head]` by default.
  488. /// - options: `JSONSerialization.ReadingOptions` used when parsing the response. `.allowFragments`
  489. /// by default.
  490. /// - completionHandler: A closure to be executed once the request has finished.
  491. ///
  492. /// - Returns: The request.
  493. @available(*, deprecated, message: "responseJSON deprecated and will be removed in Alamofire 6. Use responseDecodable instead.")
  494. @preconcurrency
  495. @discardableResult
  496. public func responseJSON(queue: DispatchQueue = .main,
  497. dataPreprocessor: any DataPreprocessor = JSONResponseSerializer.defaultDataPreprocessor,
  498. emptyResponseCodes: Set<Int> = JSONResponseSerializer.defaultEmptyResponseCodes,
  499. emptyRequestMethods: Set<HTTPMethod> = JSONResponseSerializer.defaultEmptyRequestMethods,
  500. options: JSONSerialization.ReadingOptions = .allowFragments,
  501. completionHandler: @escaping @Sendable (AFDownloadResponse<Any>) -> Void) -> Self {
  502. response(queue: queue,
  503. responseSerializer: JSONResponseSerializer(dataPreprocessor: dataPreprocessor,
  504. emptyResponseCodes: emptyResponseCodes,
  505. emptyRequestMethods: emptyRequestMethods,
  506. options: options),
  507. completionHandler: completionHandler)
  508. }
  509. /// Adds a handler using a `DecodableResponseSerializer` to be called once the request has finished.
  510. ///
  511. /// - Note: This handler will read the entire downloaded file into memory, use with caution.
  512. ///
  513. /// - Parameters:
  514. /// - type: `Decodable` type to decode from response data.
  515. /// - queue: The queue on which the completion handler is dispatched. `.main` by default.
  516. /// - dataPreprocessor: `DataPreprocessor` which processes the received `Data` before calling the
  517. /// `completionHandler`. `PassthroughPreprocessor()` by default.
  518. /// - decoder: `DataDecoder` to use to decode the response. `JSONDecoder()` by default.
  519. /// - emptyResponseCodes: HTTP status codes for which empty responses are always valid. `[204, 205]` by default.
  520. /// - emptyRequestMethods: `HTTPMethod`s for which empty responses are always valid. `[.head]` by default.
  521. /// - completionHandler: A closure to be executed once the request has finished.
  522. ///
  523. /// - Returns: The request.
  524. @preconcurrency
  525. @discardableResult
  526. public func responseDecodable<T: Decodable>(of type: T.Type = T.self,
  527. queue: DispatchQueue = .main,
  528. dataPreprocessor: any DataPreprocessor = DecodableResponseSerializer<T>.defaultDataPreprocessor,
  529. decoder: any DataDecoder = JSONDecoder(),
  530. emptyResponseCodes: Set<Int> = DecodableResponseSerializer<T>.defaultEmptyResponseCodes,
  531. emptyRequestMethods: Set<HTTPMethod> = DecodableResponseSerializer<T>.defaultEmptyRequestMethods,
  532. completionHandler: @escaping @Sendable (AFDownloadResponse<T>) -> Void) -> Self where T: Sendable {
  533. response(queue: queue,
  534. responseSerializer: DecodableResponseSerializer(dataPreprocessor: dataPreprocessor,
  535. decoder: decoder,
  536. emptyResponseCodes: emptyResponseCodes,
  537. emptyRequestMethods: emptyRequestMethods),
  538. completionHandler: completionHandler)
  539. }
  540. }