Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)))
Expand Down Expand Up @@ -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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)))
Expand Down Expand Up @@ -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)))
Expand Down
Loading