ResponseSerialization.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. //
  2. // ResponseSerialization.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. // MARK: Protocols
  26. /// The type to which all data response serializers must conform in order to serialize a response.
  27. public protocol DataResponseSerializerProtocol {
  28. /// The type of serialized object to be created by this serializer.
  29. associatedtype SerializedObject
  30. /// The function used to serialize the response data in response handlers.
  31. func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> SerializedObject
  32. }
  33. /// The type to which all download response serializers must conform in order to serialize a response.
  34. public protocol DownloadResponseSerializerProtocol {
  35. /// The type of serialized object to be created by this `DownloadResponseSerializerType`.
  36. associatedtype SerializedObject
  37. /// The function used to serialize the downloaded data in response handlers.
  38. func serializeDownload(request: URLRequest?, response: HTTPURLResponse?, fileURL: URL?, error: Error?) throws -> SerializedObject
  39. }
  40. /// A serializer that can handle both data and download responses.
  41. public typealias ResponseSerializer = DataResponseSerializerProtocol & DownloadResponseSerializerProtocol
  42. /// By default, any serializer declared to conform to both types will get file serialization for free, as it just feeds
  43. /// the data read from disk into the data response serializer.
  44. public extension DownloadResponseSerializerProtocol where Self: DataResponseSerializerProtocol {
  45. public func serializeDownload(request: URLRequest?, response: HTTPURLResponse?, fileURL: URL?, error: Error?) throws -> Self.SerializedObject {
  46. guard error == nil else { throw error! }
  47. guard let fileURL = fileURL else {
  48. throw AFError.responseSerializationFailed(reason: .inputFileNil)
  49. }
  50. let data: Data
  51. do {
  52. data = try Data(contentsOf: fileURL)
  53. } catch {
  54. throw AFError.responseSerializationFailed(reason: .inputFileReadFailed(at: fileURL))
  55. }
  56. do {
  57. return try serialize(request: request, response: response, data: data, error: error)
  58. } catch {
  59. throw error
  60. }
  61. }
  62. }
  63. // MARK: - AnyResponseSerializer
  64. /// A generic `ResponseSerializer` conforming type.
  65. public final class AnyResponseSerializer<Value>: ResponseSerializer {
  66. /// A closure which can be used to serialize data responses.
  67. public typealias DataSerializer = (_ request: URLRequest?, _ response: HTTPURLResponse?, _ data: Data?, _ error: Error?) throws -> Value
  68. /// A closure which can be used to serialize download reponses.
  69. public typealias DownloadSerializer = (_ request: URLRequest?, _ response: HTTPURLResponse?, _ fileURL: URL?, _ error: Error?) throws -> Value
  70. let dataSerializer: DataSerializer
  71. let downloadSerializer: DownloadSerializer?
  72. /// Initialze the instance with both a `DataSerializer` closure and a `DownloadSerializer` closure.
  73. ///
  74. /// - Parameters:
  75. /// - dataSerializer: A `DataSerializer` closure.
  76. /// - downloadSerializer: A `DownloadSerializer` closure.
  77. public init(dataSerializer: @escaping DataSerializer, downloadSerializer: @escaping DownloadSerializer) {
  78. self.dataSerializer = dataSerializer
  79. self.downloadSerializer = downloadSerializer
  80. }
  81. /// Initialze the instance with a `DataSerializer` closure. Download serialization will fallback to a default
  82. /// implementation.
  83. ///
  84. /// - Parameters:
  85. /// - dataSerializer: A `DataSerializer` closure.
  86. public init(dataSerializer: @escaping DataSerializer) {
  87. self.dataSerializer = dataSerializer
  88. self.downloadSerializer = nil
  89. }
  90. public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> Value {
  91. return try dataSerializer(request, response, data, error)
  92. }
  93. public func serializeDownload(request: URLRequest?, response: HTTPURLResponse?, fileURL: URL?, error: Error?) throws -> Value {
  94. return try downloadSerializer?(request, response, fileURL, error) ?? { (request, response, fileURL, error) in
  95. guard error == nil else { throw error! }
  96. guard let fileURL = fileURL else {
  97. throw AFError.responseSerializationFailed(reason: .inputFileNil)
  98. }
  99. let data: Data
  100. do {
  101. data = try Data(contentsOf: fileURL)
  102. } catch {
  103. throw AFError.responseSerializationFailed(reason: .inputFileReadFailed(at: fileURL))
  104. }
  105. do {
  106. return try serialize(request: request, response: response, data: data, error: error)
  107. } catch {
  108. throw error
  109. }
  110. }(request, response, fileURL, error)
  111. }
  112. }
  113. // MARK: - Timeline
  114. //extension Request {
  115. // var timeline: Timeline {
  116. // let requestStartTime = self.startTime ?? CFAbsoluteTimeGetCurrent()
  117. // let requestCompletedTime = self.endTime ?? CFAbsoluteTimeGetCurrent()
  118. // let initialResponseTime = self.delegate.initialResponseTime ?? requestCompletedTime
  119. //
  120. // return Timeline(
  121. // requestStartTime: requestStartTime,
  122. // initialResponseTime: initialResponseTime,
  123. // requestCompletedTime: requestCompletedTime,
  124. // serializationCompletedTime: CFAbsoluteTimeGetCurrent()
  125. // )
  126. // }
  127. //}
  128. // MARK: - Default
  129. extension DataRequest {
  130. /// Adds a handler to be called once the request has finished.
  131. ///
  132. /// - Parameters:
  133. /// - queue: The queue on which the completion handler is dispatched. Defaults to `nil`, which means
  134. /// the handler is called on `.main`.
  135. /// - completionHandler: The code to be executed once the request has finished.
  136. /// - Returns: The request.
  137. @discardableResult
  138. public func response(queue: DispatchQueue? = nil, completionHandler: @escaping (DataResponse<Data?>) -> Void) -> Self {
  139. internalQueue.addOperation {
  140. // TODO: Use internal serialization queue?
  141. let result = Result(value: self.data, error: self.error)
  142. let response = DataResponse(request: self.request,
  143. response: self.response,
  144. data: self.data,
  145. metrics: self.metrics,
  146. result: result)
  147. (queue ?? .main).async { completionHandler(response) }
  148. }
  149. return self
  150. }
  151. /// Adds a handler to be called once the request has finished.
  152. ///
  153. /// - Parameters:
  154. /// - queue: The queue on which the completion handler is dispatched. Defaults to `nil`, which means
  155. /// the handler is called on `.main`.
  156. /// - responseSerializer: The response serializer responsible for serializing the request, response, and data.
  157. /// - completionHandler: The code to be executed once the request has finished.
  158. /// - Returns: The request.
  159. @discardableResult
  160. public func response<Serializer: DataResponseSerializerProtocol>(
  161. queue: DispatchQueue? = nil,
  162. responseSerializer: Serializer,
  163. completionHandler: @escaping (DataResponse<Serializer.SerializedObject>) -> Void)
  164. -> Self
  165. {
  166. internalQueue.addOperation {
  167. // TODO: Use internal serialization queue?
  168. let result = Result { try responseSerializer.serialize(request: self.request,
  169. response: self.response,
  170. data: self.data,
  171. error: self.error) }
  172. let response = DataResponse(request: self.request,
  173. response: self.response,
  174. data: self.data,
  175. metrics: self.metrics,
  176. result: result)
  177. (queue ?? .main).async { completionHandler(response) }
  178. }
  179. return self
  180. }
  181. }
  182. //extension DownloadRequest {
  183. // /// Adds a handler to be called once the request has finished.
  184. // ///
  185. // /// - Parameters:
  186. // /// - queue: The queue on which the completion handler is dispatched. Defaults to `nil`, which means
  187. // /// the handler is called on `.main`.
  188. // /// - completionHandler: The code to be executed once the request has finished.
  189. // /// - Returns: The request.
  190. // @discardableResult
  191. // public func response(
  192. // queue: DispatchQueue? = nil,
  193. // completionHandler: @escaping (DefaultDownloadResponse) -> Void)
  194. // -> Self
  195. // {
  196. // delegate.queue.addOperation {
  197. // (queue ?? .main).async {
  198. // var downloadResponse = DefaultDownloadResponse(
  199. // request: self.request,
  200. // response: self.response,
  201. // temporaryURL: self.downloadDelegate.temporaryURL,
  202. // destinationURL: self.downloadDelegate.destinationURL,
  203. // resumeData: self.downloadDelegate.resumeData,
  204. // error: self.downloadDelegate.error,
  205. // timeline: self.timeline
  206. // )
  207. //
  208. // downloadResponse.add(self.delegate.metrics)
  209. //
  210. // completionHandler(downloadResponse)
  211. // }
  212. // }
  213. //
  214. // return self
  215. // }
  216. //
  217. // /// Adds a handler to be called once the request has finished.
  218. // ///
  219. // /// - Parameters:
  220. // /// - queue: The queue on which the completion handler is dispatched. Defaults to `nil`, which means
  221. // /// the handler is called on `.main`.
  222. // /// - responseSerializer: The response serializer responsible for serializing the request, response, and data
  223. // /// contained in the destination url.
  224. // /// - completionHandler: The code to be executed once the request has finished.
  225. // /// - Returns: The request.
  226. // @discardableResult
  227. // public func response<T: DownloadResponseSerializerProtocol>(
  228. // queue: DispatchQueue? = nil,
  229. // responseSerializer: T,
  230. // completionHandler: @escaping (DownloadResponse<T.SerializedObject>) -> Void)
  231. // -> Self
  232. // {
  233. // delegate.queue.addOperation {
  234. // let result = Result { try responseSerializer.serializeDownload(request: self.request,
  235. // response: self.response,
  236. // fileURL: self.downloadDelegate.fileURL,
  237. // error: self.downloadDelegate.error) }
  238. // var downloadResponse = DownloadResponse<T.SerializedObject>(
  239. // request: self.request,
  240. // response: self.response,
  241. // temporaryURL: self.downloadDelegate.temporaryURL,
  242. // destinationURL: self.downloadDelegate.destinationURL,
  243. // resumeData: self.downloadDelegate.resumeData,
  244. // result: result,
  245. // timeline: self.timeline
  246. // )
  247. //
  248. // downloadResponse.add(self.delegate.metrics)
  249. //
  250. // (queue ?? .main).async { completionHandler(downloadResponse) }
  251. // }
  252. //
  253. // return self
  254. // }
  255. //}
  256. // MARK: - Data
  257. extension DataRequest {
  258. /// Adds a handler to be called once the request has finished.
  259. ///
  260. /// - Parameters:
  261. /// - queue: The queue on which the completion handler is dispatched. Defaults to `nil`, which means
  262. /// the handler is called on `.main`.
  263. /// - completionHandler: The code to be executed once the request has finished.
  264. /// - Returns: The request.
  265. @discardableResult
  266. public func responseData(
  267. queue: DispatchQueue? = nil,
  268. completionHandler: @escaping (DataResponse<Data>) -> Void)
  269. -> Self
  270. {
  271. return response(queue: queue,
  272. responseSerializer: DataResponseSerializer(),
  273. completionHandler: completionHandler)
  274. }
  275. }
  276. /// A `ResponseSerializer` that performs minimal reponse checking and returns any response data as-is. By default, a
  277. /// request returning `nil` or no data is considered an error. However, if the response is has a status code valid for
  278. /// empty responses (`204`, `205`), then an empty `Data` value is returned.
  279. public final class DataResponseSerializer: ResponseSerializer {
  280. public init() { }
  281. public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> Data {
  282. guard error == nil else { throw error! }
  283. if let response = response, emptyDataStatusCodes.contains(response.statusCode) { return Data() }
  284. guard let validData = data else {
  285. throw AFError.responseSerializationFailed(reason: .inputDataNil)
  286. }
  287. return validData
  288. }
  289. }
  290. //extension DownloadRequest {
  291. // /// Adds a handler to be called once the request has finished.
  292. // ///
  293. // /// - Parameters:
  294. // /// - queue: The queue on which the completion handler is dispatched. Defaults to `nil`, which means
  295. // /// the handler is called on `.main`.
  296. // /// - completionHandler: The code to be executed once the request has finished.
  297. // /// - Returns: The request.
  298. // @discardableResult
  299. // public func responseData(
  300. // queue: DispatchQueue? = nil,
  301. // completionHandler: @escaping (DownloadResponse<Data>) -> Void)
  302. // -> Self
  303. // {
  304. // return response(
  305. // queue: queue,
  306. // responseSerializer: DataResponseSerializer(),
  307. // completionHandler: completionHandler
  308. // )
  309. // }
  310. //}
  311. // MARK: - String
  312. /// A `ResponseSerializer` that decodes the response data as a `String`. By default, a request returning `nil` or no
  313. /// data is considered an error. However, if the response is has a status code valid for empty responses (`204`, `205`),
  314. /// then an empty `String` is returned.
  315. public final class StringResponseSerializer: ResponseSerializer {
  316. let encoding: String.Encoding?
  317. /// Creates an instance with the given `String.Encoding`.
  318. ///
  319. /// - Parameter encoding: A string encoding. Defaults to `nil`, in which case the encoding will be determined from
  320. /// the server response, falling back to the default HTTP character set, `ISO-8859-1`.
  321. public init(encoding: String.Encoding? = nil) {
  322. self.encoding = encoding
  323. }
  324. public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> String {
  325. guard error == nil else { throw error! }
  326. if let response = response, emptyDataStatusCodes.contains(response.statusCode) { return "" }
  327. guard let validData = data else {
  328. throw AFError.responseSerializationFailed(reason: .inputDataNil)
  329. }
  330. var convertedEncoding = encoding
  331. if let encodingName = response?.textEncodingName as CFString?, convertedEncoding == nil {
  332. let ianaCharSet = CFStringConvertIANACharSetNameToEncoding(encodingName)
  333. let nsStringEncoding = CFStringConvertEncodingToNSStringEncoding(ianaCharSet)
  334. convertedEncoding = String.Encoding(rawValue: nsStringEncoding)
  335. }
  336. let actualEncoding = convertedEncoding ?? .isoLatin1
  337. guard let string = String(data: validData, encoding: actualEncoding) else {
  338. throw AFError.responseSerializationFailed(reason: .stringSerializationFailed(encoding: actualEncoding))
  339. }
  340. return string
  341. }
  342. }
  343. extension DataRequest {
  344. /// Adds a handler to be called once the request has finished.
  345. ///
  346. /// - Parameters:
  347. /// - queue: The queue on which the completion handler is dispatched. Defaults to `nil`, which means
  348. /// the handler is called on `.main`.
  349. /// - encoding: The string encoding. Defaults to `nil`, in which case the encoding will be determined from
  350. /// the server response, falling back to the default HTTP character set, `ISO-8859-1`.
  351. /// - completionHandler: A closure to be executed once the request has finished.
  352. /// - Returns: The request.
  353. @discardableResult
  354. public func responseString(queue: DispatchQueue? = nil,
  355. encoding: String.Encoding? = nil,
  356. completionHandler: @escaping (DataResponse<String>) -> Void) -> Self {
  357. return response(queue: queue,
  358. responseSerializer: StringResponseSerializer(encoding: encoding),
  359. completionHandler: completionHandler)
  360. }
  361. }
  362. //extension DownloadRequest {
  363. // /// Adds a handler to be called once the request has finished.
  364. // ///
  365. // /// - Parameters:
  366. // /// - queue: The queue on which the completion handler is dispatched. Defaults to `nil`, which means
  367. // /// the handler is called on `.main`.
  368. // /// - encoding: The string encoding. Defaults to `nil`, in which case the encoding will be determined from
  369. // /// the server response, falling back to the default HTTP character set, `ISO-8859-1`.
  370. // /// - completionHandler: A closure to be executed once the request has finished.
  371. // /// - Returns: The request.
  372. // @discardableResult
  373. // public func responseString(
  374. // queue: DispatchQueue? = nil,
  375. // encoding: String.Encoding? = nil,
  376. // completionHandler: @escaping (DownloadResponse<String>) -> Void)
  377. // -> Self
  378. // {
  379. // return response(
  380. // queue: queue,
  381. // responseSerializer: StringResponseSerializer(encoding: encoding),
  382. // completionHandler: completionHandler
  383. // )
  384. // }
  385. //}
  386. // MARK: - JSON
  387. /// A `ResponseSerializer` that decodes the response data using `JSONSerialization`. By default, a request returning
  388. /// `nil` or no data is considered an error. However, if the response is has a status code valid for empty responses
  389. /// (`204`, `205`), then an `NSNull` value is returned.
  390. public final class JSONResponseSerializer: ResponseSerializer {
  391. let options: JSONSerialization.ReadingOptions
  392. /// Creates an instance with the given `JSONSerilization.ReadingOptions`.
  393. ///
  394. /// - Parameter options: The options to use. Defaults to `.allowFragments`.
  395. public init(options: JSONSerialization.ReadingOptions = .allowFragments) {
  396. self.options = options
  397. }
  398. public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> Any {
  399. guard error == nil else { throw error! }
  400. guard let validData = data, validData.count > 0 else {
  401. if let response = response, emptyDataStatusCodes.contains(response.statusCode) {
  402. return NSNull()
  403. }
  404. throw AFError.responseSerializationFailed(reason: .inputDataNilOrZeroLength)
  405. }
  406. do {
  407. return try JSONSerialization.jsonObject(with: validData, options: options)
  408. } catch {
  409. throw AFError.responseSerializationFailed(reason: .jsonSerializationFailed(error: error))
  410. }
  411. }
  412. }
  413. extension DataRequest {
  414. /// Adds a handler to be called once the request has finished.
  415. ///
  416. /// - Parameters:
  417. /// - queue: The queue on which the completion handler is dispatched. Defaults to `nil`, which means
  418. /// the handler is called on `.main`.
  419. /// - options: The JSON serialization reading options. Defaults to `.allowFragments`.
  420. /// - completionHandler: A closure to be executed once the request has finished.
  421. /// - Returns: The request.
  422. @discardableResult
  423. public func responseJSON(queue: DispatchQueue? = nil,
  424. options: JSONSerialization.ReadingOptions = .allowFragments,
  425. completionHandler: @escaping (DataResponse<Any>) -> Void) -> Self {
  426. return response(queue: queue,
  427. responseSerializer: JSONResponseSerializer(options: options),
  428. completionHandler: completionHandler)
  429. }
  430. }
  431. //extension DownloadRequest {
  432. // /// Adds a handler to be called once the request has finished.
  433. // ///
  434. // /// - Parameters:
  435. // /// - queue: The queue on which the completion handler is dispatched. Defaults to `nil`, which means
  436. // /// the handler is called on `.main`.
  437. // /// - options: The JSON serialization reading options. Defaults to `.allowFragments`.
  438. // /// - completionHandler: A closure to be executed once the request has finished.
  439. // /// - Returns: The request.
  440. // @discardableResult
  441. // public func responseJSON(
  442. // queue: DispatchQueue? = nil,
  443. // options: JSONSerialization.ReadingOptions = .allowFragments,
  444. // completionHandler: @escaping (DownloadResponse<Any>) -> Void)
  445. // -> Self
  446. // {
  447. // return response(queue: queue,
  448. // responseSerializer: JSONResponseSerializer(options: options),
  449. // completionHandler: completionHandler)
  450. // }
  451. //}
  452. // MARK: - Empty
  453. /// A type representing an empty response. Use `Empty.response` to get the instance.
  454. public struct Empty: Decodable {
  455. public static let response = Empty()
  456. }
  457. // MARK: - JSON Decodable
  458. /// A `ResponseSerializer` that decodes the response data as a generic value using a `JSONDecoder`. By default, a
  459. /// request returning `nil` or no data is considered an error. However, if the response is has a status code valid for
  460. /// empty responses (`204`, `205`), then the `Empty.response` value is returned.
  461. public final class JSONDecodableResponseSerializer<T: Decodable>: ResponseSerializer {
  462. let decoder: JSONDecoder
  463. /// Creates an instance with the given `JSONDecoder` instance.
  464. ///
  465. /// - Parameter decoder: A decoder. Defaults to a `JSONDecoder` with default settings.
  466. public init(decoder: JSONDecoder = JSONDecoder()) {
  467. self.decoder = decoder
  468. }
  469. public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> T {
  470. guard error == nil else { throw error! }
  471. guard let validData = data, validData.count > 0 else {
  472. if let response = response, emptyDataStatusCodes.contains(response.statusCode) {
  473. guard let emptyResponse = Empty.response as? T else {
  474. throw AFError.responseSerializationFailed(reason: .invalidEmptyResponse(type: "\(T.self)"))
  475. }
  476. return emptyResponse
  477. }
  478. throw AFError.responseSerializationFailed(reason: .inputDataNilOrZeroLength)
  479. }
  480. do {
  481. return try decoder.decode(T.self, from: validData)
  482. } catch {
  483. throw error
  484. }
  485. }
  486. }
  487. extension DataRequest {
  488. /// Adds a handler to be called once the request has finished.
  489. ///
  490. /// - Parameters:
  491. /// - queue: The queue on which the completion handler is dispatched. Defaults to `nil`, which means
  492. /// the handler is called on `.main`.
  493. /// - decoder: The decoder to use to decode the response. Defaults to a `JSONDecoder` with default
  494. /// settings.
  495. /// - completionHandler: A closure to be executed once the request has finished.
  496. /// - Returns: The request.
  497. @discardableResult
  498. public func responseJSONDecodable<T: Decodable>(queue: DispatchQueue? = nil,
  499. decoder: JSONDecoder = JSONDecoder(),
  500. completionHandler: @escaping (DataResponse<T>) -> Void) -> Self {
  501. return response(queue: queue,
  502. responseSerializer: JSONDecodableResponseSerializer(decoder: decoder),
  503. completionHandler: completionHandler)
  504. }
  505. }
  506. /// A set of HTTP response status code that do not contain response data.
  507. private let emptyDataStatusCodes: Set<Int> = [204, 205]