[swift5] Remove optional from body (#10938)

* Unwrap body

* Remove unsupported type from nonDecodableBuilder

* Update samples

* Remove !

* Fix typo
This commit is contained in:
Fumito Nakazawa 2021-11-23 22:18:56 +09:00 committed by GitHub
parent faae00df91
commit 64a478a93c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
62 changed files with 231 additions and 1063 deletions

View File

@ -33,15 +33,15 @@ protocol JSONEncodable {
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class Response<T> {
{{#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 body: T
{{#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) {
self.statusCode = statusCode
self.header = header
self.body = body
}
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} convenience init(response: HTTPURLResponse, body: T?) {
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} convenience init(response: HTTPURLResponse, body: T) {
let rawHeader = response.allHeaderFields
var header = [String: String]()
for (key, value) in rawHeader {

View File

@ -97,7 +97,7 @@ extension {{projectName}}API {
switch result {
{{#returnType}}
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
{{/returnType}}
{{^returnType}}
case .success:
@ -128,7 +128,7 @@ extension {{projectName}}API {
switch result {
{{#returnType}}
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
{{/returnType}}
{{^returnType}}
case .success:
@ -167,7 +167,7 @@ extension {{projectName}}API {
switch result {
{{#returnType}}
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
{{/returnType}}
{{^returnType}}
case .success:
@ -212,7 +212,7 @@ extension {{projectName}}API {
switch result {
{{#returnType}}
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
{{/returnType}}
{{^returnType}}
case .success:
@ -246,7 +246,7 @@ extension {{projectName}}API {
switch result {
{{#returnType}}
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
{{/returnType}}
{{^returnType}}
case .success:

View File

@ -160,100 +160,20 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.Session>()
let validatedRequest = request.validate()
switch T.self {
case is String.Type:
validatedRequest.responseString(queue: apiResponseQueue, completionHandler: { stringResponse in
cleanupRequest()
switch stringResponse.result {
case let .success(value):
completion(.success(Response(response: stringResponse.response!, body: value as? T)))
case let .failure(error):
completion(.failure(ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.response, error)))
}
})
case is URL.Type:
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { dataResponse in
cleanupRequest()
do {
guard case .success = dataResponse.result 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()
switch voidResponse.result {
case .success:
completion(.success(Response(response: voidResponse.response!, body: nil)))
completion(.success(Response(response: voidResponse.response!, body: () as! T)))
case let .failure(error):
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
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)))
}
})
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
}
}
@ -338,7 +258,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)))
case let .failure(error):
completion(.failure(ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.response, error)))
}
@ -381,7 +301,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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, dataResponse.data, dataResponse.response, requestParserError)))
@ -396,7 +316,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.Session>()
switch voidResponse.result {
case .success:
completion(.success(Response(response: voidResponse.response!, body: nil)))
completion(.success(Response(response: voidResponse.response!, body: () as! T)))
case let .failure(error):
completion(.failure(ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.response, error)))
}
@ -408,7 +328,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)))
case let .failure(error):
completion(.failure(ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.response, error)))
}

View File

@ -200,60 +200,12 @@ private var credentialStore = SynchronizedDictionary<Int, URLCredential>()
}
switch T.self {
case is String.Type:
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
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)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: () as! T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
}
}
@ -345,7 +297,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)))
case is URL.Type:
do {
@ -376,7 +328,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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
@ -386,11 +338,11 @@ private var credentialStore = SynchronizedDictionary<Int, URLCredential>()
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))
completion(.success(Response(response: httpResponse, body: () as! T)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: data as! T)))
default:

View File

@ -160,100 +160,20 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
let validatedRequest = request.validate()
switch T.self {
case is String.Type:
validatedRequest.responseString(queue: apiResponseQueue, completionHandler: { stringResponse in
cleanupRequest()
switch stringResponse.result {
case let .success(value):
completion(.success(Response(response: stringResponse.response!, body: value as? T)))
case let .failure(error):
completion(.failure(ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.response, error)))
}
})
case is URL.Type:
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { dataResponse in
cleanupRequest()
do {
guard case .success = dataResponse.result 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()
switch voidResponse.result {
case .success:
completion(.success(Response(response: voidResponse.response!, body: nil)))
completion(.success(Response(response: voidResponse.response!, body: () as! T)))
case let .failure(error):
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
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)))
}
})
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
}
}
@ -338,7 +258,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)))
case let .failure(error):
completion(.failure(ErrorResponse.error(stringResponse.response?.statusCode ?? 500, stringResponse.data, stringResponse.response, error)))
}
@ -381,7 +301,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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, dataResponse.data, dataResponse.response, requestParserError)))
@ -396,7 +316,7 @@ open class AlamofireDecodableRequestBuilder<T: Decodable>: AlamofireRequestBuild
switch voidResponse.result {
case .success:
completion(.success(Response(response: voidResponse.response!, body: nil)))
completion(.success(Response(response: voidResponse.response!, body: () as! T)))
case let .failure(error):
completion(.failure(ErrorResponse.error(voidResponse.response?.statusCode ?? 500, voidResponse.data, voidResponse.response, error)))
}
@ -408,7 +328,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)))
case let .failure(error):
completion(.failure(ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.response, error)))
}

View File

@ -33,15 +33,15 @@ public enum DecodableRequestBuilderError: Error {
open class Response<T> {
public let statusCode: Int
public let header: [String: String]
public let body: T?
public let body: T
public init(statusCode: Int, header: [String: String], body: T?) {
public init(statusCode: Int, header: [String: String], body: T) {
self.statusCode = statusCode
self.header = header
self.body = body
}
public convenience init(response: HTTPURLResponse, body: T?) {
public convenience init(response: HTTPURLResponse, body: T) {
let rawHeader = response.allHeaderFields
var header = [String: String]()
for (key, value) in rawHeader {

View File

@ -33,7 +33,7 @@ open class AnotherFakeAPI {
task = call123testSpecialTagsWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
case let .failure(error):
continuation.resume(throwing: error)
}

View File

@ -32,7 +32,7 @@ open class FakeAPI {
task = fakeOuterBooleanSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
case let .failure(error):
continuation.resume(throwing: error)
}
@ -87,7 +87,7 @@ open class FakeAPI {
task = fakeOuterCompositeSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
case let .failure(error):
continuation.resume(throwing: error)
}
@ -142,7 +142,7 @@ open class FakeAPI {
task = fakeOuterNumberSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
case let .failure(error):
continuation.resume(throwing: error)
}
@ -197,7 +197,7 @@ open class FakeAPI {
task = fakeOuterStringSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
case let .failure(error):
continuation.resume(throwing: error)
}
@ -367,7 +367,7 @@ open class FakeAPI {
task = testClientModelWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
case let .failure(error):
continuation.resume(throwing: error)
}

View File

@ -33,7 +33,7 @@ open class FakeClassnameTags123API {
task = testClassnameWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
case let .failure(error):
continuation.resume(throwing: error)
}

View File

@ -165,7 +165,7 @@ open class PetAPI {
task = findPetsByStatusWithRequestBuilder(status: status).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
case let .failure(error):
continuation.resume(throwing: error)
}
@ -229,7 +229,7 @@ open class PetAPI {
task = findPetsByTagsWithRequestBuilder(tags: tags).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
case let .failure(error):
continuation.resume(throwing: error)
}
@ -293,7 +293,7 @@ open class PetAPI {
task = getPetByIdWithRequestBuilder(petId: petId).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
case let .failure(error):
continuation.resume(throwing: error)
}
@ -489,7 +489,7 @@ open class PetAPI {
task = uploadFileWithRequestBuilder(petId: petId, additionalMetadata: additionalMetadata, file: file).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
case let .failure(error):
continuation.resume(throwing: error)
}
@ -561,7 +561,7 @@ open class PetAPI {
task = uploadFileWithRequiredFileWithRequestBuilder(petId: petId, requiredFile: requiredFile, additionalMetadata: additionalMetadata).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
case let .failure(error):
continuation.resume(throwing: error)
}

View File

@ -92,7 +92,7 @@ open class StoreAPI {
task = getInventoryWithRequestBuilder().execute(apiResponseQueue) { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
case let .failure(error):
continuation.resume(throwing: error)
}
@ -151,7 +151,7 @@ open class StoreAPI {
task = getOrderByIdWithRequestBuilder(orderId: orderId).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
case let .failure(error):
continuation.resume(throwing: error)
}
@ -211,7 +211,7 @@ open class StoreAPI {
task = placeOrderWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
case let .failure(error):
continuation.resume(throwing: error)
}

View File

@ -262,7 +262,7 @@ open class UserAPI {
task = getUserByNameWithRequestBuilder(username: username).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
case let .failure(error):
continuation.resume(throwing: error)
}
@ -322,7 +322,7 @@ open class UserAPI {
task = loginUserWithRequestBuilder(username: username, password: password).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
continuation.resume(returning: response.body!)
continuation.resume(returning: response.body)
case let .failure(error):
continuation.resume(throwing: error)
}

View File

@ -33,15 +33,15 @@ public enum DecodableRequestBuilderError: Error {
open class Response<T> {
public let statusCode: Int
public let header: [String: String]
public let body: T?
public let body: T
public init(statusCode: Int, header: [String: String], body: T?) {
public init(statusCode: Int, header: [String: String], body: T) {
self.statusCode = statusCode
self.header = header
self.body = body
}
public convenience init(response: HTTPURLResponse, body: T?) {
public convenience init(response: HTTPURLResponse, body: T) {
let rawHeader = response.allHeaderFields
var header = [String: String]()
for (key, value) in rawHeader {

View File

@ -200,60 +200,12 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
switch T.self {
case is String.Type:
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
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)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: () as! T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
}
}
@ -345,7 +297,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)))
case is URL.Type:
do {
@ -376,7 +328,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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
@ -386,11 +338,11 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))
completion(.success(Response(response: httpResponse, body: () as! T)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: data as! T)))
default:

View File

@ -30,7 +30,7 @@ open class AnotherFakeAPI {
task = call123testSpecialTagsWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
case let .failure(error):
promise(.failure(error))
}

View File

@ -29,7 +29,7 @@ open class FakeAPI {
task = fakeOuterBooleanSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
case let .failure(error):
promise(.failure(error))
}
@ -80,7 +80,7 @@ open class FakeAPI {
task = fakeOuterCompositeSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
case let .failure(error):
promise(.failure(error))
}
@ -131,7 +131,7 @@ open class FakeAPI {
task = fakeOuterNumberSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
case let .failure(error):
promise(.failure(error))
}
@ -182,7 +182,7 @@ open class FakeAPI {
task = fakeOuterStringSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
case let .failure(error):
promise(.failure(error))
}
@ -340,7 +340,7 @@ open class FakeAPI {
task = testClientModelWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
case let .failure(error):
promise(.failure(error))
}

View File

@ -30,7 +30,7 @@ open class FakeClassnameTags123API {
task = testClassnameWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
case let .failure(error):
promise(.failure(error))
}

View File

@ -154,7 +154,7 @@ open class PetAPI {
task = findPetsByStatusWithRequestBuilder(status: status).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
case let .failure(error):
promise(.failure(error))
}
@ -214,7 +214,7 @@ open class PetAPI {
task = findPetsByTagsWithRequestBuilder(tags: tags).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
case let .failure(error):
promise(.failure(error))
}
@ -274,7 +274,7 @@ open class PetAPI {
task = getPetByIdWithRequestBuilder(petId: petId).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
case let .failure(error):
promise(.failure(error))
}
@ -458,7 +458,7 @@ open class PetAPI {
task = uploadFileWithRequestBuilder(petId: petId, additionalMetadata: additionalMetadata, file: file).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
case let .failure(error):
promise(.failure(error))
}
@ -526,7 +526,7 @@ open class PetAPI {
task = uploadFileWithRequiredFileWithRequestBuilder(petId: petId, requiredFile: requiredFile, additionalMetadata: additionalMetadata).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
case let .failure(error):
promise(.failure(error))
}

View File

@ -85,7 +85,7 @@ open class StoreAPI {
task = getInventoryWithRequestBuilder().execute(apiResponseQueue) { result in
switch result {
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
case let .failure(error):
promise(.failure(error))
}
@ -140,7 +140,7 @@ open class StoreAPI {
task = getOrderByIdWithRequestBuilder(orderId: orderId).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
case let .failure(error):
promise(.failure(error))
}
@ -196,7 +196,7 @@ open class StoreAPI {
task = placeOrderWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
case let .failure(error):
promise(.failure(error))
}

View File

@ -243,7 +243,7 @@ open class UserAPI {
task = getUserByNameWithRequestBuilder(username: username).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
case let .failure(error):
promise(.failure(error))
}
@ -299,7 +299,7 @@ open class UserAPI {
task = loginUserWithRequestBuilder(username: username, password: password).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
promise(.success(response.body!))
promise(.success(response.body))
case let .failure(error):
promise(.failure(error))
}

View File

@ -33,15 +33,15 @@ public enum DecodableRequestBuilderError: Error {
open class Response<T> {
public let statusCode: Int
public let header: [String: String]
public let body: T?
public let body: T
public init(statusCode: Int, header: [String: String], body: T?) {
public init(statusCode: Int, header: [String: String], body: T) {
self.statusCode = statusCode
self.header = header
self.body = body
}
public convenience init(response: HTTPURLResponse, body: T?) {
public convenience init(response: HTTPURLResponse, body: T) {
let rawHeader = response.allHeaderFields
var header = [String: String]()
for (key, value) in rawHeader {

View File

@ -200,60 +200,12 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
switch T.self {
case is String.Type:
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
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)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: () as! T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
}
}
@ -345,7 +297,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)))
case is URL.Type:
do {
@ -376,7 +328,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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
@ -386,11 +338,11 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))
completion(.success(Response(response: httpResponse, body: () as! T)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: data as! T)))
default:

View File

@ -33,15 +33,15 @@ public enum DecodableRequestBuilderError: Error {
open class Response<T> {
public let statusCode: Int
public let header: [String: String]
public let body: T?
public let body: T
public init(statusCode: Int, header: [String: String], body: T?) {
public init(statusCode: Int, header: [String: String], body: T) {
self.statusCode = statusCode
self.header = header
self.body = body
}
public convenience init(response: HTTPURLResponse, body: T?) {
public convenience init(response: HTTPURLResponse, body: T) {
let rawHeader = response.allHeaderFields
var header = [String: String]()
for (key, value) in rawHeader {

View File

@ -200,60 +200,12 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
switch T.self {
case is String.Type:
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
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)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: () as! T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
}
}
@ -345,7 +297,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)))
case is URL.Type:
do {
@ -376,7 +328,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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
@ -386,11 +338,11 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))
completion(.success(Response(response: httpResponse, body: () as! T)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: data as! T)))
default:

View File

@ -33,15 +33,15 @@ public enum DecodableRequestBuilderError: Error {
open class Response<T> {
public let statusCode: Int
public let header: [String: String]
public let body: T?
public let body: T
public init(statusCode: Int, header: [String: String], body: T?) {
public init(statusCode: Int, header: [String: String], body: T) {
self.statusCode = statusCode
self.header = header
self.body = body
}
public convenience init(response: HTTPURLResponse, body: T?) {
public convenience init(response: HTTPURLResponse, body: T) {
let rawHeader = response.allHeaderFields
var header = [String: String]()
for (key, value) in rawHeader {

View File

@ -200,60 +200,12 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
switch T.self {
case is String.Type:
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
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)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: () as! T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
}
}
@ -345,7 +297,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)))
case is URL.Type:
do {
@ -376,7 +328,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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
@ -386,11 +338,11 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))
completion(.success(Response(response: httpResponse, body: () as! T)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: data as! T)))
default:

View File

@ -33,15 +33,15 @@ internal enum DecodableRequestBuilderError: Error {
internal class Response<T> {
internal let statusCode: Int
internal let header: [String: String]
internal let body: T?
internal let body: T
internal init(statusCode: Int, header: [String: String], body: T?) {
internal init(statusCode: Int, header: [String: String], body: T) {
self.statusCode = statusCode
self.header = header
self.body = body
}
internal convenience init(response: HTTPURLResponse, body: T?) {
internal convenience init(response: HTTPURLResponse, body: T) {
let rawHeader = response.allHeaderFields
var header = [String: String]()
for (key, value) in rawHeader {

View File

@ -200,60 +200,12 @@ internal class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
switch T.self {
case is String.Type:
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
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)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: () as! T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
}
}
@ -345,7 +297,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)))
case is URL.Type:
do {
@ -376,7 +328,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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
@ -386,11 +338,11 @@ internal class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionReques
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))
completion(.success(Response(response: httpResponse, body: () as! T)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: data as! T)))
default:

View File

@ -33,15 +33,15 @@ public enum DecodableRequestBuilderError: Error {
open class Response<T> {
public let statusCode: Int
public let header: [String: String]
public let body: T?
public let body: T
public init(statusCode: Int, header: [String: String], body: T?) {
public init(statusCode: Int, header: [String: String], body: T) {
self.statusCode = statusCode
self.header = header
self.body = body
}
public convenience init(response: HTTPURLResponse, body: T?) {
public convenience init(response: HTTPURLResponse, body: T) {
let rawHeader = response.allHeaderFields
var header = [String: String]()
for (key, value) in rawHeader {

View File

@ -200,60 +200,12 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
switch T.self {
case is String.Type:
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
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)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: () as! T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
}
}
@ -345,7 +297,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)))
case is URL.Type:
do {
@ -376,7 +328,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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
@ -386,11 +338,11 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))
completion(.success(Response(response: httpResponse, body: () as! T)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: data as! T)))
default:

View File

@ -33,15 +33,15 @@ public enum DecodableRequestBuilderError: Error {
open class Response<T> {
public let statusCode: Int
public let header: [String: String]
public let body: T?
public let body: T
public init(statusCode: Int, header: [String: String], body: T?) {
public init(statusCode: Int, header: [String: String], body: T) {
self.statusCode = statusCode
self.header = header
self.body = body
}
public convenience init(response: HTTPURLResponse, body: T?) {
public convenience init(response: HTTPURLResponse, body: T) {
let rawHeader = response.allHeaderFields
var header = [String: String]()
for (key, value) in rawHeader {

View File

@ -200,60 +200,12 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
switch T.self {
case is String.Type:
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
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)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: () as! T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
}
}
@ -345,7 +297,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)))
case is URL.Type:
do {
@ -376,7 +328,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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
@ -386,11 +338,11 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))
completion(.success(Response(response: httpResponse, body: () as! T)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: data as! T)))
default:

View File

@ -25,7 +25,7 @@ open class AnotherFakeAPI {
call123testSpecialTagsWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
case let .failure(error):
deferred.resolver.reject(error)
}

View File

@ -24,7 +24,7 @@ open class FakeAPI {
fakeOuterBooleanSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
case let .failure(error):
deferred.resolver.reject(error)
}
@ -67,7 +67,7 @@ open class FakeAPI {
fakeOuterCompositeSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
case let .failure(error):
deferred.resolver.reject(error)
}
@ -110,7 +110,7 @@ open class FakeAPI {
fakeOuterNumberSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
case let .failure(error):
deferred.resolver.reject(error)
}
@ -153,7 +153,7 @@ open class FakeAPI {
fakeOuterStringSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
case let .failure(error):
deferred.resolver.reject(error)
}
@ -287,7 +287,7 @@ open class FakeAPI {
testClientModelWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
case let .failure(error):
deferred.resolver.reject(error)
}

View File

@ -25,7 +25,7 @@ open class FakeClassnameTags123API {
testClassnameWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
case let .failure(error):
deferred.resolver.reject(error)
}

View File

@ -133,7 +133,7 @@ open class PetAPI {
findPetsByStatusWithRequestBuilder(status: status).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
case let .failure(error):
deferred.resolver.reject(error)
}
@ -185,7 +185,7 @@ open class PetAPI {
findPetsByTagsWithRequestBuilder(tags: tags).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
case let .failure(error):
deferred.resolver.reject(error)
}
@ -237,7 +237,7 @@ open class PetAPI {
getPetByIdWithRequestBuilder(petId: petId).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
case let .failure(error):
deferred.resolver.reject(error)
}
@ -397,7 +397,7 @@ open class PetAPI {
uploadFileWithRequestBuilder(petId: petId, additionalMetadata: additionalMetadata, file: file).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
case let .failure(error):
deferred.resolver.reject(error)
}
@ -457,7 +457,7 @@ open class PetAPI {
uploadFileWithRequiredFileWithRequestBuilder(petId: petId, requiredFile: requiredFile, additionalMetadata: additionalMetadata).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
case let .failure(error):
deferred.resolver.reject(error)
}

View File

@ -72,7 +72,7 @@ open class StoreAPI {
getInventoryWithRequestBuilder().execute(apiResponseQueue) { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
case let .failure(error):
deferred.resolver.reject(error)
}
@ -119,7 +119,7 @@ open class StoreAPI {
getOrderByIdWithRequestBuilder(orderId: orderId).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
case let .failure(error):
deferred.resolver.reject(error)
}
@ -167,7 +167,7 @@ open class StoreAPI {
placeOrderWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
case let .failure(error):
deferred.resolver.reject(error)
}

View File

@ -206,7 +206,7 @@ open class UserAPI {
getUserByNameWithRequestBuilder(username: username).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
case let .failure(error):
deferred.resolver.reject(error)
}
@ -254,7 +254,7 @@ open class UserAPI {
loginUserWithRequestBuilder(username: username, password: password).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
deferred.resolver.fulfill(response.body!)
deferred.resolver.fulfill(response.body)
case let .failure(error):
deferred.resolver.reject(error)
}

View File

@ -33,15 +33,15 @@ public enum DecodableRequestBuilderError: Error {
open class Response<T> {
public let statusCode: Int
public let header: [String: String]
public let body: T?
public let body: T
public init(statusCode: Int, header: [String: String], body: T?) {
public init(statusCode: Int, header: [String: String], body: T) {
self.statusCode = statusCode
self.header = header
self.body = body
}
public convenience init(response: HTTPURLResponse, body: T?) {
public convenience init(response: HTTPURLResponse, body: T) {
let rawHeader = response.allHeaderFields
var header = [String: String]()
for (key, value) in rawHeader {

View File

@ -200,60 +200,12 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
switch T.self {
case is String.Type:
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
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)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: () as! T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
}
}
@ -345,7 +297,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)))
case is URL.Type:
do {
@ -376,7 +328,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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
@ -386,11 +338,11 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))
completion(.success(Response(response: httpResponse, body: () as! T)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: data as! T)))
default:

View File

@ -33,15 +33,15 @@ public enum DecodableRequestBuilderError: Error {
open class Response<T> {
public let statusCode: Int
public let header: [String: String]
public let body: T?
public let body: T
public init(statusCode: Int, header: [String: String], body: T?) {
public init(statusCode: Int, header: [String: String], body: T) {
self.statusCode = statusCode
self.header = header
self.body = body
}
public convenience init(response: HTTPURLResponse, body: T?) {
public convenience init(response: HTTPURLResponse, body: T) {
let rawHeader = response.allHeaderFields
var header = [String: String]()
for (key, value) in rawHeader {

View File

@ -200,60 +200,12 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
switch T.self {
case is String.Type:
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
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)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: () as! T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
}
}
@ -345,7 +297,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)))
case is URL.Type:
do {
@ -376,7 +328,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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
@ -386,11 +338,11 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))
completion(.success(Response(response: httpResponse, body: () as! T)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: data as! T)))
default:

View File

@ -24,7 +24,7 @@ open class AnotherFakeAPI {
return call123testSpecialTagsWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
case let .failure(error):
completion(.failure(error))
}

View File

@ -23,7 +23,7 @@ open class FakeAPI {
return fakeOuterBooleanSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
case let .failure(error):
completion(.failure(error))
}
@ -65,7 +65,7 @@ open class FakeAPI {
return fakeOuterCompositeSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
case let .failure(error):
completion(.failure(error))
}
@ -107,7 +107,7 @@ open class FakeAPI {
return fakeOuterNumberSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
case let .failure(error):
completion(.failure(error))
}
@ -149,7 +149,7 @@ open class FakeAPI {
return fakeOuterStringSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
case let .failure(error):
completion(.failure(error))
}
@ -280,7 +280,7 @@ open class FakeAPI {
return testClientModelWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
case let .failure(error):
completion(.failure(error))
}

View File

@ -24,7 +24,7 @@ open class FakeClassnameTags123API {
return testClassnameWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
case let .failure(error):
completion(.failure(error))
}

View File

@ -130,7 +130,7 @@ open class PetAPI {
return findPetsByStatusWithRequestBuilder(status: status).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
case let .failure(error):
completion(.failure(error))
}
@ -181,7 +181,7 @@ open class PetAPI {
return findPetsByTagsWithRequestBuilder(tags: tags).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
case let .failure(error):
completion(.failure(error))
}
@ -232,7 +232,7 @@ open class PetAPI {
return getPetByIdWithRequestBuilder(petId: petId).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
case let .failure(error):
completion(.failure(error))
}
@ -389,7 +389,7 @@ open class PetAPI {
return uploadFileWithRequestBuilder(petId: petId, additionalMetadata: additionalMetadata, file: file).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
case let .failure(error):
completion(.failure(error))
}
@ -448,7 +448,7 @@ open class PetAPI {
return uploadFileWithRequiredFileWithRequestBuilder(petId: petId, requiredFile: requiredFile, additionalMetadata: additionalMetadata).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
case let .failure(error):
completion(.failure(error))
}

View File

@ -70,7 +70,7 @@ open class StoreAPI {
return getInventoryWithRequestBuilder().execute(apiResponseQueue) { result in
switch result {
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
case let .failure(error):
completion(.failure(error))
}
@ -116,7 +116,7 @@ open class StoreAPI {
return getOrderByIdWithRequestBuilder(orderId: orderId).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
case let .failure(error):
completion(.failure(error))
}
@ -163,7 +163,7 @@ open class StoreAPI {
return placeOrderWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
case let .failure(error):
completion(.failure(error))
}

View File

@ -201,7 +201,7 @@ open class UserAPI {
return getUserByNameWithRequestBuilder(username: username).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
case let .failure(error):
completion(.failure(error))
}
@ -248,7 +248,7 @@ open class UserAPI {
return loginUserWithRequestBuilder(username: username, password: password).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
completion(.success(response.body!))
completion(.success(response.body))
case let .failure(error):
completion(.failure(error))
}

View File

@ -33,15 +33,15 @@ public enum DecodableRequestBuilderError: Error {
open class Response<T> {
public let statusCode: Int
public let header: [String: String]
public let body: T?
public let body: T
public init(statusCode: Int, header: [String: String], body: T?) {
public init(statusCode: Int, header: [String: String], body: T) {
self.statusCode = statusCode
self.header = header
self.body = body
}
public convenience init(response: HTTPURLResponse, body: T?) {
public convenience init(response: HTTPURLResponse, body: T) {
let rawHeader = response.allHeaderFields
var header = [String: String]()
for (key, value) in rawHeader {

View File

@ -200,60 +200,12 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
switch T.self {
case is String.Type:
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
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)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: () as! T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
}
}
@ -345,7 +297,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)))
case is URL.Type:
do {
@ -376,7 +328,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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
@ -386,11 +338,11 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))
completion(.success(Response(response: httpResponse, body: () as! T)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: data as! T)))
default:

View File

@ -25,7 +25,7 @@ open class AnotherFakeAPI {
let task = call123testSpecialTagsWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
case let .failure(error):
observer.onError(error)
}

View File

@ -24,7 +24,7 @@ open class FakeAPI {
let task = fakeOuterBooleanSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
case let .failure(error):
observer.onError(error)
}
@ -72,7 +72,7 @@ open class FakeAPI {
let task = fakeOuterCompositeSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
case let .failure(error):
observer.onError(error)
}
@ -120,7 +120,7 @@ open class FakeAPI {
let task = fakeOuterNumberSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
case let .failure(error):
observer.onError(error)
}
@ -168,7 +168,7 @@ open class FakeAPI {
let task = fakeOuterStringSerializeWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
case let .failure(error):
observer.onError(error)
}
@ -317,7 +317,7 @@ open class FakeAPI {
let task = testClientModelWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
case let .failure(error):
observer.onError(error)
}

View File

@ -25,7 +25,7 @@ open class FakeClassnameTags123API {
let task = testClassnameWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
case let .failure(error):
observer.onError(error)
}

View File

@ -143,7 +143,7 @@ open class PetAPI {
let task = findPetsByStatusWithRequestBuilder(status: status).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
case let .failure(error):
observer.onError(error)
}
@ -200,7 +200,7 @@ open class PetAPI {
let task = findPetsByTagsWithRequestBuilder(tags: tags).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
case let .failure(error):
observer.onError(error)
}
@ -257,7 +257,7 @@ open class PetAPI {
let task = getPetByIdWithRequestBuilder(petId: petId).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
case let .failure(error):
observer.onError(error)
}
@ -432,7 +432,7 @@ open class PetAPI {
let task = uploadFileWithRequestBuilder(petId: petId, additionalMetadata: additionalMetadata, file: file).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
case let .failure(error):
observer.onError(error)
}
@ -497,7 +497,7 @@ open class PetAPI {
let task = uploadFileWithRequiredFileWithRequestBuilder(petId: petId, requiredFile: requiredFile, additionalMetadata: additionalMetadata).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
case let .failure(error):
observer.onError(error)
}

View File

@ -77,7 +77,7 @@ open class StoreAPI {
let task = getInventoryWithRequestBuilder().execute(apiResponseQueue) { result in
switch result {
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
case let .failure(error):
observer.onError(error)
}
@ -129,7 +129,7 @@ open class StoreAPI {
let task = getOrderByIdWithRequestBuilder(orderId: orderId).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
case let .failure(error):
observer.onError(error)
}
@ -182,7 +182,7 @@ open class StoreAPI {
let task = placeOrderWithRequestBuilder(body: body).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
case let .failure(error):
observer.onError(error)
}

View File

@ -226,7 +226,7 @@ open class UserAPI {
let task = getUserByNameWithRequestBuilder(username: username).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
case let .failure(error):
observer.onError(error)
}
@ -279,7 +279,7 @@ open class UserAPI {
let task = loginUserWithRequestBuilder(username: username, password: password).execute(apiResponseQueue) { result in
switch result {
case let .success(response):
observer.onNext(response.body!)
observer.onNext(response.body)
case let .failure(error):
observer.onError(error)
}

View File

@ -33,15 +33,15 @@ public enum DecodableRequestBuilderError: Error {
open class Response<T> {
public let statusCode: Int
public let header: [String: String]
public let body: T?
public let body: T
public init(statusCode: Int, header: [String: String], body: T?) {
public init(statusCode: Int, header: [String: String], body: T) {
self.statusCode = statusCode
self.header = header
self.body = body
}
public convenience init(response: HTTPURLResponse, body: T?) {
public convenience init(response: HTTPURLResponse, body: T) {
let rawHeader = response.allHeaderFields
var header = [String: String]()
for (key, value) in rawHeader {

View File

@ -200,60 +200,12 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
switch T.self {
case is String.Type:
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
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)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: () as! T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
}
}
@ -345,7 +297,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)))
case is URL.Type:
do {
@ -376,7 +328,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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
@ -386,11 +338,11 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))
completion(.success(Response(response: httpResponse, body: () as! T)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: data as! T)))
default:

View File

@ -33,15 +33,15 @@ public enum DecodableRequestBuilderError: Error {
open class Response<T> {
public let statusCode: Int
public let header: [String: String]
public let body: T?
public let body: T
public init(statusCode: Int, header: [String: String], body: T?) {
public init(statusCode: Int, header: [String: String], body: T) {
self.statusCode = statusCode
self.header = header
self.body = body
}
public convenience init(response: HTTPURLResponse, body: T?) {
public convenience init(response: HTTPURLResponse, body: T) {
let rawHeader = response.allHeaderFields
var header = [String: String]()
for (key, value) in rawHeader {

View File

@ -200,60 +200,12 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
switch T.self {
case is String.Type:
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
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)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: () as! T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
}
}
@ -345,7 +297,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)))
case is URL.Type:
do {
@ -376,7 +328,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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
@ -386,11 +338,11 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))
completion(.success(Response(response: httpResponse, body: () as! T)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: data as! T)))
default:

View File

@ -33,15 +33,15 @@ public enum DecodableRequestBuilderError: Error {
open class Response<T> {
public let statusCode: Int
public let header: [String: String]
public let body: T?
public let body: T
public init(statusCode: Int, header: [String: String], body: T?) {
public init(statusCode: Int, header: [String: String], body: T) {
self.statusCode = statusCode
self.header = header
self.body = body
}
public convenience init(response: HTTPURLResponse, body: T?) {
public convenience init(response: HTTPURLResponse, body: T) {
let rawHeader = response.allHeaderFields
var header = [String: String]()
for (key, value) in rawHeader {

View File

@ -200,60 +200,12 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
}
switch T.self {
case is String.Type:
let body = data.flatMap { String(data: $0, encoding: .utf8) } ?? ""
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)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: () as! T)))
default:
completion(.success(Response(response: httpResponse, body: data as? T)))
fatalError("Unsupported Response Body Type - \(String(describing: T.self))")
}
}
@ -345,7 +297,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)))
case is URL.Type:
do {
@ -376,7 +328,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)))
} catch let requestParserError as DownloadException {
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
@ -386,11 +338,11 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
case is Void.Type:
completion(.success(Response(response: httpResponse, body: nil)))
completion(.success(Response(response: httpResponse, body: () as! T)))
case is Data.Type:
completion(.success(Response(response: httpResponse, body: data as? T)))
completion(.success(Response(response: httpResponse, body: data as! T)))
default: