forked from loafle/openapi-generator-original
enhance response with bodyData (#14006)
This commit is contained in:
parent
2e44e78474
commit
e882421ff3
@ -87,14 +87,16 @@ extension NullEncodable: Codable where Wrapped: Codable {
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let statusCode: Int
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let header: [String: String]
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let body: T
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let bodyData: Data?
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init(statusCode: Int, header: [String: String], body: T) {
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init(statusCode: Int, header: [String: String], body: T, bodyData: Data?) {
|
||||
self.statusCode = statusCode
|
||||
self.header = header
|
||||
self.body = body
|
||||
self.bodyData = bodyData
|
||||
}
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} convenience init(response: HTTPURLResponse, body: T) {
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} convenience init(response: HTTPURLResponse, body: T, bodyData: Data?) {
|
||||
let rawHeader = response.allHeaderFields
|
||||
var header = [String: String]()
|
||||
for (key, value) in rawHeader {
|
||||
@ -102,7 +104,7 @@ extension NullEncodable: Codable where Wrapped: Codable {
|
||||
header[key] = value
|
||||
}
|
||||
}
|
||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||
self.init(statusCode: response.statusCode, header: header, body: body, bodyData: bodyData)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -176,7 +176,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.Session>()
|
||||
|
||||
switch voidResponse.result {
|
||||
case .success:
|
||||
completion(.success(Response(response: voidResponse.response!, body: () as! T)))
|
||||
completion(.success(Response(response: voidResponse.response!, body: () as! T, bodyData: voidResponse.data)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.response, error)))
|
||||
}
|
||||
@ -270,7 +270,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.Session>()
|
||||
|
||||
switch stringResponse.result {
|
||||
case let .success(value):
|
||||
completion(.success(Response(response: stringResponse.response!, body: value as! T)))
|
||||
completion(.success(Response(response: stringResponse.response!, body: value as! T, bodyData: stringResponse.data)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.response, error)))
|
||||
}
|
||||
@ -315,7 +315,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.Session>()
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(.success(Response(response: dataResponse.response!, body: filePath as! T)))
|
||||
completion(.success(Response(response: dataResponse.response!, body: filePath as! T, bodyData: data)))
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, dataResponse.data, dataResponse.response, requestParserError)))
|
||||
@ -332,7 +332,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.Session>()
|
||||
|
||||
switch voidResponse.result {
|
||||
case .success:
|
||||
completion(.success(Response(response: voidResponse.response!, body: () as! T)))
|
||||
completion(.success(Response(response: voidResponse.response!, body: () as! T, bodyData: voidResponse.data)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.response, error)))
|
||||
}
|
||||
@ -346,7 +346,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.Session>()
|
||||
|
||||
switch dataResponse.result {
|
||||
case .success:
|
||||
completion(.success(Response(response: dataResponse.response!, body: dataResponse.data as! T)))
|
||||
completion(.success(Response(response: dataResponse.response!, body: dataResponse.data as! T, bodyData: dataResponse.data)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.response, error)))
|
||||
}
|
||||
@ -370,7 +370,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.Session>()
|
||||
|
||||
guard let data = dataResponse.data, !data.isEmpty else {
|
||||
if T.self is ExpressibleByNilLiteral.Type {
|
||||
completion(.success(Response(response: httpResponse, body: Optional<T>.none as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: Optional<T>.none as! T, bodyData: dataResponse.data)))
|
||||
} else {
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, nil, httpResponse, DecodableRequestBuilderError.emptyDataResponse)))
|
||||
}
|
||||
@ -381,7 +381,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.Session>()
|
||||
|
||||
switch decodeResult {
|
||||
case let .success(decodableObj):
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj)))
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj, bodyData: data)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, data, httpResponse, error)))
|
||||
}
|
||||
|
@ -208,7 +208,7 @@ private var credentialStore = SynchronizedDictionary<Int, URLCredential>()
|
||||
switch T.self {
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
|
||||
@ -303,7 +303,7 @@ private var credentialStore = SynchronizedDictionary<Int, URLCredential>()
|
||||
|
||||
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
|
||||
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T)))
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T, bodyData: data)))
|
||||
|
||||
case is URL.Type:
|
||||
do {
|
||||
@ -334,7 +334,7 @@ private var credentialStore = SynchronizedDictionary<Int, URLCredential>()
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T, bodyData: data)))
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
@ -344,30 +344,30 @@ private var credentialStore = SynchronizedDictionary<Int, URLCredential>()
|
||||
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
case is Data.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: data as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: data as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
|
||||
guard let data = data, !data.isEmpty else {
|
||||
guard let unwrappedData = data, !unwrappedData.isEmpty else {
|
||||
if let E = T.self as? ExpressibleByNilLiteral.Type {
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T, bodyData: data)))
|
||||
} else {
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, nil, response, DecodableRequestBuilderError.emptyDataResponse)))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let decodeResult = CodableHelper.decode(T.self, from: data)
|
||||
let decodeResult = CodableHelper.decode(T.self, from: unwrappedData)
|
||||
|
||||
switch decodeResult {
|
||||
case let .success(decodableObj):
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj)))
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj, bodyData: unwrappedData)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, data, response, error)))
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, unwrappedData, response, error)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -176,7 +176,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
switch voidResponse.result {
|
||||
case .success:
|
||||
completion(.success(Response(response: voidResponse.response!, body: () as! T)))
|
||||
completion(.success(Response(response: voidResponse.response!, body: () as! T, bodyData: voidResponse.data)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.response, error)))
|
||||
}
|
||||
@ -270,7 +270,7 @@ open class AlamofireDecodableRequestBuilder<T: Decodable>: AlamofireRequestBuild
|
||||
|
||||
switch stringResponse.result {
|
||||
case let .success(value):
|
||||
completion(.success(Response(response: stringResponse.response!, body: value as! T)))
|
||||
completion(.success(Response(response: stringResponse.response!, body: value as! T, bodyData: stringResponse.data)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.response, error)))
|
||||
}
|
||||
@ -315,7 +315,7 @@ open class AlamofireDecodableRequestBuilder<T: Decodable>: AlamofireRequestBuild
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(.success(Response(response: dataResponse.response!, body: filePath as! T)))
|
||||
completion(.success(Response(response: dataResponse.response!, body: filePath as! T, bodyData: data)))
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, dataResponse.data, dataResponse.response, requestParserError)))
|
||||
@ -332,7 +332,7 @@ open class AlamofireDecodableRequestBuilder<T: Decodable>: AlamofireRequestBuild
|
||||
|
||||
switch voidResponse.result {
|
||||
case .success:
|
||||
completion(.success(Response(response: voidResponse.response!, body: () as! T)))
|
||||
completion(.success(Response(response: voidResponse.response!, body: () as! T, bodyData: voidResponse.data)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.response, error)))
|
||||
}
|
||||
@ -346,7 +346,7 @@ open class AlamofireDecodableRequestBuilder<T: Decodable>: AlamofireRequestBuild
|
||||
|
||||
switch dataResponse.result {
|
||||
case .success:
|
||||
completion(.success(Response(response: dataResponse.response!, body: dataResponse.data as! T)))
|
||||
completion(.success(Response(response: dataResponse.response!, body: dataResponse.data as! T, bodyData: dataResponse.data)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.response, error)))
|
||||
}
|
||||
@ -370,7 +370,7 @@ open class AlamofireDecodableRequestBuilder<T: Decodable>: AlamofireRequestBuild
|
||||
|
||||
guard let data = dataResponse.data, !data.isEmpty else {
|
||||
if T.self is ExpressibleByNilLiteral.Type {
|
||||
completion(.success(Response(response: httpResponse, body: Optional<T>.none as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: Optional<T>.none as! T, bodyData: dataResponse.data)))
|
||||
} else {
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, nil, httpResponse, DecodableRequestBuilderError.emptyDataResponse)))
|
||||
}
|
||||
@ -381,7 +381,7 @@ open class AlamofireDecodableRequestBuilder<T: Decodable>: AlamofireRequestBuild
|
||||
|
||||
switch decodeResult {
|
||||
case let .success(decodableObj):
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj)))
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj, bodyData: data)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, data, httpResponse, error)))
|
||||
}
|
||||
|
@ -87,14 +87,16 @@ open class Response<T> {
|
||||
public let statusCode: Int
|
||||
public let header: [String: String]
|
||||
public let body: T
|
||||
public let bodyData: Data?
|
||||
|
||||
public init(statusCode: Int, header: [String: String], body: T) {
|
||||
public init(statusCode: Int, header: [String: String], body: T, bodyData: Data?) {
|
||||
self.statusCode = statusCode
|
||||
self.header = header
|
||||
self.body = body
|
||||
self.bodyData = bodyData
|
||||
}
|
||||
|
||||
public convenience init(response: HTTPURLResponse, body: T) {
|
||||
public convenience init(response: HTTPURLResponse, body: T, bodyData: Data?) {
|
||||
let rawHeader = response.allHeaderFields
|
||||
var header = [String: String]()
|
||||
for (key, value) in rawHeader {
|
||||
@ -102,7 +104,7 @@ open class Response<T> {
|
||||
header[key] = value
|
||||
}
|
||||
}
|
||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||
self.init(statusCode: response.statusCode, header: header, body: body, bodyData: bodyData)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -86,14 +86,16 @@ open class Response<T> {
|
||||
public let statusCode: Int
|
||||
public let header: [String: String]
|
||||
public let body: T
|
||||
public let bodyData: Data?
|
||||
|
||||
public init(statusCode: Int, header: [String: String], body: T) {
|
||||
public init(statusCode: Int, header: [String: String], body: T, bodyData: Data?) {
|
||||
self.statusCode = statusCode
|
||||
self.header = header
|
||||
self.body = body
|
||||
self.bodyData = bodyData
|
||||
}
|
||||
|
||||
public convenience init(response: HTTPURLResponse, body: T) {
|
||||
public convenience init(response: HTTPURLResponse, body: T, bodyData: Data?) {
|
||||
let rawHeader = response.allHeaderFields
|
||||
var header = [String: String]()
|
||||
for (key, value) in rawHeader {
|
||||
@ -101,7 +103,7 @@ open class Response<T> {
|
||||
header[key] = value
|
||||
}
|
||||
}
|
||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||
self.init(statusCode: response.statusCode, header: header, body: body, bodyData: bodyData)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
switch T.self {
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
|
||||
@ -303,7 +303,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
|
||||
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T)))
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T, bodyData: data)))
|
||||
|
||||
case is URL.Type:
|
||||
do {
|
||||
@ -334,7 +334,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T, bodyData: data)))
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
@ -344,30 +344,30 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
case is Data.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: data as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: data as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
|
||||
guard let data = data, !data.isEmpty else {
|
||||
guard let unwrappedData = data, !unwrappedData.isEmpty else {
|
||||
if let E = T.self as? ExpressibleByNilLiteral.Type {
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T, bodyData: data)))
|
||||
} else {
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, nil, response, DecodableRequestBuilderError.emptyDataResponse)))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let decodeResult = CodableHelper.decode(T.self, from: data)
|
||||
let decodeResult = CodableHelper.decode(T.self, from: unwrappedData)
|
||||
|
||||
switch decodeResult {
|
||||
case let .success(decodableObj):
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj)))
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj, bodyData: unwrappedData)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, data, response, error)))
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, unwrappedData, response, error)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,14 +86,16 @@ open class Response<T> {
|
||||
public let statusCode: Int
|
||||
public let header: [String: String]
|
||||
public let body: T
|
||||
public let bodyData: Data?
|
||||
|
||||
public init(statusCode: Int, header: [String: String], body: T) {
|
||||
public init(statusCode: Int, header: [String: String], body: T, bodyData: Data?) {
|
||||
self.statusCode = statusCode
|
||||
self.header = header
|
||||
self.body = body
|
||||
self.bodyData = bodyData
|
||||
}
|
||||
|
||||
public convenience init(response: HTTPURLResponse, body: T) {
|
||||
public convenience init(response: HTTPURLResponse, body: T, bodyData: Data?) {
|
||||
let rawHeader = response.allHeaderFields
|
||||
var header = [String: String]()
|
||||
for (key, value) in rawHeader {
|
||||
@ -101,7 +103,7 @@ open class Response<T> {
|
||||
header[key] = value
|
||||
}
|
||||
}
|
||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||
self.init(statusCode: response.statusCode, header: header, body: body, bodyData: bodyData)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
switch T.self {
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
|
||||
@ -303,7 +303,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
|
||||
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T)))
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T, bodyData: data)))
|
||||
|
||||
case is URL.Type:
|
||||
do {
|
||||
@ -334,7 +334,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T, bodyData: data)))
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
@ -344,30 +344,30 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
case is Data.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: data as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: data as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
|
||||
guard let data = data, !data.isEmpty else {
|
||||
guard let unwrappedData = data, !unwrappedData.isEmpty else {
|
||||
if let E = T.self as? ExpressibleByNilLiteral.Type {
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T, bodyData: data)))
|
||||
} else {
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, nil, response, DecodableRequestBuilderError.emptyDataResponse)))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let decodeResult = CodableHelper.decode(T.self, from: data)
|
||||
let decodeResult = CodableHelper.decode(T.self, from: unwrappedData)
|
||||
|
||||
switch decodeResult {
|
||||
case let .success(decodableObj):
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj)))
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj, bodyData: unwrappedData)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, data, response, error)))
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, unwrappedData, response, error)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,14 +86,16 @@ open class Response<T> {
|
||||
public let statusCode: Int
|
||||
public let header: [String: String]
|
||||
public let body: T
|
||||
public let bodyData: Data?
|
||||
|
||||
public init(statusCode: Int, header: [String: String], body: T) {
|
||||
public init(statusCode: Int, header: [String: String], body: T, bodyData: Data?) {
|
||||
self.statusCode = statusCode
|
||||
self.header = header
|
||||
self.body = body
|
||||
self.bodyData = bodyData
|
||||
}
|
||||
|
||||
public convenience init(response: HTTPURLResponse, body: T) {
|
||||
public convenience init(response: HTTPURLResponse, body: T, bodyData: Data?) {
|
||||
let rawHeader = response.allHeaderFields
|
||||
var header = [String: String]()
|
||||
for (key, value) in rawHeader {
|
||||
@ -101,7 +103,7 @@ open class Response<T> {
|
||||
header[key] = value
|
||||
}
|
||||
}
|
||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||
self.init(statusCode: response.statusCode, header: header, body: body, bodyData: bodyData)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
switch T.self {
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
|
||||
@ -303,7 +303,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
|
||||
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T)))
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T, bodyData: data)))
|
||||
|
||||
case is URL.Type:
|
||||
do {
|
||||
@ -334,7 +334,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T, bodyData: data)))
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
@ -344,30 +344,30 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
case is Data.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: data as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: data as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
|
||||
guard let data = data, !data.isEmpty else {
|
||||
guard let unwrappedData = data, !unwrappedData.isEmpty else {
|
||||
if let E = T.self as? ExpressibleByNilLiteral.Type {
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T, bodyData: data)))
|
||||
} else {
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, nil, response, DecodableRequestBuilderError.emptyDataResponse)))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let decodeResult = CodableHelper.decode(T.self, from: data)
|
||||
let decodeResult = CodableHelper.decode(T.self, from: unwrappedData)
|
||||
|
||||
switch decodeResult {
|
||||
case let .success(decodableObj):
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj)))
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj, bodyData: unwrappedData)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, data, response, error)))
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, unwrappedData, response, error)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,14 +86,16 @@ open class Response<T> {
|
||||
public let statusCode: Int
|
||||
public let header: [String: String]
|
||||
public let body: T
|
||||
public let bodyData: Data?
|
||||
|
||||
public init(statusCode: Int, header: [String: String], body: T) {
|
||||
public init(statusCode: Int, header: [String: String], body: T, bodyData: Data?) {
|
||||
self.statusCode = statusCode
|
||||
self.header = header
|
||||
self.body = body
|
||||
self.bodyData = bodyData
|
||||
}
|
||||
|
||||
public convenience init(response: HTTPURLResponse, body: T) {
|
||||
public convenience init(response: HTTPURLResponse, body: T, bodyData: Data?) {
|
||||
let rawHeader = response.allHeaderFields
|
||||
var header = [String: String]()
|
||||
for (key, value) in rawHeader {
|
||||
@ -101,7 +103,7 @@ open class Response<T> {
|
||||
header[key] = value
|
||||
}
|
||||
}
|
||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||
self.init(statusCode: response.statusCode, header: header, body: body, bodyData: bodyData)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
switch T.self {
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
|
||||
@ -303,7 +303,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
|
||||
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T)))
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T, bodyData: data)))
|
||||
|
||||
case is URL.Type:
|
||||
do {
|
||||
@ -334,7 +334,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T, bodyData: data)))
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
@ -344,30 +344,30 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
case is Data.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: data as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: data as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
|
||||
guard let data = data, !data.isEmpty else {
|
||||
guard let unwrappedData = data, !unwrappedData.isEmpty else {
|
||||
if let E = T.self as? ExpressibleByNilLiteral.Type {
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T, bodyData: data)))
|
||||
} else {
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, nil, response, DecodableRequestBuilderError.emptyDataResponse)))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let decodeResult = CodableHelper.decode(T.self, from: data)
|
||||
let decodeResult = CodableHelper.decode(T.self, from: unwrappedData)
|
||||
|
||||
switch decodeResult {
|
||||
case let .success(decodableObj):
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj)))
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj, bodyData: unwrappedData)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, data, response, error)))
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, unwrappedData, response, error)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,14 +86,16 @@ open class Response<T> {
|
||||
public let statusCode: Int
|
||||
public let header: [String: String]
|
||||
public let body: T
|
||||
public let bodyData: Data?
|
||||
|
||||
public init(statusCode: Int, header: [String: String], body: T) {
|
||||
public init(statusCode: Int, header: [String: String], body: T, bodyData: Data?) {
|
||||
self.statusCode = statusCode
|
||||
self.header = header
|
||||
self.body = body
|
||||
self.bodyData = bodyData
|
||||
}
|
||||
|
||||
public convenience init(response: HTTPURLResponse, body: T) {
|
||||
public convenience init(response: HTTPURLResponse, body: T, bodyData: Data?) {
|
||||
let rawHeader = response.allHeaderFields
|
||||
var header = [String: String]()
|
||||
for (key, value) in rawHeader {
|
||||
@ -101,7 +103,7 @@ open class Response<T> {
|
||||
header[key] = value
|
||||
}
|
||||
}
|
||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||
self.init(statusCode: response.statusCode, header: header, body: body, bodyData: bodyData)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
switch T.self {
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
|
||||
@ -303,7 +303,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
|
||||
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T)))
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T, bodyData: data)))
|
||||
|
||||
case is URL.Type:
|
||||
do {
|
||||
@ -334,7 +334,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T, bodyData: data)))
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
@ -344,30 +344,30 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
case is Data.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: data as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: data as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
|
||||
guard let data = data, !data.isEmpty else {
|
||||
guard let unwrappedData = data, !unwrappedData.isEmpty else {
|
||||
if let E = T.self as? ExpressibleByNilLiteral.Type {
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T, bodyData: data)))
|
||||
} else {
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, nil, response, DecodableRequestBuilderError.emptyDataResponse)))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let decodeResult = CodableHelper.decode(T.self, from: data)
|
||||
let decodeResult = CodableHelper.decode(T.self, from: unwrappedData)
|
||||
|
||||
switch decodeResult {
|
||||
case let .success(decodableObj):
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj)))
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj, bodyData: unwrappedData)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, data, response, error)))
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, unwrappedData, response, error)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,14 +86,16 @@ internal class Response<T> {
|
||||
internal let statusCode: Int
|
||||
internal let header: [String: String]
|
||||
internal let body: T
|
||||
internal let bodyData: Data?
|
||||
|
||||
internal init(statusCode: Int, header: [String: String], body: T) {
|
||||
internal init(statusCode: Int, header: [String: String], body: T, bodyData: Data?) {
|
||||
self.statusCode = statusCode
|
||||
self.header = header
|
||||
self.body = body
|
||||
self.bodyData = bodyData
|
||||
}
|
||||
|
||||
internal convenience init(response: HTTPURLResponse, body: T) {
|
||||
internal convenience init(response: HTTPURLResponse, body: T, bodyData: Data?) {
|
||||
let rawHeader = response.allHeaderFields
|
||||
var header = [String: String]()
|
||||
for (key, value) in rawHeader {
|
||||
@ -101,7 +103,7 @@ internal class Response<T> {
|
||||
header[key] = value
|
||||
}
|
||||
}
|
||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||
self.init(statusCode: response.statusCode, header: header, body: body, bodyData: bodyData)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ internal class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
switch T.self {
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
|
||||
@ -303,7 +303,7 @@ internal class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionReques
|
||||
|
||||
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
|
||||
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T)))
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T, bodyData: data)))
|
||||
|
||||
case is URL.Type:
|
||||
do {
|
||||
@ -334,7 +334,7 @@ internal class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionReques
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T, bodyData: data)))
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
@ -344,30 +344,30 @@ internal class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionReques
|
||||
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
case is Data.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: data as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: data as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
|
||||
guard let data = data, !data.isEmpty else {
|
||||
guard let unwrappedData = data, !unwrappedData.isEmpty else {
|
||||
if let E = T.self as? ExpressibleByNilLiteral.Type {
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T, bodyData: data)))
|
||||
} else {
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, nil, response, DecodableRequestBuilderError.emptyDataResponse)))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let decodeResult = CodableHelper.decode(T.self, from: data)
|
||||
let decodeResult = CodableHelper.decode(T.self, from: unwrappedData)
|
||||
|
||||
switch decodeResult {
|
||||
case let .success(decodableObj):
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj)))
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj, bodyData: unwrappedData)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, data, response, error)))
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, unwrappedData, response, error)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,14 +86,16 @@ open class Response<T> {
|
||||
public let statusCode: Int
|
||||
public let header: [String: String]
|
||||
public let body: T
|
||||
public let bodyData: Data?
|
||||
|
||||
public init(statusCode: Int, header: [String: String], body: T) {
|
||||
public init(statusCode: Int, header: [String: String], body: T, bodyData: Data?) {
|
||||
self.statusCode = statusCode
|
||||
self.header = header
|
||||
self.body = body
|
||||
self.bodyData = bodyData
|
||||
}
|
||||
|
||||
public convenience init(response: HTTPURLResponse, body: T) {
|
||||
public convenience init(response: HTTPURLResponse, body: T, bodyData: Data?) {
|
||||
let rawHeader = response.allHeaderFields
|
||||
var header = [String: String]()
|
||||
for (key, value) in rawHeader {
|
||||
@ -101,7 +103,7 @@ open class Response<T> {
|
||||
header[key] = value
|
||||
}
|
||||
}
|
||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||
self.init(statusCode: response.statusCode, header: header, body: body, bodyData: bodyData)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
switch T.self {
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
|
||||
@ -303,7 +303,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
|
||||
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T)))
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T, bodyData: data)))
|
||||
|
||||
case is URL.Type:
|
||||
do {
|
||||
@ -334,7 +334,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T, bodyData: data)))
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
@ -344,30 +344,30 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
case is Data.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: data as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: data as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
|
||||
guard let data = data, !data.isEmpty else {
|
||||
guard let unwrappedData = data, !unwrappedData.isEmpty else {
|
||||
if let E = T.self as? ExpressibleByNilLiteral.Type {
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T, bodyData: data)))
|
||||
} else {
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, nil, response, DecodableRequestBuilderError.emptyDataResponse)))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let decodeResult = CodableHelper.decode(T.self, from: data)
|
||||
let decodeResult = CodableHelper.decode(T.self, from: unwrappedData)
|
||||
|
||||
switch decodeResult {
|
||||
case let .success(decodableObj):
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj)))
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj, bodyData: unwrappedData)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, data, response, error)))
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, unwrappedData, response, error)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,14 +86,16 @@ open class Response<T> {
|
||||
public let statusCode: Int
|
||||
public let header: [String: String]
|
||||
public let body: T
|
||||
public let bodyData: Data?
|
||||
|
||||
public init(statusCode: Int, header: [String: String], body: T) {
|
||||
public init(statusCode: Int, header: [String: String], body: T, bodyData: Data?) {
|
||||
self.statusCode = statusCode
|
||||
self.header = header
|
||||
self.body = body
|
||||
self.bodyData = bodyData
|
||||
}
|
||||
|
||||
public convenience init(response: HTTPURLResponse, body: T) {
|
||||
public convenience init(response: HTTPURLResponse, body: T, bodyData: Data?) {
|
||||
let rawHeader = response.allHeaderFields
|
||||
var header = [String: String]()
|
||||
for (key, value) in rawHeader {
|
||||
@ -101,7 +103,7 @@ open class Response<T> {
|
||||
header[key] = value
|
||||
}
|
||||
}
|
||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||
self.init(statusCode: response.statusCode, header: header, body: body, bodyData: bodyData)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
switch T.self {
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
|
||||
@ -303,7 +303,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
|
||||
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T)))
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T, bodyData: data)))
|
||||
|
||||
case is URL.Type:
|
||||
do {
|
||||
@ -334,7 +334,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T, bodyData: data)))
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
@ -344,30 +344,30 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
case is Data.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: data as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: data as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
|
||||
guard let data = data, !data.isEmpty else {
|
||||
guard let unwrappedData = data, !unwrappedData.isEmpty else {
|
||||
if let E = T.self as? ExpressibleByNilLiteral.Type {
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T, bodyData: data)))
|
||||
} else {
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, nil, response, DecodableRequestBuilderError.emptyDataResponse)))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let decodeResult = CodableHelper.decode(T.self, from: data)
|
||||
let decodeResult = CodableHelper.decode(T.self, from: unwrappedData)
|
||||
|
||||
switch decodeResult {
|
||||
case let .success(decodableObj):
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj)))
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj, bodyData: unwrappedData)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, data, response, error)))
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, unwrappedData, response, error)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,14 +86,16 @@ open class Response<T> {
|
||||
public let statusCode: Int
|
||||
public let header: [String: String]
|
||||
public let body: T
|
||||
public let bodyData: Data?
|
||||
|
||||
public init(statusCode: Int, header: [String: String], body: T) {
|
||||
public init(statusCode: Int, header: [String: String], body: T, bodyData: Data?) {
|
||||
self.statusCode = statusCode
|
||||
self.header = header
|
||||
self.body = body
|
||||
self.bodyData = bodyData
|
||||
}
|
||||
|
||||
public convenience init(response: HTTPURLResponse, body: T) {
|
||||
public convenience init(response: HTTPURLResponse, body: T, bodyData: Data?) {
|
||||
let rawHeader = response.allHeaderFields
|
||||
var header = [String: String]()
|
||||
for (key, value) in rawHeader {
|
||||
@ -101,7 +103,7 @@ open class Response<T> {
|
||||
header[key] = value
|
||||
}
|
||||
}
|
||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||
self.init(statusCode: response.statusCode, header: header, body: body, bodyData: bodyData)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
switch T.self {
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
|
||||
@ -303,7 +303,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
|
||||
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T)))
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T, bodyData: data)))
|
||||
|
||||
case is URL.Type:
|
||||
do {
|
||||
@ -334,7 +334,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T, bodyData: data)))
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
@ -344,30 +344,30 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
case is Data.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: data as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: data as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
|
||||
guard let data = data, !data.isEmpty else {
|
||||
guard let unwrappedData = data, !unwrappedData.isEmpty else {
|
||||
if let E = T.self as? ExpressibleByNilLiteral.Type {
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T, bodyData: data)))
|
||||
} else {
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, nil, response, DecodableRequestBuilderError.emptyDataResponse)))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let decodeResult = CodableHelper.decode(T.self, from: data)
|
||||
let decodeResult = CodableHelper.decode(T.self, from: unwrappedData)
|
||||
|
||||
switch decodeResult {
|
||||
case let .success(decodableObj):
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj)))
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj, bodyData: unwrappedData)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, data, response, error)))
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, unwrappedData, response, error)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,14 +86,16 @@ open class Response<T> {
|
||||
public let statusCode: Int
|
||||
public let header: [String: String]
|
||||
public let body: T
|
||||
public let bodyData: Data?
|
||||
|
||||
public init(statusCode: Int, header: [String: String], body: T) {
|
||||
public init(statusCode: Int, header: [String: String], body: T, bodyData: Data?) {
|
||||
self.statusCode = statusCode
|
||||
self.header = header
|
||||
self.body = body
|
||||
self.bodyData = bodyData
|
||||
}
|
||||
|
||||
public convenience init(response: HTTPURLResponse, body: T) {
|
||||
public convenience init(response: HTTPURLResponse, body: T, bodyData: Data?) {
|
||||
let rawHeader = response.allHeaderFields
|
||||
var header = [String: String]()
|
||||
for (key, value) in rawHeader {
|
||||
@ -101,7 +103,7 @@ open class Response<T> {
|
||||
header[key] = value
|
||||
}
|
||||
}
|
||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||
self.init(statusCode: response.statusCode, header: header, body: body, bodyData: bodyData)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
switch T.self {
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
|
||||
@ -303,7 +303,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
|
||||
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T)))
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T, bodyData: data)))
|
||||
|
||||
case is URL.Type:
|
||||
do {
|
||||
@ -334,7 +334,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T, bodyData: data)))
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
@ -344,30 +344,30 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
case is Data.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: data as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: data as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
|
||||
guard let data = data, !data.isEmpty else {
|
||||
guard let unwrappedData = data, !unwrappedData.isEmpty else {
|
||||
if let E = T.self as? ExpressibleByNilLiteral.Type {
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T, bodyData: data)))
|
||||
} else {
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, nil, response, DecodableRequestBuilderError.emptyDataResponse)))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let decodeResult = CodableHelper.decode(T.self, from: data)
|
||||
let decodeResult = CodableHelper.decode(T.self, from: unwrappedData)
|
||||
|
||||
switch decodeResult {
|
||||
case let .success(decodableObj):
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj)))
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj, bodyData: unwrappedData)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, data, response, error)))
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, unwrappedData, response, error)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,14 +86,16 @@ open class Response<T> {
|
||||
public let statusCode: Int
|
||||
public let header: [String: String]
|
||||
public let body: T
|
||||
public let bodyData: Data?
|
||||
|
||||
public init(statusCode: Int, header: [String: String], body: T) {
|
||||
public init(statusCode: Int, header: [String: String], body: T, bodyData: Data?) {
|
||||
self.statusCode = statusCode
|
||||
self.header = header
|
||||
self.body = body
|
||||
self.bodyData = bodyData
|
||||
}
|
||||
|
||||
public convenience init(response: HTTPURLResponse, body: T) {
|
||||
public convenience init(response: HTTPURLResponse, body: T, bodyData: Data?) {
|
||||
let rawHeader = response.allHeaderFields
|
||||
var header = [String: String]()
|
||||
for (key, value) in rawHeader {
|
||||
@ -101,7 +103,7 @@ open class Response<T> {
|
||||
header[key] = value
|
||||
}
|
||||
}
|
||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||
self.init(statusCode: response.statusCode, header: header, body: body, bodyData: bodyData)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
switch T.self {
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
|
||||
@ -303,7 +303,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
|
||||
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T)))
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T, bodyData: data)))
|
||||
|
||||
case is URL.Type:
|
||||
do {
|
||||
@ -334,7 +334,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T, bodyData: data)))
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
@ -344,30 +344,30 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
case is Data.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: data as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: data as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
|
||||
guard let data = data, !data.isEmpty else {
|
||||
guard let unwrappedData = data, !unwrappedData.isEmpty else {
|
||||
if let E = T.self as? ExpressibleByNilLiteral.Type {
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T, bodyData: data)))
|
||||
} else {
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, nil, response, DecodableRequestBuilderError.emptyDataResponse)))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let decodeResult = CodableHelper.decode(T.self, from: data)
|
||||
let decodeResult = CodableHelper.decode(T.self, from: unwrappedData)
|
||||
|
||||
switch decodeResult {
|
||||
case let .success(decodableObj):
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj)))
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj, bodyData: unwrappedData)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, data, response, error)))
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, unwrappedData, response, error)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,14 +86,16 @@ open class Response<T> {
|
||||
public let statusCode: Int
|
||||
public let header: [String: String]
|
||||
public let body: T
|
||||
public let bodyData: Data?
|
||||
|
||||
public init(statusCode: Int, header: [String: String], body: T) {
|
||||
public init(statusCode: Int, header: [String: String], body: T, bodyData: Data?) {
|
||||
self.statusCode = statusCode
|
||||
self.header = header
|
||||
self.body = body
|
||||
self.bodyData = bodyData
|
||||
}
|
||||
|
||||
public convenience init(response: HTTPURLResponse, body: T) {
|
||||
public convenience init(response: HTTPURLResponse, body: T, bodyData: Data?) {
|
||||
let rawHeader = response.allHeaderFields
|
||||
var header = [String: String]()
|
||||
for (key, value) in rawHeader {
|
||||
@ -101,7 +103,7 @@ open class Response<T> {
|
||||
header[key] = value
|
||||
}
|
||||
}
|
||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||
self.init(statusCode: response.statusCode, header: header, body: body, bodyData: bodyData)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
switch T.self {
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
|
||||
@ -303,7 +303,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
|
||||
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T)))
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T, bodyData: data)))
|
||||
|
||||
case is URL.Type:
|
||||
do {
|
||||
@ -334,7 +334,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T, bodyData: data)))
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
@ -344,30 +344,30 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
case is Data.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: data as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: data as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
|
||||
guard let data = data, !data.isEmpty else {
|
||||
guard let unwrappedData = data, !unwrappedData.isEmpty else {
|
||||
if let E = T.self as? ExpressibleByNilLiteral.Type {
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T, bodyData: data)))
|
||||
} else {
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, nil, response, DecodableRequestBuilderError.emptyDataResponse)))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let decodeResult = CodableHelper.decode(T.self, from: data)
|
||||
let decodeResult = CodableHelper.decode(T.self, from: unwrappedData)
|
||||
|
||||
switch decodeResult {
|
||||
case let .success(decodableObj):
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj)))
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj, bodyData: unwrappedData)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, data, response, error)))
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, unwrappedData, response, error)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,14 +86,16 @@ open class Response<T> {
|
||||
public let statusCode: Int
|
||||
public let header: [String: String]
|
||||
public let body: T
|
||||
public let bodyData: Data?
|
||||
|
||||
public init(statusCode: Int, header: [String: String], body: T) {
|
||||
public init(statusCode: Int, header: [String: String], body: T, bodyData: Data?) {
|
||||
self.statusCode = statusCode
|
||||
self.header = header
|
||||
self.body = body
|
||||
self.bodyData = bodyData
|
||||
}
|
||||
|
||||
public convenience init(response: HTTPURLResponse, body: T) {
|
||||
public convenience init(response: HTTPURLResponse, body: T, bodyData: Data?) {
|
||||
let rawHeader = response.allHeaderFields
|
||||
var header = [String: String]()
|
||||
for (key, value) in rawHeader {
|
||||
@ -101,7 +103,7 @@ open class Response<T> {
|
||||
header[key] = value
|
||||
}
|
||||
}
|
||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||
self.init(statusCode: response.statusCode, header: header, body: body, bodyData: bodyData)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
switch T.self {
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
|
||||
@ -303,7 +303,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
|
||||
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T)))
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T, bodyData: data)))
|
||||
|
||||
case is URL.Type:
|
||||
do {
|
||||
@ -334,7 +334,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T, bodyData: data)))
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
@ -344,30 +344,30 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
case is Data.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: data as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: data as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
|
||||
guard let data = data, !data.isEmpty else {
|
||||
guard let unwrappedData = data, !unwrappedData.isEmpty else {
|
||||
if let E = T.self as? ExpressibleByNilLiteral.Type {
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T, bodyData: data)))
|
||||
} else {
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, nil, response, DecodableRequestBuilderError.emptyDataResponse)))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let decodeResult = CodableHelper.decode(T.self, from: data)
|
||||
let decodeResult = CodableHelper.decode(T.self, from: unwrappedData)
|
||||
|
||||
switch decodeResult {
|
||||
case let .success(decodableObj):
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj)))
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj, bodyData: unwrappedData)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, data, response, error)))
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, unwrappedData, response, error)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,14 +86,16 @@ open class Response<T> {
|
||||
public let statusCode: Int
|
||||
public let header: [String: String]
|
||||
public let body: T
|
||||
public let bodyData: Data?
|
||||
|
||||
public init(statusCode: Int, header: [String: String], body: T) {
|
||||
public init(statusCode: Int, header: [String: String], body: T, bodyData: Data?) {
|
||||
self.statusCode = statusCode
|
||||
self.header = header
|
||||
self.body = body
|
||||
self.bodyData = bodyData
|
||||
}
|
||||
|
||||
public convenience init(response: HTTPURLResponse, body: T) {
|
||||
public convenience init(response: HTTPURLResponse, body: T, bodyData: Data?) {
|
||||
let rawHeader = response.allHeaderFields
|
||||
var header = [String: String]()
|
||||
for (key, value) in rawHeader {
|
||||
@ -101,7 +103,7 @@ open class Response<T> {
|
||||
header[key] = value
|
||||
}
|
||||
}
|
||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||
self.init(statusCode: response.statusCode, header: header, body: body, bodyData: bodyData)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
switch T.self {
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
|
||||
@ -303,7 +303,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
|
||||
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T)))
|
||||
completion(.success(Response<T>(response: httpResponse, body: body as! T, bodyData: data)))
|
||||
|
||||
case is URL.Type:
|
||||
do {
|
||||
@ -334,7 +334,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
|
||||
try data.write(to: filePath, options: .atomic)
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: filePath as! T, bodyData: data)))
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
@ -344,30 +344,30 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
|
||||
|
||||
case is Void.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: () as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: () as! T, bodyData: data)))
|
||||
|
||||
case is Data.Type:
|
||||
|
||||
completion(.success(Response(response: httpResponse, body: data as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: data as! T, bodyData: data)))
|
||||
|
||||
default:
|
||||
|
||||
guard let data = data, !data.isEmpty else {
|
||||
guard let unwrappedData = data, !unwrappedData.isEmpty else {
|
||||
if let E = T.self as? ExpressibleByNilLiteral.Type {
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T)))
|
||||
completion(.success(Response(response: httpResponse, body: E.init(nilLiteral: ()) as! T, bodyData: data)))
|
||||
} else {
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, nil, response, DecodableRequestBuilderError.emptyDataResponse)))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let decodeResult = CodableHelper.decode(T.self, from: data)
|
||||
let decodeResult = CodableHelper.decode(T.self, from: unwrappedData)
|
||||
|
||||
switch decodeResult {
|
||||
case let .success(decodableObj):
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj)))
|
||||
completion(.success(Response(response: httpResponse, body: decodableObj, bodyData: unwrappedData)))
|
||||
case let .failure(error):
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, data, response, error)))
|
||||
completion(.failure(ErrorResponse.error(httpResponse.statusCode, unwrappedData, response, error)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user