[swift5][client] fix binary response (#9595)

* [swift5][client] fix binary respose

* [swift5][client] fix binary respose
This commit is contained in:
Bruno Coelho
2021-05-27 03:18:51 +01:00
committed by GitHub
parent b5843f41a6
commit a61db6e596
15 changed files with 709 additions and 30 deletions

View File

@@ -190,16 +190,18 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
let fileManager = FileManager.default
let urlRequest = try request.asURLRequest()
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try self.getURL(from: urlRequest)
var requestPath = try self.getPath(from: requestURL)
if let headerFileName = self.getFileName(fromContentDisposition: dataResponse.response?.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.{{projectName}}.\(UUID().uuidString)")
}
let filePath = documentsDirectory.appendingPathComponent(requestPath)
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
@@ -225,6 +227,18 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
completion(.failure(ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.response, error)))
}
})
case is Data.Type:
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { dataResponse in
cleanupRequest()
switch dataResponse.result {
case .success:
completion(.success(Response(response: dataResponse.response!, body: dataResponse.data as? T)))
case let .failure(error):
completion(.failure(ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.response, error)))
}
})
default:
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { dataResponse in
@@ -328,6 +342,52 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
}
})
case is URL.Type:
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { dataResponse in
cleanupRequest()
do {
guard !dataResponse.result.isFailure else {
throw DownloadException.responseFailed
}
guard let data = dataResponse.data else {
throw DownloadException.responseDataMissing
}
guard let request = request.request else {
throw DownloadException.requestMissing
}
let fileManager = FileManager.default
let urlRequest = try request.asURLRequest()
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try self.getURL(from: urlRequest)
var requestPath = try self.getPath(from: requestURL)
if let headerFileName = self.getFileName(fromContentDisposition: dataResponse.response?.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.{{projectName}}.\(UUID().uuidString)")
}
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, dataResponse.data, dataResponse.response, requestParserError)))
} catch {
completion(.failure(ErrorResponse.error(400, dataResponse.data, dataResponse.response, error)))
}
return
})
case is Void.Type:
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { voidResponse in
cleanupRequest()

View File

@@ -205,16 +205,18 @@ private var urlSessionStore = SynchronizedDictionary<String, URLSession>()
}
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.{{projectName}}.\(UUID().uuidString)")
}
let filePath = documentsDirectory.appendingPathComponent(requestPath)
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
@@ -232,6 +234,10 @@ private var urlSessionStore = SynchronizedDictionary<String, URLSession>()
completion(.success(Response(response: httpResponse, body: nil)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
@@ -328,6 +334,43 @@ private var urlSessionStore = SynchronizedDictionary<String, URLSession>()
completion(.success(Response<T>(response: httpResponse, body: body as? T)))
case is URL.Type:
do {
guard error == nil else {
throw DownloadException.responseFailed
}
guard let data = data else {
throw DownloadException.responseDataMissing
}
let fileManager = FileManager.default
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.{{projectName}}.\(UUID().uuidString)")
}
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
} catch {
completion(.failure(ErrorResponse.error(400, data, response, error)))
}
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))

View File

@@ -190,16 +190,18 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
let fileManager = FileManager.default
let urlRequest = try request.asURLRequest()
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try self.getURL(from: urlRequest)
var requestPath = try self.getPath(from: requestURL)
if let headerFileName = self.getFileName(fromContentDisposition: dataResponse.response?.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = documentsDirectory.appendingPathComponent(requestPath)
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
@@ -225,6 +227,18 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
completion(.failure(ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.response, error)))
}
})
case is Data.Type:
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { dataResponse in
cleanupRequest()
switch dataResponse.result {
case .success:
completion(.success(Response(response: dataResponse.response!, body: dataResponse.data as? T)))
case let .failure(error):
completion(.failure(ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.response, error)))
}
})
default:
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { dataResponse in
@@ -328,6 +342,52 @@ open class AlamofireDecodableRequestBuilder<T: Decodable>: AlamofireRequestBuild
}
})
case is URL.Type:
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { dataResponse in
cleanupRequest()
do {
guard !dataResponse.result.isFailure else {
throw DownloadException.responseFailed
}
guard let data = dataResponse.data else {
throw DownloadException.responseDataMissing
}
guard let request = request.request else {
throw DownloadException.requestMissing
}
let fileManager = FileManager.default
let urlRequest = try request.asURLRequest()
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try self.getURL(from: urlRequest)
var requestPath = try self.getPath(from: requestURL)
if let headerFileName = self.getFileName(fromContentDisposition: dataResponse.response?.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, dataResponse.data, dataResponse.response, requestParserError)))
} catch {
completion(.failure(ErrorResponse.error(400, dataResponse.data, dataResponse.response, error)))
}
return
})
case is Void.Type:
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { voidResponse in
cleanupRequest()

View File

@@ -205,16 +205,18 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = documentsDirectory.appendingPathComponent(requestPath)
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
@@ -232,6 +234,10 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
completion(.success(Response(response: httpResponse, body: nil)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
@@ -328,6 +334,43 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
completion(.success(Response<T>(response: httpResponse, body: body as? T)))
case is URL.Type:
do {
guard error == nil else {
throw DownloadException.responseFailed
}
guard let data = data else {
throw DownloadException.responseDataMissing
}
let fileManager = FileManager.default
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
} catch {
completion(.failure(ErrorResponse.error(400, data, response, error)))
}
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))

View File

@@ -205,16 +205,18 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = documentsDirectory.appendingPathComponent(requestPath)
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
@@ -232,6 +234,10 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
completion(.success(Response(response: httpResponse, body: nil)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
@@ -328,6 +334,43 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
completion(.success(Response<T>(response: httpResponse, body: body as? T)))
case is URL.Type:
do {
guard error == nil else {
throw DownloadException.responseFailed
}
guard let data = data else {
throw DownloadException.responseDataMissing
}
let fileManager = FileManager.default
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
} catch {
completion(.failure(ErrorResponse.error(400, data, response, error)))
}
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))

View File

@@ -205,16 +205,18 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = documentsDirectory.appendingPathComponent(requestPath)
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
@@ -232,6 +234,10 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
completion(.success(Response(response: httpResponse, body: nil)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
@@ -328,6 +334,43 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
completion(.success(Response<T>(response: httpResponse, body: body as? T)))
case is URL.Type:
do {
guard error == nil else {
throw DownloadException.responseFailed
}
guard let data = data else {
throw DownloadException.responseDataMissing
}
let fileManager = FileManager.default
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
} catch {
completion(.failure(ErrorResponse.error(400, data, response, error)))
}
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))

View File

@@ -205,16 +205,18 @@ internal class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = documentsDirectory.appendingPathComponent(requestPath)
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
@@ -232,6 +234,10 @@ internal class URLSessionRequestBuilder<T>: RequestBuilder<T> {
completion(.success(Response(response: httpResponse, body: nil)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
@@ -328,6 +334,43 @@ internal class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionReques
completion(.success(Response<T>(response: httpResponse, body: body as? T)))
case is URL.Type:
do {
guard error == nil else {
throw DownloadException.responseFailed
}
guard let data = data else {
throw DownloadException.responseDataMissing
}
let fileManager = FileManager.default
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
} catch {
completion(.failure(ErrorResponse.error(400, data, response, error)))
}
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))

View File

@@ -205,16 +205,18 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = documentsDirectory.appendingPathComponent(requestPath)
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
@@ -232,6 +234,10 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
completion(.success(Response(response: httpResponse, body: nil)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
@@ -328,6 +334,43 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
completion(.success(Response<T>(response: httpResponse, body: body as? T)))
case is URL.Type:
do {
guard error == nil else {
throw DownloadException.responseFailed
}
guard let data = data else {
throw DownloadException.responseDataMissing
}
let fileManager = FileManager.default
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
} catch {
completion(.failure(ErrorResponse.error(400, data, response, error)))
}
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))

View File

@@ -205,16 +205,18 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = documentsDirectory.appendingPathComponent(requestPath)
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
@@ -232,6 +234,10 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
completion(.success(Response(response: httpResponse, body: nil)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
@@ -328,6 +334,43 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
completion(.success(Response<T>(response: httpResponse, body: body as? T)))
case is URL.Type:
do {
guard error == nil else {
throw DownloadException.responseFailed
}
guard let data = data else {
throw DownloadException.responseDataMissing
}
let fileManager = FileManager.default
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
} catch {
completion(.failure(ErrorResponse.error(400, data, response, error)))
}
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))

View File

@@ -205,16 +205,18 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = documentsDirectory.appendingPathComponent(requestPath)
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
@@ -232,6 +234,10 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
completion(.success(Response(response: httpResponse, body: nil)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
@@ -328,6 +334,43 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
completion(.success(Response<T>(response: httpResponse, body: body as? T)))
case is URL.Type:
do {
guard error == nil else {
throw DownloadException.responseFailed
}
guard let data = data else {
throw DownloadException.responseDataMissing
}
let fileManager = FileManager.default
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
} catch {
completion(.failure(ErrorResponse.error(400, data, response, error)))
}
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))

View File

@@ -205,16 +205,18 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = documentsDirectory.appendingPathComponent(requestPath)
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
@@ -232,6 +234,10 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
completion(.success(Response(response: httpResponse, body: nil)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
@@ -328,6 +334,43 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
completion(.success(Response<T>(response: httpResponse, body: body as? T)))
case is URL.Type:
do {
guard error == nil else {
throw DownloadException.responseFailed
}
guard let data = data else {
throw DownloadException.responseDataMissing
}
let fileManager = FileManager.default
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
} catch {
completion(.failure(ErrorResponse.error(400, data, response, error)))
}
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))

View File

@@ -205,16 +205,18 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = documentsDirectory.appendingPathComponent(requestPath)
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
@@ -232,6 +234,10 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
completion(.success(Response(response: httpResponse, body: nil)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
@@ -328,6 +334,43 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
completion(.success(Response<T>(response: httpResponse, body: body as? T)))
case is URL.Type:
do {
guard error == nil else {
throw DownloadException.responseFailed
}
guard let data = data else {
throw DownloadException.responseDataMissing
}
let fileManager = FileManager.default
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
} catch {
completion(.failure(ErrorResponse.error(400, data, response, error)))
}
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))

View File

@@ -205,16 +205,18 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = documentsDirectory.appendingPathComponent(requestPath)
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
@@ -232,6 +234,10 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
completion(.success(Response(response: httpResponse, body: nil)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
@@ -328,6 +334,43 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
completion(.success(Response<T>(response: httpResponse, body: body as? T)))
case is URL.Type:
do {
guard error == nil else {
throw DownloadException.responseFailed
}
guard let data = data else {
throw DownloadException.responseDataMissing
}
let fileManager = FileManager.default
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
} catch {
completion(.failure(ErrorResponse.error(400, data, response, error)))
}
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))

View File

@@ -205,16 +205,18 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = documentsDirectory.appendingPathComponent(requestPath)
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
@@ -232,6 +234,10 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
completion(.success(Response(response: httpResponse, body: nil)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
@@ -328,6 +334,43 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
completion(.success(Response<T>(response: httpResponse, body: body as? T)))
case is URL.Type:
do {
guard error == nil else {
throw DownloadException.responseFailed
}
guard let data = data else {
throw DownloadException.responseDataMissing
}
let fileManager = FileManager.default
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
} catch {
completion(.failure(ErrorResponse.error(400, data, response, error)))
}
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))

View File

@@ -205,16 +205,18 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
let fileManager = FileManager.default
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = documentsDirectory.appendingPathComponent(requestPath)
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
try fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true, attributes: nil)
@@ -232,6 +234,10 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
completion(.success(Response(response: httpResponse, body: nil)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
@@ -328,6 +334,43 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
completion(.success(Response<T>(response: httpResponse, body: body as? T)))
case is URL.Type:
do {
guard error == nil else {
throw DownloadException.responseFailed
}
guard let data = data else {
throw DownloadException.responseDataMissing
}
let fileManager = FileManager.default
let cachesDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let requestURL = try getURL(from: urlRequest)
var requestPath = try getPath(from: requestURL)
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
requestPath = requestPath.appending("/\(headerFileName)")
} else {
requestPath = requestPath.appending("/tmp.PetstoreClient.\(UUID().uuidString)")
}
let filePath = cachesDirectory.appendingPathComponent(requestPath)
let directoryPath = filePath.deletingLastPathComponent().path
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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
} catch {
completion(.failure(ErrorResponse.error(400, data, response, error)))
}
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))