|
@@ -1,7 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -24,80 +24,144 @@
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
public protocol DataResponseSerializerProtocol {
|
|
|
-
|
|
|
+
|
|
|
associatedtype SerializedObject
|
|
|
|
|
|
-
|
|
|
- var serializeResponse: (URLRequest?, HTTPURLResponse?, Data?, Error?) -> Result<SerializedObject> { get }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> SerializedObject
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-public struct DataResponseSerializer<Value>: DataResponseSerializerProtocol {
|
|
|
-
|
|
|
- public typealias SerializedObject = Value
|
|
|
-
|
|
|
-
|
|
|
- public var serializeResponse: (URLRequest?, HTTPURLResponse?, Data?, Error?) -> Result<Value>
|
|
|
+
|
|
|
+public protocol DownloadResponseSerializerProtocol {
|
|
|
+
|
|
|
+ associatedtype SerializedObject
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
- public init(serializeResponse: @escaping (URLRequest?, HTTPURLResponse?, Data?, Error?) -> Result<Value>) {
|
|
|
- self.serializeResponse = serializeResponse
|
|
|
- }
|
|
|
+
|
|
|
+
|
|
|
+ func serializeDownload(request: URLRequest?, response: HTTPURLResponse?, fileURL: URL?, error: Error?) throws -> SerializedObject
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+public protocol ResponseSerializer: DataResponseSerializerProtocol & DownloadResponseSerializerProtocol {
|
|
|
+
|
|
|
+ var dataPreprocessor: DataPreprocessor { get }
|
|
|
+
|
|
|
+ var emptyRequestMethods: Set<HTTPMethod> { get }
|
|
|
+
|
|
|
+ var emptyResponseCodes: Set<Int> { get }
|
|
|
+}
|
|
|
|
|
|
-
|
|
|
-public protocol DownloadResponseSerializerProtocol {
|
|
|
-
|
|
|
- associatedtype SerializedObject
|
|
|
+
|
|
|
+public protocol DataPreprocessor {
|
|
|
+
|
|
|
+
|
|
|
+ func preprocess(_ data: Data) throws -> Data
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+public struct PassthroughPreprocessor: DataPreprocessor {
|
|
|
+ public init() {}
|
|
|
+
|
|
|
+ public func preprocess(_ data: Data) throws -> Data { data }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+public struct GoogleXSSIPreprocessor: DataPreprocessor {
|
|
|
+ public init() {}
|
|
|
|
|
|
-
|
|
|
- var serializeResponse: (URLRequest?, HTTPURLResponse?, URL?, Error?) -> Result<SerializedObject> { get }
|
|
|
+ public func preprocess(_ data: Data) throws -> Data {
|
|
|
+ (data.prefix(6) == Data(")]}',\n".utf8)) ? data.dropFirst(6) : data
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+extension ResponseSerializer {
|
|
|
+
|
|
|
+ public static var defaultDataPreprocessor: DataPreprocessor { PassthroughPreprocessor() }
|
|
|
+
|
|
|
+ public static var defaultEmptyRequestMethods: Set<HTTPMethod> { [.head] }
|
|
|
+
|
|
|
+ public static var defaultEmptyResponseCodes: Set<Int> { [204, 205] }
|
|
|
|
|
|
-
|
|
|
-public struct DownloadResponseSerializer<Value>: DownloadResponseSerializerProtocol {
|
|
|
-
|
|
|
- public typealias SerializedObject = Value
|
|
|
+ public var dataPreprocessor: DataPreprocessor { Self.defaultDataPreprocessor }
|
|
|
+ public var emptyRequestMethods: Set<HTTPMethod> { Self.defaultEmptyRequestMethods }
|
|
|
+ public var emptyResponseCodes: Set<Int> { Self.defaultEmptyResponseCodes }
|
|
|
|
|
|
-
|
|
|
- public var serializeResponse: (URLRequest?, HTTPURLResponse?, URL?, Error?) -> Result<Value>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public func requestAllowsEmptyResponseData(_ request: URLRequest?) -> Bool? {
|
|
|
+ request.flatMap { $0.httpMethod }
|
|
|
+ .flatMap(HTTPMethod.init)
|
|
|
+ .map { emptyRequestMethods.contains($0) }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public func responseAllowsEmptyResponseData(_ response: HTTPURLResponse?) -> Bool? {
|
|
|
+ response.flatMap { $0.statusCode }
|
|
|
+ .map { emptyResponseCodes.contains($0) }
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
- public init(serializeResponse: @escaping (URLRequest?, HTTPURLResponse?, URL?, Error?) -> Result<Value>) {
|
|
|
- self.serializeResponse = serializeResponse
|
|
|
+
|
|
|
+ public func emptyResponseAllowed(forRequest request: URLRequest?, response: HTTPURLResponse?) -> Bool {
|
|
|
+ (requestAllowsEmptyResponseData(request) == true) || (responseAllowsEmptyResponseData(response) == true)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+extension DownloadResponseSerializerProtocol where Self: DataResponseSerializerProtocol {
|
|
|
+ public func serializeDownload(request: URLRequest?, response: HTTPURLResponse?, fileURL: URL?, error: Error?) throws -> Self.SerializedObject {
|
|
|
+ guard error == nil else { throw error! }
|
|
|
|
|
|
-extension Request {
|
|
|
- var timeline: Timeline {
|
|
|
- let requestStartTime = self.startTime ?? CFAbsoluteTimeGetCurrent()
|
|
|
- let requestCompletedTime = self.endTime ?? CFAbsoluteTimeGetCurrent()
|
|
|
- let initialResponseTime = self.delegate.initialResponseTime ?? requestCompletedTime
|
|
|
+ guard let fileURL = fileURL else {
|
|
|
+ throw AFError.responseSerializationFailed(reason: .inputFileNil)
|
|
|
+ }
|
|
|
|
|
|
- return Timeline(
|
|
|
- requestStartTime: requestStartTime,
|
|
|
- initialResponseTime: initialResponseTime,
|
|
|
- requestCompletedTime: requestCompletedTime,
|
|
|
- serializationCompletedTime: CFAbsoluteTimeGetCurrent()
|
|
|
- )
|
|
|
+ let data: Data
|
|
|
+ do {
|
|
|
+ data = try Data(contentsOf: fileURL)
|
|
|
+ } catch {
|
|
|
+ throw AFError.responseSerializationFailed(reason: .inputFileReadFailed(at: fileURL))
|
|
|
+ }
|
|
|
+
|
|
|
+ do {
|
|
|
+ return try serialize(request: request, response: response, data: data, error: error)
|
|
|
+ } catch {
|
|
|
+ throw error
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -106,25 +170,29 @@ extension Request {
|
|
|
extension DataRequest {
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
@discardableResult
|
|
|
- public func response(queue: DispatchQueue? = nil, completionHandler: @escaping (DefaultDataResponse) -> Void) -> Self {
|
|
|
- delegate.queue.addOperation {
|
|
|
- (queue ?? DispatchQueue.main).async {
|
|
|
- var dataResponse = DefaultDataResponse(
|
|
|
- request: self.request,
|
|
|
- response: self.response,
|
|
|
- data: self.delegate.data,
|
|
|
- error: self.delegate.error,
|
|
|
- timeline: self.timeline
|
|
|
- )
|
|
|
-
|
|
|
- dataResponse.add(self.delegate.metrics)
|
|
|
-
|
|
|
- completionHandler(dataResponse)
|
|
|
+ public func response(queue: DispatchQueue = .main, completionHandler: @escaping (AFDataResponse<Data?>) -> Void) -> Self {
|
|
|
+ appendResponseSerializer {
|
|
|
+
|
|
|
+ let result = AFResult<Data?>(value: self.data, error: self.error)
|
|
|
+
|
|
|
+
|
|
|
+ self.underlyingQueue.async {
|
|
|
+ let response = DataResponse(request: self.request,
|
|
|
+ response: self.response,
|
|
|
+ data: self.data,
|
|
|
+ metrics: self.metrics,
|
|
|
+ serializationDuration: 0,
|
|
|
+ result: result)
|
|
|
+
|
|
|
+ self.eventMonitor?.request(self, didParseResponse: response)
|
|
|
+
|
|
|
+ self.responseSerializerDidComplete { queue.async { completionHandler(response) } }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -133,38 +201,77 @@ extension DataRequest {
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
@discardableResult
|
|
|
- public func response<T: DataResponseSerializerProtocol>(
|
|
|
- queue: DispatchQueue? = nil,
|
|
|
- responseSerializer: T,
|
|
|
- completionHandler: @escaping (DataResponse<T.SerializedObject>) -> Void)
|
|
|
- -> Self
|
|
|
- {
|
|
|
- delegate.queue.addOperation {
|
|
|
- let result = responseSerializer.serializeResponse(
|
|
|
- self.request,
|
|
|
- self.response,
|
|
|
- self.delegate.data,
|
|
|
- self.delegate.error
|
|
|
- )
|
|
|
-
|
|
|
- var dataResponse = DataResponse<T.SerializedObject>(
|
|
|
- request: self.request,
|
|
|
- response: self.response,
|
|
|
- data: self.delegate.data,
|
|
|
- result: result,
|
|
|
- timeline: self.timeline
|
|
|
- )
|
|
|
-
|
|
|
- dataResponse.add(self.delegate.metrics)
|
|
|
-
|
|
|
- (queue ?? DispatchQueue.main).async { completionHandler(dataResponse) }
|
|
|
+ public func response<Serializer: DataResponseSerializerProtocol>(queue: DispatchQueue = .main,
|
|
|
+ responseSerializer: Serializer,
|
|
|
+ completionHandler: @escaping (AFDataResponse<Serializer.SerializedObject>) -> Void)
|
|
|
+ -> Self {
|
|
|
+ appendResponseSerializer {
|
|
|
+
|
|
|
+ let start = ProcessInfo.processInfo.systemUptime
|
|
|
+ let result: AFResult<Serializer.SerializedObject> = Result {
|
|
|
+ try responseSerializer.serialize(request: self.request,
|
|
|
+ response: self.response,
|
|
|
+ data: self.data,
|
|
|
+ error: self.error)
|
|
|
+ }.mapError { error in
|
|
|
+ error.asAFError(or: .responseSerializationFailed(reason: .customSerializationFailed(error: error)))
|
|
|
+ }
|
|
|
+
|
|
|
+ let end = ProcessInfo.processInfo.systemUptime
|
|
|
+
|
|
|
+
|
|
|
+ self.underlyingQueue.async {
|
|
|
+ let response = DataResponse(request: self.request,
|
|
|
+ response: self.response,
|
|
|
+ data: self.data,
|
|
|
+ metrics: self.metrics,
|
|
|
+ serializationDuration: end - start,
|
|
|
+ result: result)
|
|
|
+
|
|
|
+ self.eventMonitor?.request(self, didParseResponse: response)
|
|
|
+
|
|
|
+ guard let serializerError = result.failure, let delegate = self.delegate else {
|
|
|
+ self.responseSerializerDidComplete { queue.async { completionHandler(response) } }
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ delegate.retryResult(for: self, dueTo: serializerError) { retryResult in
|
|
|
+ var didComplete: (() -> Void)?
|
|
|
+
|
|
|
+ defer {
|
|
|
+ if let didComplete = didComplete {
|
|
|
+ self.responseSerializerDidComplete { queue.async { didComplete() } }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ switch retryResult {
|
|
|
+ case .doNotRetry:
|
|
|
+ didComplete = { completionHandler(response) }
|
|
|
+
|
|
|
+ case let .doNotRetryWithError(retryError):
|
|
|
+ let result: AFResult<Serializer.SerializedObject> = .failure(retryError.asAFError(orFailWith: "Received retryError was not already AFError"))
|
|
|
+
|
|
|
+ let response = DataResponse(request: self.request,
|
|
|
+ response: self.response,
|
|
|
+ data: self.data,
|
|
|
+ metrics: self.metrics,
|
|
|
+ serializationDuration: end - start,
|
|
|
+ result: result)
|
|
|
+
|
|
|
+ didComplete = { completionHandler(response) }
|
|
|
+
|
|
|
+ case .retry, .retryWithDelay:
|
|
|
+ delegate.retryRequest(self, withDelay: retryResult.delay)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return self
|
|
@@ -174,31 +281,32 @@ extension DataRequest {
|
|
|
extension DownloadRequest {
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
@discardableResult
|
|
|
- public func response(
|
|
|
- queue: DispatchQueue? = nil,
|
|
|
- completionHandler: @escaping (DefaultDownloadResponse) -> Void)
|
|
|
- -> Self
|
|
|
- {
|
|
|
- delegate.queue.addOperation {
|
|
|
- (queue ?? DispatchQueue.main).async {
|
|
|
- var downloadResponse = DefaultDownloadResponse(
|
|
|
- request: self.request,
|
|
|
- response: self.response,
|
|
|
- temporaryURL: self.downloadDelegate.temporaryURL,
|
|
|
- destinationURL: self.downloadDelegate.destinationURL,
|
|
|
- resumeData: self.downloadDelegate.resumeData,
|
|
|
- error: self.downloadDelegate.error,
|
|
|
- timeline: self.timeline
|
|
|
- )
|
|
|
-
|
|
|
- downloadResponse.add(self.delegate.metrics)
|
|
|
-
|
|
|
- completionHandler(downloadResponse)
|
|
|
+ public func response(queue: DispatchQueue = .main,
|
|
|
+ completionHandler: @escaping (AFDownloadResponse<URL?>) -> Void)
|
|
|
+ -> Self {
|
|
|
+ appendResponseSerializer {
|
|
|
+
|
|
|
+ let result = AFResult<URL?>(value: self.fileURL, error: self.error)
|
|
|
+
|
|
|
+
|
|
|
+ self.underlyingQueue.async {
|
|
|
+ let response = DownloadResponse(request: self.request,
|
|
|
+ response: self.response,
|
|
|
+ fileURL: self.fileURL,
|
|
|
+ resumeData: self.resumeData,
|
|
|
+ metrics: self.metrics,
|
|
|
+ serializationDuration: 0,
|
|
|
+ result: result)
|
|
|
+
|
|
|
+ self.eventMonitor?.request(self, didParseResponse: response)
|
|
|
+
|
|
|
+ self.responseSerializerDidComplete { queue.async { completionHandler(response) } }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -207,509 +315,802 @@ extension DownloadRequest {
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
@discardableResult
|
|
|
- public func response<T: DownloadResponseSerializerProtocol>(
|
|
|
- queue: DispatchQueue? = nil,
|
|
|
- responseSerializer: T,
|
|
|
- completionHandler: @escaping (DownloadResponse<T.SerializedObject>) -> Void)
|
|
|
- -> Self
|
|
|
- {
|
|
|
- delegate.queue.addOperation {
|
|
|
- let result = responseSerializer.serializeResponse(
|
|
|
- self.request,
|
|
|
- self.response,
|
|
|
- self.downloadDelegate.fileURL,
|
|
|
- self.downloadDelegate.error
|
|
|
- )
|
|
|
-
|
|
|
- var downloadResponse = DownloadResponse<T.SerializedObject>(
|
|
|
- request: self.request,
|
|
|
- response: self.response,
|
|
|
- temporaryURL: self.downloadDelegate.temporaryURL,
|
|
|
- destinationURL: self.downloadDelegate.destinationURL,
|
|
|
- resumeData: self.downloadDelegate.resumeData,
|
|
|
- result: result,
|
|
|
- timeline: self.timeline
|
|
|
- )
|
|
|
-
|
|
|
- downloadResponse.add(self.delegate.metrics)
|
|
|
-
|
|
|
- (queue ?? DispatchQueue.main).async { completionHandler(downloadResponse) }
|
|
|
+ public func response<Serializer: DownloadResponseSerializerProtocol>(queue: DispatchQueue = .main,
|
|
|
+ responseSerializer: Serializer,
|
|
|
+ completionHandler: @escaping (AFDownloadResponse<Serializer.SerializedObject>) -> Void)
|
|
|
+ -> Self {
|
|
|
+ appendResponseSerializer {
|
|
|
+
|
|
|
+ let start = ProcessInfo.processInfo.systemUptime
|
|
|
+ let result: AFResult<Serializer.SerializedObject> = Result {
|
|
|
+ try responseSerializer.serializeDownload(request: self.request,
|
|
|
+ response: self.response,
|
|
|
+ fileURL: self.fileURL,
|
|
|
+ error: self.error)
|
|
|
+ }.mapError { error in
|
|
|
+ error.asAFError(or: .responseSerializationFailed(reason: .customSerializationFailed(error: error)))
|
|
|
+ }
|
|
|
+ let end = ProcessInfo.processInfo.systemUptime
|
|
|
+
|
|
|
+
|
|
|
+ self.underlyingQueue.async {
|
|
|
+ let response = DownloadResponse(request: self.request,
|
|
|
+ response: self.response,
|
|
|
+ fileURL: self.fileURL,
|
|
|
+ resumeData: self.resumeData,
|
|
|
+ metrics: self.metrics,
|
|
|
+ serializationDuration: end - start,
|
|
|
+ result: result)
|
|
|
+
|
|
|
+ self.eventMonitor?.request(self, didParseResponse: response)
|
|
|
+
|
|
|
+ guard let serializerError = result.failure, let delegate = self.delegate else {
|
|
|
+ self.responseSerializerDidComplete { queue.async { completionHandler(response) } }
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ delegate.retryResult(for: self, dueTo: serializerError) { retryResult in
|
|
|
+ var didComplete: (() -> Void)?
|
|
|
+
|
|
|
+ defer {
|
|
|
+ if let didComplete = didComplete {
|
|
|
+ self.responseSerializerDidComplete { queue.async { didComplete() } }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ switch retryResult {
|
|
|
+ case .doNotRetry:
|
|
|
+ didComplete = { completionHandler(response) }
|
|
|
+
|
|
|
+ case let .doNotRetryWithError(retryError):
|
|
|
+ let result: AFResult<Serializer.SerializedObject> = .failure(retryError.asAFError(orFailWith: "Received retryError was not already AFError"))
|
|
|
+
|
|
|
+ let response = DownloadResponse(request: self.request,
|
|
|
+ response: self.response,
|
|
|
+ fileURL: self.fileURL,
|
|
|
+ resumeData: self.resumeData,
|
|
|
+ metrics: self.metrics,
|
|
|
+ serializationDuration: end - start,
|
|
|
+ result: result)
|
|
|
+
|
|
|
+ didComplete = { completionHandler(response) }
|
|
|
+
|
|
|
+ case .retry, .retryWithDelay:
|
|
|
+ delegate.retryRequest(self, withDelay: retryResult.delay)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return self
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
-extension Request {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public static func serializeResponseData(response: HTTPURLResponse?, data: Data?, error: Error?) -> Result<Data> {
|
|
|
- guard error == nil else { return .failure(error!) }
|
|
|
+
|
|
|
+
|
|
|
+public struct URLResponseSerializer: DownloadResponseSerializerProtocol {
|
|
|
+
|
|
|
+ public init() {}
|
|
|
|
|
|
- if let response = response, emptyDataStatusCodes.contains(response.statusCode) { return .success(Data()) }
|
|
|
+ public func serializeDownload(request: URLRequest?,
|
|
|
+ response: HTTPURLResponse?,
|
|
|
+ fileURL: URL?,
|
|
|
+ error: Error?) throws -> URL {
|
|
|
+ guard error == nil else { throw error! }
|
|
|
|
|
|
- guard let validData = data else {
|
|
|
- return .failure(AFError.responseSerializationFailed(reason: .inputDataNil))
|
|
|
+ guard let url = fileURL else {
|
|
|
+ throw AFError.responseSerializationFailed(reason: .inputFileNil)
|
|
|
}
|
|
|
|
|
|
- return .success(validData)
|
|
|
+ return url
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-extension DataRequest {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public static func dataResponseSerializer() -> DataResponseSerializer<Data> {
|
|
|
- return DataResponseSerializer { _, response, data, error in
|
|
|
- return Request.serializeResponseData(response: response, data: data, error: error)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
+extension DownloadRequest {
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
@discardableResult
|
|
|
- public func responseData(
|
|
|
- queue: DispatchQueue? = nil,
|
|
|
- completionHandler: @escaping (DataResponse<Data>) -> Void)
|
|
|
- -> Self
|
|
|
- {
|
|
|
- return response(
|
|
|
- queue: queue,
|
|
|
- responseSerializer: DataRequest.dataResponseSerializer(),
|
|
|
- completionHandler: completionHandler
|
|
|
- )
|
|
|
+ public func responseURL(queue: DispatchQueue = .main,
|
|
|
+ completionHandler: @escaping (AFDownloadResponse<URL>) -> Void) -> Self {
|
|
|
+ response(queue: queue, responseSerializer: URLResponseSerializer(), completionHandler: completionHandler)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-extension DownloadRequest {
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+public final class DataResponseSerializer: ResponseSerializer {
|
|
|
+ public let dataPreprocessor: DataPreprocessor
|
|
|
+ public let emptyResponseCodes: Set<Int>
|
|
|
+ public let emptyRequestMethods: Set<HTTPMethod>
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
- public static func dataResponseSerializer() -> DownloadResponseSerializer<Data> {
|
|
|
- return DownloadResponseSerializer { _, response, fileURL, error in
|
|
|
- guard error == nil else { return .failure(error!) }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public init(dataPreprocessor: DataPreprocessor = DataResponseSerializer.defaultDataPreprocessor,
|
|
|
+ emptyResponseCodes: Set<Int> = DataResponseSerializer.defaultEmptyResponseCodes,
|
|
|
+ emptyRequestMethods: Set<HTTPMethod> = DataResponseSerializer.defaultEmptyRequestMethods) {
|
|
|
+ self.dataPreprocessor = dataPreprocessor
|
|
|
+ self.emptyResponseCodes = emptyResponseCodes
|
|
|
+ self.emptyRequestMethods = emptyRequestMethods
|
|
|
+ }
|
|
|
|
|
|
- guard let fileURL = fileURL else {
|
|
|
- return .failure(AFError.responseSerializationFailed(reason: .inputFileNil))
|
|
|
- }
|
|
|
+ public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> Data {
|
|
|
+ guard error == nil else { throw error! }
|
|
|
|
|
|
- do {
|
|
|
- let data = try Data(contentsOf: fileURL)
|
|
|
- return Request.serializeResponseData(response: response, data: data, error: error)
|
|
|
- } catch {
|
|
|
- return .failure(AFError.responseSerializationFailed(reason: .inputFileReadFailed(at: fileURL)))
|
|
|
+ guard var data = data, !data.isEmpty else {
|
|
|
+ guard emptyResponseAllowed(forRequest: request, response: response) else {
|
|
|
+ throw AFError.responseSerializationFailed(reason: .inputDataNilOrZeroLength)
|
|
|
}
|
|
|
+
|
|
|
+ return Data()
|
|
|
}
|
|
|
+
|
|
|
+ data = try dataPreprocessor.preprocess(data)
|
|
|
+
|
|
|
+ return data
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
-
|
|
|
+extension DataRequest {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @discardableResult
|
|
|
+ public func responseData(queue: DispatchQueue = .main,
|
|
|
+ dataPreprocessor: DataPreprocessor = DataResponseSerializer.defaultDataPreprocessor,
|
|
|
+ emptyResponseCodes: Set<Int> = DataResponseSerializer.defaultEmptyResponseCodes,
|
|
|
+ emptyRequestMethods: Set<HTTPMethod> = DataResponseSerializer.defaultEmptyRequestMethods,
|
|
|
+ completionHandler: @escaping (AFDataResponse<Data>) -> Void) -> Self {
|
|
|
+ response(queue: queue,
|
|
|
+ responseSerializer: DataResponseSerializer(dataPreprocessor: dataPreprocessor,
|
|
|
+ emptyResponseCodes: emptyResponseCodes,
|
|
|
+ emptyRequestMethods: emptyRequestMethods),
|
|
|
+ completionHandler: completionHandler)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+extension DownloadRequest {
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
@discardableResult
|
|
|
- public func responseData(
|
|
|
- queue: DispatchQueue? = nil,
|
|
|
- completionHandler: @escaping (DownloadResponse<Data>) -> Void)
|
|
|
- -> Self
|
|
|
- {
|
|
|
- return response(
|
|
|
- queue: queue,
|
|
|
- responseSerializer: DownloadRequest.dataResponseSerializer(),
|
|
|
- completionHandler: completionHandler
|
|
|
- )
|
|
|
+ public func responseData(queue: DispatchQueue = .main,
|
|
|
+ dataPreprocessor: DataPreprocessor = DataResponseSerializer.defaultDataPreprocessor,
|
|
|
+ emptyResponseCodes: Set<Int> = DataResponseSerializer.defaultEmptyResponseCodes,
|
|
|
+ emptyRequestMethods: Set<HTTPMethod> = DataResponseSerializer.defaultEmptyRequestMethods,
|
|
|
+ completionHandler: @escaping (AFDownloadResponse<Data>) -> Void) -> Self {
|
|
|
+ response(queue: queue,
|
|
|
+ responseSerializer: DataResponseSerializer(dataPreprocessor: dataPreprocessor,
|
|
|
+ emptyResponseCodes: emptyResponseCodes,
|
|
|
+ emptyRequestMethods: emptyRequestMethods),
|
|
|
+ completionHandler: completionHandler)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
-extension Request {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public static func serializeResponseString(
|
|
|
- encoding: String.Encoding?,
|
|
|
- response: HTTPURLResponse?,
|
|
|
- data: Data?,
|
|
|
- error: Error?)
|
|
|
- -> Result<String>
|
|
|
- {
|
|
|
- guard error == nil else { return .failure(error!) }
|
|
|
-
|
|
|
- if let response = response, emptyDataStatusCodes.contains(response.statusCode) { return .success("") }
|
|
|
-
|
|
|
- guard let validData = data else {
|
|
|
- return .failure(AFError.responseSerializationFailed(reason: .inputDataNil))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+public final class StringResponseSerializer: ResponseSerializer {
|
|
|
+ public let dataPreprocessor: DataPreprocessor
|
|
|
+
|
|
|
+ public let encoding: String.Encoding?
|
|
|
+ public let emptyResponseCodes: Set<Int>
|
|
|
+ public let emptyRequestMethods: Set<HTTPMethod>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public init(dataPreprocessor: DataPreprocessor = StringResponseSerializer.defaultDataPreprocessor,
|
|
|
+ encoding: String.Encoding? = nil,
|
|
|
+ emptyResponseCodes: Set<Int> = StringResponseSerializer.defaultEmptyResponseCodes,
|
|
|
+ emptyRequestMethods: Set<HTTPMethod> = StringResponseSerializer.defaultEmptyRequestMethods) {
|
|
|
+ self.dataPreprocessor = dataPreprocessor
|
|
|
+ self.encoding = encoding
|
|
|
+ self.emptyResponseCodes = emptyResponseCodes
|
|
|
+ self.emptyRequestMethods = emptyRequestMethods
|
|
|
+ }
|
|
|
+
|
|
|
+ public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> String {
|
|
|
+ guard error == nil else { throw error! }
|
|
|
+
|
|
|
+ guard var data = data, !data.isEmpty else {
|
|
|
+ guard emptyResponseAllowed(forRequest: request, response: response) else {
|
|
|
+ throw AFError.responseSerializationFailed(reason: .inputDataNilOrZeroLength)
|
|
|
+ }
|
|
|
+
|
|
|
+ return ""
|
|
|
}
|
|
|
|
|
|
+ data = try dataPreprocessor.preprocess(data)
|
|
|
+
|
|
|
var convertedEncoding = encoding
|
|
|
|
|
|
- if let encodingName = response?.textEncodingName as CFString?, convertedEncoding == nil {
|
|
|
- convertedEncoding = String.Encoding(rawValue: CFStringConvertEncodingToNSStringEncoding(
|
|
|
- CFStringConvertIANACharSetNameToEncoding(encodingName))
|
|
|
- )
|
|
|
+ if let encodingName = response?.textEncodingName, convertedEncoding == nil {
|
|
|
+ convertedEncoding = String.Encoding(ianaCharsetName: encodingName)
|
|
|
}
|
|
|
|
|
|
let actualEncoding = convertedEncoding ?? .isoLatin1
|
|
|
|
|
|
- if let string = String(data: validData, encoding: actualEncoding) {
|
|
|
- return .success(string)
|
|
|
- } else {
|
|
|
- return .failure(AFError.responseSerializationFailed(reason: .stringSerializationFailed(encoding: actualEncoding)))
|
|
|
+ guard let string = String(data: data, encoding: actualEncoding) else {
|
|
|
+ throw AFError.responseSerializationFailed(reason: .stringSerializationFailed(encoding: actualEncoding))
|
|
|
}
|
|
|
+
|
|
|
+ return string
|
|
|
}
|
|
|
}
|
|
|
|
|
|
extension DataRequest {
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
- public static func stringResponseSerializer(encoding: String.Encoding? = nil) -> DataResponseSerializer<String> {
|
|
|
- return DataResponseSerializer { _, response, data, error in
|
|
|
- return Request.serializeResponseString(encoding: encoding, response: response, data: data, error: error)
|
|
|
- }
|
|
|
+
|
|
|
+ @discardableResult
|
|
|
+ public func responseString(queue: DispatchQueue = .main,
|
|
|
+ dataPreprocessor: DataPreprocessor = StringResponseSerializer.defaultDataPreprocessor,
|
|
|
+ encoding: String.Encoding? = nil,
|
|
|
+ emptyResponseCodes: Set<Int> = StringResponseSerializer.defaultEmptyResponseCodes,
|
|
|
+ emptyRequestMethods: Set<HTTPMethod> = StringResponseSerializer.defaultEmptyRequestMethods,
|
|
|
+ completionHandler: @escaping (AFDataResponse<String>) -> Void) -> Self {
|
|
|
+ response(queue: queue,
|
|
|
+ responseSerializer: StringResponseSerializer(dataPreprocessor: dataPreprocessor,
|
|
|
+ encoding: encoding,
|
|
|
+ emptyResponseCodes: emptyResponseCodes,
|
|
|
+ emptyRequestMethods: emptyRequestMethods),
|
|
|
+ completionHandler: completionHandler)
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
-
|
|
|
+extension DownloadRequest {
|
|
|
+
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
@discardableResult
|
|
|
- public func responseString(
|
|
|
- queue: DispatchQueue? = nil,
|
|
|
- encoding: String.Encoding? = nil,
|
|
|
- completionHandler: @escaping (DataResponse<String>) -> Void)
|
|
|
- -> Self
|
|
|
- {
|
|
|
- return response(
|
|
|
- queue: queue,
|
|
|
- responseSerializer: DataRequest.stringResponseSerializer(encoding: encoding),
|
|
|
- completionHandler: completionHandler
|
|
|
- )
|
|
|
+ public func responseString(queue: DispatchQueue = .main,
|
|
|
+ dataPreprocessor: DataPreprocessor = StringResponseSerializer.defaultDataPreprocessor,
|
|
|
+ encoding: String.Encoding? = nil,
|
|
|
+ emptyResponseCodes: Set<Int> = StringResponseSerializer.defaultEmptyResponseCodes,
|
|
|
+ emptyRequestMethods: Set<HTTPMethod> = StringResponseSerializer.defaultEmptyRequestMethods,
|
|
|
+ completionHandler: @escaping (AFDownloadResponse<String>) -> Void) -> Self {
|
|
|
+ response(queue: queue,
|
|
|
+ responseSerializer: StringResponseSerializer(dataPreprocessor: dataPreprocessor,
|
|
|
+ encoding: encoding,
|
|
|
+ emptyResponseCodes: emptyResponseCodes,
|
|
|
+ emptyRequestMethods: emptyRequestMethods),
|
|
|
+ completionHandler: completionHandler)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-extension DownloadRequest {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+public final class JSONResponseSerializer: ResponseSerializer {
|
|
|
+ public let dataPreprocessor: DataPreprocessor
|
|
|
+ public let emptyResponseCodes: Set<Int>
|
|
|
+ public let emptyRequestMethods: Set<HTTPMethod>
|
|
|
+
|
|
|
+ public let options: JSONSerialization.ReadingOptions
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
- public static func stringResponseSerializer(encoding: String.Encoding? = nil) -> DownloadResponseSerializer<String> {
|
|
|
- return DownloadResponseSerializer { _, response, fileURL, error in
|
|
|
- guard error == nil else { return .failure(error!) }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public init(dataPreprocessor: DataPreprocessor = JSONResponseSerializer.defaultDataPreprocessor,
|
|
|
+ emptyResponseCodes: Set<Int> = JSONResponseSerializer.defaultEmptyResponseCodes,
|
|
|
+ emptyRequestMethods: Set<HTTPMethod> = JSONResponseSerializer.defaultEmptyRequestMethods,
|
|
|
+ options: JSONSerialization.ReadingOptions = .allowFragments) {
|
|
|
+ self.dataPreprocessor = dataPreprocessor
|
|
|
+ self.emptyResponseCodes = emptyResponseCodes
|
|
|
+ self.emptyRequestMethods = emptyRequestMethods
|
|
|
+ self.options = options
|
|
|
+ }
|
|
|
|
|
|
- guard let fileURL = fileURL else {
|
|
|
- return .failure(AFError.responseSerializationFailed(reason: .inputFileNil))
|
|
|
- }
|
|
|
+ public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> Any {
|
|
|
+ guard error == nil else { throw error! }
|
|
|
|
|
|
- do {
|
|
|
- let data = try Data(contentsOf: fileURL)
|
|
|
- return Request.serializeResponseString(encoding: encoding, response: response, data: data, error: error)
|
|
|
- } catch {
|
|
|
- return .failure(AFError.responseSerializationFailed(reason: .inputFileReadFailed(at: fileURL)))
|
|
|
+ guard var data = data, !data.isEmpty else {
|
|
|
+ guard emptyResponseAllowed(forRequest: request, response: response) else {
|
|
|
+ throw AFError.responseSerializationFailed(reason: .inputDataNilOrZeroLength)
|
|
|
}
|
|
|
+
|
|
|
+ return NSNull()
|
|
|
+ }
|
|
|
+
|
|
|
+ data = try dataPreprocessor.preprocess(data)
|
|
|
+
|
|
|
+ do {
|
|
|
+ return try JSONSerialization.jsonObject(with: data, options: options)
|
|
|
+ } catch {
|
|
|
+ throw AFError.responseSerializationFailed(reason: .jsonSerializationFailed(error: error))
|
|
|
}
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
-
|
|
|
+extension DataRequest {
|
|
|
+
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
@discardableResult
|
|
|
- public func responseString(
|
|
|
- queue: DispatchQueue? = nil,
|
|
|
- encoding: String.Encoding? = nil,
|
|
|
- completionHandler: @escaping (DownloadResponse<String>) -> Void)
|
|
|
- -> Self
|
|
|
- {
|
|
|
- return response(
|
|
|
- queue: queue,
|
|
|
- responseSerializer: DownloadRequest.stringResponseSerializer(encoding: encoding),
|
|
|
- completionHandler: completionHandler
|
|
|
- )
|
|
|
+ public func responseJSON(queue: DispatchQueue = .main,
|
|
|
+ dataPreprocessor: DataPreprocessor = JSONResponseSerializer.defaultDataPreprocessor,
|
|
|
+ emptyResponseCodes: Set<Int> = JSONResponseSerializer.defaultEmptyResponseCodes,
|
|
|
+ emptyRequestMethods: Set<HTTPMethod> = JSONResponseSerializer.defaultEmptyRequestMethods,
|
|
|
+ options: JSONSerialization.ReadingOptions = .allowFragments,
|
|
|
+ completionHandler: @escaping (AFDataResponse<Any>) -> Void) -> Self {
|
|
|
+ response(queue: queue,
|
|
|
+ responseSerializer: JSONResponseSerializer(dataPreprocessor: dataPreprocessor,
|
|
|
+ emptyResponseCodes: emptyResponseCodes,
|
|
|
+ emptyRequestMethods: emptyRequestMethods,
|
|
|
+ options: options),
|
|
|
+ completionHandler: completionHandler)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+extension DownloadRequest {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @discardableResult
|
|
|
+ public func responseJSON(queue: DispatchQueue = .main,
|
|
|
+ dataPreprocessor: DataPreprocessor = JSONResponseSerializer.defaultDataPreprocessor,
|
|
|
+ emptyResponseCodes: Set<Int> = JSONResponseSerializer.defaultEmptyResponseCodes,
|
|
|
+ emptyRequestMethods: Set<HTTPMethod> = JSONResponseSerializer.defaultEmptyRequestMethods,
|
|
|
+ options: JSONSerialization.ReadingOptions = .allowFragments,
|
|
|
+ completionHandler: @escaping (AFDownloadResponse<Any>) -> Void) -> Self {
|
|
|
+ response(queue: queue,
|
|
|
+ responseSerializer: JSONResponseSerializer(dataPreprocessor: dataPreprocessor,
|
|
|
+ emptyResponseCodes: emptyResponseCodes,
|
|
|
+ emptyRequestMethods: emptyRequestMethods,
|
|
|
+ options: options),
|
|
|
+ completionHandler: completionHandler)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+public protocol EmptyResponse {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ static func emptyValue() -> Self
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+public struct Empty: Codable {
|
|
|
+
|
|
|
+ public static let value = Empty()
|
|
|
+}
|
|
|
+
|
|
|
+extension Empty: EmptyResponse {
|
|
|
+ public static func emptyValue() -> Empty {
|
|
|
+ value
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
-extension Request {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public static func serializeResponseJSON(
|
|
|
- options: JSONSerialization.ReadingOptions,
|
|
|
- response: HTTPURLResponse?,
|
|
|
- data: Data?,
|
|
|
- error: Error?)
|
|
|
- -> Result<Any>
|
|
|
- {
|
|
|
- guard error == nil else { return .failure(error!) }
|
|
|
-
|
|
|
- if let response = response, emptyDataStatusCodes.contains(response.statusCode) { return .success(NSNull()) }
|
|
|
-
|
|
|
- guard let validData = data, validData.count > 0 else {
|
|
|
- return .failure(AFError.responseSerializationFailed(reason: .inputDataNilOrZeroLength))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+public protocol DataDecoder {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ func decode<D: Decodable>(_ type: D.Type, from data: Data) throws -> D
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+extension JSONDecoder: DataDecoder {}
|
|
|
+
|
|
|
+extension PropertyListDecoder: DataDecoder {}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+public final class DecodableResponseSerializer<T: Decodable>: ResponseSerializer {
|
|
|
+ public let dataPreprocessor: DataPreprocessor
|
|
|
+
|
|
|
+ public let decoder: DataDecoder
|
|
|
+ public let emptyResponseCodes: Set<Int>
|
|
|
+ public let emptyRequestMethods: Set<HTTPMethod>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public init(dataPreprocessor: DataPreprocessor = DecodableResponseSerializer.defaultDataPreprocessor,
|
|
|
+ decoder: DataDecoder = JSONDecoder(),
|
|
|
+ emptyResponseCodes: Set<Int> = DecodableResponseSerializer.defaultEmptyResponseCodes,
|
|
|
+ emptyRequestMethods: Set<HTTPMethod> = DecodableResponseSerializer.defaultEmptyRequestMethods) {
|
|
|
+ self.dataPreprocessor = dataPreprocessor
|
|
|
+ self.decoder = decoder
|
|
|
+ self.emptyResponseCodes = emptyResponseCodes
|
|
|
+ self.emptyRequestMethods = emptyRequestMethods
|
|
|
+ }
|
|
|
+
|
|
|
+ public func serialize(request: URLRequest?, response: HTTPURLResponse?, data: Data?, error: Error?) throws -> T {
|
|
|
+ guard error == nil else { throw error! }
|
|
|
+
|
|
|
+ guard var data = data, !data.isEmpty else {
|
|
|
+ guard emptyResponseAllowed(forRequest: request, response: response) else {
|
|
|
+ throw AFError.responseSerializationFailed(reason: .inputDataNilOrZeroLength)
|
|
|
+ }
|
|
|
+
|
|
|
+ guard let emptyResponseType = T.self as? EmptyResponse.Type, let emptyValue = emptyResponseType.emptyValue() as? T else {
|
|
|
+ throw AFError.responseSerializationFailed(reason: .invalidEmptyResponse(type: "\(T.self)"))
|
|
|
+ }
|
|
|
+
|
|
|
+ return emptyValue
|
|
|
}
|
|
|
|
|
|
+ data = try dataPreprocessor.preprocess(data)
|
|
|
+
|
|
|
do {
|
|
|
- let json = try JSONSerialization.jsonObject(with: validData, options: options)
|
|
|
- return .success(json)
|
|
|
+ return try decoder.decode(T.self, from: data)
|
|
|
} catch {
|
|
|
- return .failure(AFError.responseSerializationFailed(reason: .jsonSerializationFailed(error: error)))
|
|
|
+ throw AFError.responseSerializationFailed(reason: .decodingFailed(error: error))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
extension DataRequest {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public static func jsonResponseSerializer(
|
|
|
- options: JSONSerialization.ReadingOptions = .allowFragments)
|
|
|
- -> DataResponseSerializer<Any>
|
|
|
- {
|
|
|
- return DataResponseSerializer { _, response, data, error in
|
|
|
- return Request.serializeResponseJSON(options: options, response: response, data: data, error: error)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
@discardableResult
|
|
|
- public func responseJSON(
|
|
|
- queue: DispatchQueue? = nil,
|
|
|
- options: JSONSerialization.ReadingOptions = .allowFragments,
|
|
|
- completionHandler: @escaping (DataResponse<Any>) -> Void)
|
|
|
- -> Self
|
|
|
- {
|
|
|
- return response(
|
|
|
- queue: queue,
|
|
|
- responseSerializer: DataRequest.jsonResponseSerializer(options: options),
|
|
|
- completionHandler: completionHandler
|
|
|
- )
|
|
|
+ public func responseDecodable<T: Decodable>(of type: T.Type = T.self,
|
|
|
+ queue: DispatchQueue = .main,
|
|
|
+ dataPreprocessor: DataPreprocessor = DecodableResponseSerializer<T>.defaultDataPreprocessor,
|
|
|
+ decoder: DataDecoder = JSONDecoder(),
|
|
|
+ emptyResponseCodes: Set<Int> = DecodableResponseSerializer<T>.defaultEmptyResponseCodes,
|
|
|
+ emptyRequestMethods: Set<HTTPMethod> = DecodableResponseSerializer<T>.defaultEmptyRequestMethods,
|
|
|
+ completionHandler: @escaping (AFDataResponse<T>) -> Void) -> Self {
|
|
|
+ response(queue: queue,
|
|
|
+ responseSerializer: DecodableResponseSerializer(dataPreprocessor: dataPreprocessor,
|
|
|
+ decoder: decoder,
|
|
|
+ emptyResponseCodes: emptyResponseCodes,
|
|
|
+ emptyRequestMethods: emptyRequestMethods),
|
|
|
+ completionHandler: completionHandler)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
extension DownloadRequest {
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
- public static func jsonResponseSerializer(
|
|
|
- options: JSONSerialization.ReadingOptions = .allowFragments)
|
|
|
- -> DownloadResponseSerializer<Any>
|
|
|
- {
|
|
|
- return DownloadResponseSerializer { _, response, fileURL, error in
|
|
|
- guard error == nil else { return .failure(error!) }
|
|
|
+
|
|
|
+ @discardableResult
|
|
|
+ public func responseDecodable<T: Decodable>(of type: T.Type = T.self,
|
|
|
+ queue: DispatchQueue = .main,
|
|
|
+ dataPreprocessor: DataPreprocessor = DecodableResponseSerializer<T>.defaultDataPreprocessor,
|
|
|
+ decoder: DataDecoder = JSONDecoder(),
|
|
|
+ emptyResponseCodes: Set<Int> = DecodableResponseSerializer<T>.defaultEmptyResponseCodes,
|
|
|
+ emptyRequestMethods: Set<HTTPMethod> = DecodableResponseSerializer<T>.defaultEmptyRequestMethods,
|
|
|
+ completionHandler: @escaping (AFDownloadResponse<T>) -> Void) -> Self {
|
|
|
+ response(queue: queue,
|
|
|
+ responseSerializer: DecodableResponseSerializer(dataPreprocessor: dataPreprocessor,
|
|
|
+ decoder: decoder,
|
|
|
+ emptyResponseCodes: emptyResponseCodes,
|
|
|
+ emptyRequestMethods: emptyRequestMethods),
|
|
|
+ completionHandler: completionHandler)
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- guard let fileURL = fileURL else {
|
|
|
- return .failure(AFError.responseSerializationFailed(reason: .inputFileNil))
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
- do {
|
|
|
- let data = try Data(contentsOf: fileURL)
|
|
|
- return Request.serializeResponseJSON(options: options, response: response, data: data, error: error)
|
|
|
- } catch {
|
|
|
- return .failure(AFError.responseSerializationFailed(reason: .inputFileReadFailed(at: fileURL)))
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
+public protocol DataStreamSerializer {
|
|
|
+
|
|
|
+ associatedtype SerializedObject
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
|
|
|
-
|
|
|
- @discardableResult
|
|
|
- public func responseJSON(
|
|
|
- queue: DispatchQueue? = nil,
|
|
|
- options: JSONSerialization.ReadingOptions = .allowFragments,
|
|
|
- completionHandler: @escaping (DownloadResponse<Any>) -> Void)
|
|
|
- -> Self
|
|
|
- {
|
|
|
- return response(
|
|
|
- queue: queue,
|
|
|
- responseSerializer: DownloadRequest.jsonResponseSerializer(options: options),
|
|
|
- completionHandler: completionHandler
|
|
|
- )
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-extension Request {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public static func serializeResponsePropertyList(
|
|
|
- options: PropertyListSerialization.ReadOptions,
|
|
|
- response: HTTPURLResponse?,
|
|
|
- data: Data?,
|
|
|
- error: Error?)
|
|
|
- -> Result<Any>
|
|
|
- {
|
|
|
- guard error == nil else { return .failure(error!) }
|
|
|
-
|
|
|
- if let response = response, emptyDataStatusCodes.contains(response.statusCode) { return .success(NSNull()) }
|
|
|
-
|
|
|
- guard let validData = data, validData.count > 0 else {
|
|
|
- return .failure(AFError.responseSerializationFailed(reason: .inputDataNilOrZeroLength))
|
|
|
- }
|
|
|
+
|
|
|
+ func serialize(_ data: Data) throws -> SerializedObject
|
|
|
+}
|
|
|
|
|
|
+
|
|
|
+public struct DecodableStreamSerializer<T: Decodable>: DataStreamSerializer {
|
|
|
+
|
|
|
+ public let decoder: DataDecoder
|
|
|
+
|
|
|
+ public let dataPreprocessor: DataPreprocessor
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public init(decoder: DataDecoder = JSONDecoder(), dataPreprocessor: DataPreprocessor = PassthroughPreprocessor()) {
|
|
|
+ self.decoder = decoder
|
|
|
+ self.dataPreprocessor = dataPreprocessor
|
|
|
+ }
|
|
|
+
|
|
|
+ public func serialize(_ data: Data) throws -> T {
|
|
|
+ let processedData = try dataPreprocessor.preprocess(data)
|
|
|
do {
|
|
|
- let plist = try PropertyListSerialization.propertyList(from: validData, options: options, format: nil)
|
|
|
- return .success(plist)
|
|
|
+ return try decoder.decode(T.self, from: processedData)
|
|
|
} catch {
|
|
|
- return .failure(AFError.responseSerializationFailed(reason: .propertyListSerializationFailed(error: error)))
|
|
|
+ throw AFError.responseSerializationFailed(reason: .decodingFailed(error: error))
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-extension DataRequest {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public static func propertyListResponseSerializer(
|
|
|
- options: PropertyListSerialization.ReadOptions = [])
|
|
|
- -> DataResponseSerializer<Any>
|
|
|
- {
|
|
|
- return DataResponseSerializer { _, response, data, error in
|
|
|
- return Request.serializeResponsePropertyList(options: options, response: response, data: data, error: error)
|
|
|
- }
|
|
|
+
|
|
|
+public struct PassthroughStreamSerializer: DataStreamSerializer {
|
|
|
+ public func serialize(_ data: Data) throws -> Data { data }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+public struct StringStreamSerializer: DataStreamSerializer {
|
|
|
+ public func serialize(_ data: Data) throws -> String {
|
|
|
+ String(decoding: data, as: UTF8.self)
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
-
|
|
|
+extension DataStreamRequest {
|
|
|
+
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
@discardableResult
|
|
|
- public func responsePropertyList(
|
|
|
- queue: DispatchQueue? = nil,
|
|
|
- options: PropertyListSerialization.ReadOptions = [],
|
|
|
- completionHandler: @escaping (DataResponse<Any>) -> Void)
|
|
|
- -> Self
|
|
|
- {
|
|
|
- return response(
|
|
|
- queue: queue,
|
|
|
- responseSerializer: DataRequest.propertyListResponseSerializer(options: options),
|
|
|
- completionHandler: completionHandler
|
|
|
- )
|
|
|
+ public func responseStream(on queue: DispatchQueue = .main, stream: @escaping Handler<Data, Never>) -> Self {
|
|
|
+ let parser = { [unowned self] (data: Data) in
|
|
|
+ queue.async {
|
|
|
+ self.capturingError {
|
|
|
+ try stream(.init(event: .stream(.success(data)), token: .init(self)))
|
|
|
+ }
|
|
|
+
|
|
|
+ self.updateAndCompleteIfPossible()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $streamMutableState.write { $0.streams.append(parser) }
|
|
|
+ appendStreamCompletion(on: queue, stream: stream)
|
|
|
+
|
|
|
+ return self
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-extension DownloadRequest {
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
- public static func propertyListResponseSerializer(
|
|
|
- options: PropertyListSerialization.ReadOptions = [])
|
|
|
- -> DownloadResponseSerializer<Any>
|
|
|
- {
|
|
|
- return DownloadResponseSerializer { _, response, fileURL, error in
|
|
|
- guard error == nil else { return .failure(error!) }
|
|
|
-
|
|
|
- guard let fileURL = fileURL else {
|
|
|
- return .failure(AFError.responseSerializationFailed(reason: .inputFileNil))
|
|
|
+
|
|
|
+ @discardableResult
|
|
|
+ public func responseStream<Serializer: DataStreamSerializer>(using serializer: Serializer,
|
|
|
+ on queue: DispatchQueue = .main,
|
|
|
+ stream: @escaping Handler<Serializer.SerializedObject, AFError>) -> Self {
|
|
|
+ let parser = { [unowned self] (data: Data) in
|
|
|
+ self.serializationQueue.async {
|
|
|
+
|
|
|
+ let result = Result { try serializer.serialize(data) }
|
|
|
+ .mapError { $0.asAFError(or: .responseSerializationFailed(reason: .customSerializationFailed(error: $0))) }
|
|
|
+
|
|
|
+ self.underlyingQueue.async {
|
|
|
+ self.eventMonitor?.request(self, didParseStream: result)
|
|
|
+
|
|
|
+ if result.isFailure, self.automaticallyCancelOnStreamError {
|
|
|
+ self.cancel()
|
|
|
+ }
|
|
|
+
|
|
|
+ queue.async {
|
|
|
+ self.capturingError {
|
|
|
+ try stream(.init(event: .stream(result), token: .init(self)))
|
|
|
+ }
|
|
|
+
|
|
|
+ self.updateAndCompleteIfPossible()
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ $streamMutableState.write { $0.streams.append(parser) }
|
|
|
+ appendStreamCompletion(on: queue, stream: stream)
|
|
|
|
|
|
- do {
|
|
|
- let data = try Data(contentsOf: fileURL)
|
|
|
- return Request.serializeResponsePropertyList(options: options, response: response, data: data, error: error)
|
|
|
- } catch {
|
|
|
- return .failure(AFError.responseSerializationFailed(reason: .inputFileReadFailed(at: fileURL)))
|
|
|
+ return self
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @discardableResult
|
|
|
+ public func responseStreamString(on queue: DispatchQueue = .main,
|
|
|
+ stream: @escaping Handler<String, Never>) -> Self {
|
|
|
+ let parser = { [unowned self] (data: Data) in
|
|
|
+ self.serializationQueue.async {
|
|
|
+
|
|
|
+ let string = String(decoding: data, as: UTF8.self)
|
|
|
+
|
|
|
+ self.underlyingQueue.async {
|
|
|
+ self.eventMonitor?.request(self, didParseStream: .success(string))
|
|
|
+
|
|
|
+ queue.async {
|
|
|
+ self.capturingError {
|
|
|
+ try stream(.init(event: .stream(.success(string)), token: .init(self)))
|
|
|
+ }
|
|
|
+
|
|
|
+ self.updateAndCompleteIfPossible()
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ $streamMutableState.write { $0.streams.append(parser) }
|
|
|
+ appendStreamCompletion(on: queue, stream: stream)
|
|
|
+
|
|
|
+ return self
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ private func updateAndCompleteIfPossible() {
|
|
|
+ $streamMutableState.write { state in
|
|
|
+ state.numberOfExecutingStreams -= 1
|
|
|
+
|
|
|
+ guard state.numberOfExecutingStreams == 0, !state.enqueuedCompletionEvents.isEmpty else { return }
|
|
|
+
|
|
|
+ let completionEvents = state.enqueuedCompletionEvents
|
|
|
+ self.underlyingQueue.async { completionEvents.forEach { $0() } }
|
|
|
+ state.enqueuedCompletionEvents.removeAll()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
+
|
|
|
@discardableResult
|
|
|
- public func responsePropertyList(
|
|
|
- queue: DispatchQueue? = nil,
|
|
|
- options: PropertyListSerialization.ReadOptions = [],
|
|
|
- completionHandler: @escaping (DownloadResponse<Any>) -> Void)
|
|
|
- -> Self
|
|
|
- {
|
|
|
- return response(
|
|
|
- queue: queue,
|
|
|
- responseSerializer: DownloadRequest.propertyListResponseSerializer(options: options),
|
|
|
- completionHandler: completionHandler
|
|
|
- )
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-private let emptyDataStatusCodes: Set<Int> = [204, 205]
|
|
|
+ public func responseStreamDecodable<T: Decodable>(of type: T.Type = T.self,
|
|
|
+ on queue: DispatchQueue = .main,
|
|
|
+ using decoder: DataDecoder = JSONDecoder(),
|
|
|
+ preprocessor: DataPreprocessor = PassthroughPreprocessor(),
|
|
|
+ stream: @escaping Handler<T, AFError>) -> Self {
|
|
|
+ responseStream(using: DecodableStreamSerializer<T>(decoder: decoder, dataPreprocessor: preprocessor),
|
|
|
+ stream: stream)
|
|
|
+ }
|
|
|
+}
|