forked from loafle/openapi-generator-original
[Swift 5] code formatting (#8385)
* [swift] improve code formatting
* [swift] update sample projects
* Revert "[swift] improve code formatting"
This reverts commit 0cc280509c.
* [swift] update sample projects
This commit is contained in:
@@ -8,7 +8,7 @@ import Foundation
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct APIHelper {
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
|
||||
let destination = source.reduce(into: [String: Any]()) { (result, item) in
|
||||
let destination = source.reduce(into: [String: Any]()) { result, item in
|
||||
if let value = item.value {
|
||||
result[item.key] = value
|
||||
}
|
||||
@@ -21,7 +21,7 @@ import Foundation
|
||||
}
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
|
||||
return source.reduce(into: [String: String]()) { (result, item) in
|
||||
return source.reduce(into: [String: String]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
result[item.key] = collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
} else if let value: Any = item.value {
|
||||
@@ -35,27 +35,27 @@ import Foundation
|
||||
return nil
|
||||
}
|
||||
|
||||
return source.reduce(into: [String: Any](), { (result, item) in
|
||||
return source.reduce(into: [String: Any]()) { result, item in
|
||||
switch item.value {
|
||||
case let x as Bool:
|
||||
result[item.key] = x.description
|
||||
default:
|
||||
result[item.key] = item.value
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func mapValueToPathItem(_ source: Any) -> Any {
|
||||
if let collection = source as? [Any?] {
|
||||
return collection.filter { $0 != nil }.map {"\($0!)"}.joined(separator: ",")
|
||||
return collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
|
||||
let destination = source.filter({ $0.value != nil}).reduce(into: [URLQueryItem]()) { (result, item) in
|
||||
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
collection.filter { $0 != nil }.map {"\($0!)"}.forEach { value in
|
||||
collection.filter { $0 != nil }.map { "\($0!)" }.forEach { value in
|
||||
result.append(URLQueryItem(name: item.key, value: value))
|
||||
}
|
||||
} else if let value = item.value {
|
||||
|
||||
@@ -52,7 +52,7 @@ import Foundation
|
||||
}
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func addCredential() -> Self {
|
||||
self.credential = {{projectName}}API.credential
|
||||
credential = {{projectName}}API.credential
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,41 +8,42 @@
|
||||
import Foundation
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class CodableHelper {
|
||||
|
||||
private static var customDateFormatter: DateFormatter?
|
||||
private static var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter()
|
||||
|
||||
private static var customJSONDecoder: JSONDecoder?
|
||||
private static var defaultJSONDecoder: JSONDecoder = {
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
return decoder
|
||||
}()
|
||||
|
||||
private static var customJSONEncoder: JSONEncoder?
|
||||
private static var defaultJSONEncoder: JSONEncoder = {
|
||||
let encoder = JSONEncoder()
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
return encoder
|
||||
}()
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var dateFormatter: DateFormatter {
|
||||
get { return self.customDateFormatter ?? self.defaultDateFormatter }
|
||||
set { self.customDateFormatter = newValue }
|
||||
get { return customDateFormatter ?? defaultDateFormatter }
|
||||
set { customDateFormatter = newValue }
|
||||
}
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var jsonDecoder: JSONDecoder {
|
||||
get { return self.customJSONDecoder ?? self.defaultJSONDecoder }
|
||||
set { self.customJSONDecoder = newValue }
|
||||
get { return customJSONDecoder ?? defaultJSONDecoder }
|
||||
set { customJSONDecoder = newValue }
|
||||
}
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var jsonEncoder: JSONEncoder {
|
||||
get { return self.customJSONEncoder ?? self.defaultJSONEncoder }
|
||||
set { self.customJSONEncoder = newValue }
|
||||
get { return customJSONEncoder ?? defaultJSONEncoder }
|
||||
set { customJSONEncoder = newValue }
|
||||
}
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable {
|
||||
return Swift.Result { try self.jsonDecoder.decode(type, from: data) }
|
||||
return Swift.Result { try jsonDecoder.decode(type, from: data) }
|
||||
}
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable {
|
||||
return Swift.Result { try self.jsonEncoder.encode(value) }
|
||||
return Swift.Result { try jsonEncoder.encode(value) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,8 @@
|
||||
import Foundation
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class Configuration {
|
||||
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
|
||||
|
||||
}
|
||||
|
||||
@@ -8,13 +8,14 @@ let package = Package(
|
||||
.iOS(.v9),
|
||||
.macOS(.v10_11),
|
||||
.tvOS(.v9),
|
||||
.watchOS(.v3)
|
||||
.watchOS(.v3),
|
||||
],
|
||||
products: [
|
||||
// Products define the executables and libraries produced by a package, and make them visible to other packages.
|
||||
.library(
|
||||
name: "{{projectName}}",
|
||||
targets: ["{{projectName}}"]),
|
||||
targets: ["{{projectName}}"]
|
||||
),
|
||||
],
|
||||
dependencies: [
|
||||
// Dependencies declare other packages that this package depends on.
|
||||
|
||||
@@ -117,14 +117,14 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
fatalError("Unprocessable value \(v) with key \(k)")
|
||||
}
|
||||
}
|
||||
}, to: URLString, method: xMethod, headers: nil, encodingCompletion: { encodingResult in
|
||||
}, to: URLString, method: xMethod, headers: nil, encodingCompletion: { encodingResult in
|
||||
switch encodingResult {
|
||||
case .success(let upload, _, _):
|
||||
case let .success(upload, _, _):
|
||||
if let onProgressReady = self.onProgressReady {
|
||||
onProgressReady(upload.uploadProgress)
|
||||
}
|
||||
self.processRequest(request: upload, managerId, apiResponseQueue, completion)
|
||||
case .failure(let encodingError):
|
||||
case let .failure(encodingError):
|
||||
apiResponseQueue.async {
|
||||
completion(.failure(ErrorResponse.error(415, nil, nil, encodingError)))
|
||||
}
|
||||
@@ -159,7 +159,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
|
||||
switch T.self {
|
||||
case is String.Type:
|
||||
validatedRequest.responseString(queue: apiResponseQueue, completionHandler: { (stringResponse) in
|
||||
validatedRequest.responseString(queue: apiResponseQueue, completionHandler: { stringResponse in
|
||||
cleanupRequest()
|
||||
|
||||
switch stringResponse.result {
|
||||
@@ -171,7 +171,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
|
||||
})
|
||||
case is URL.Type:
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { (dataResponse) in
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { dataResponse in
|
||||
cleanupRequest()
|
||||
|
||||
do {
|
||||
@@ -209,13 +209,13 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, dataResponse.data, dataResponse.response, requestParserError)))
|
||||
} catch let error {
|
||||
} catch {
|
||||
completion(.failure(ErrorResponse.error(400, dataResponse.data, dataResponse.response, error)))
|
||||
}
|
||||
return
|
||||
})
|
||||
case is Void.Type:
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { (voidResponse) in
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { voidResponse in
|
||||
cleanupRequest()
|
||||
|
||||
switch voidResponse.result {
|
||||
@@ -227,7 +227,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
|
||||
})
|
||||
default:
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { (dataResponse) in
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { dataResponse in
|
||||
cleanupRequest()
|
||||
|
||||
switch dataResponse.result {
|
||||
@@ -243,7 +243,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func buildHeaders() -> [String: String] {
|
||||
var httpHeaders = SessionManager.defaultHTTPHeaders
|
||||
for (key, value) in self.headers {
|
||||
for (key, value) in headers {
|
||||
httpHeaders[key] = value
|
||||
}
|
||||
return httpHeaders
|
||||
@@ -317,7 +317,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
|
||||
switch T.self {
|
||||
case is String.Type:
|
||||
validatedRequest.responseString(queue: apiResponseQueue, completionHandler: { (stringResponse) in
|
||||
validatedRequest.responseString(queue: apiResponseQueue, completionHandler: { stringResponse in
|
||||
cleanupRequest()
|
||||
|
||||
switch stringResponse.result {
|
||||
@@ -329,7 +329,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
|
||||
})
|
||||
case is Void.Type:
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { (voidResponse) in
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { voidResponse in
|
||||
cleanupRequest()
|
||||
|
||||
switch voidResponse.result {
|
||||
@@ -341,7 +341,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
|
||||
})
|
||||
case is Data.Type:
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { (dataResponse) in
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { dataResponse in
|
||||
cleanupRequest()
|
||||
|
||||
switch dataResponse.result {
|
||||
@@ -402,6 +402,6 @@ extension JSONDataEncoding: ParameterEncoding {
|
||||
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
|
||||
let urlRequest = try urlRequest.asURLRequest()
|
||||
|
||||
return self.encode(urlRequest, with: parameters)
|
||||
return encode(urlRequest, with: parameters)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,11 +209,11 @@ private var urlSessionStore = SynchronizedDictionary<String, URLSession>()
|
||||
|
||||
let fileManager = FileManager.default
|
||||
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
|
||||
let requestURL = try self.getURL(from: urlRequest)
|
||||
let requestURL = try getURL(from: urlRequest)
|
||||
|
||||
var requestPath = try self.getPath(from: requestURL)
|
||||
var requestPath = try getPath(from: requestURL)
|
||||
|
||||
if let headerFileName = self.getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
requestPath = requestPath.appending("/\(headerFileName)")
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@ private var urlSessionStore = SynchronizedDictionary<String, URLSession>()
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
} catch let error {
|
||||
} catch {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, error)))
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ private var urlSessionStore = SynchronizedDictionary<String, URLSession>()
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func buildHeaders() -> [String: String] {
|
||||
var httpHeaders: [String: String] = [:]
|
||||
for (key, value) in self.headers {
|
||||
for (key, value) in headers {
|
||||
httpHeaders[key] = value
|
||||
}
|
||||
for (key, value) in {{projectName}}API.customHeaders {
|
||||
@@ -390,13 +390,13 @@ private class SessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} enum HTTPMethod: String {
|
||||
case options = "OPTIONS"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case connect = "CONNECT"
|
||||
}
|
||||
|
||||
@@ -499,7 +499,7 @@ private class FormDataEncoding: ParameterEncoding {
|
||||
|
||||
let fileData = try Data(contentsOf: fileURL)
|
||||
|
||||
let mimetype = self.contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
let mimetype = contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
|
||||
let fileName = fileURL.lastPathComponent
|
||||
|
||||
|
||||
@@ -8,25 +8,26 @@ let package = Package(
|
||||
.iOS(.v9),
|
||||
.macOS(.v10_11),
|
||||
.tvOS(.v9),
|
||||
.watchOS(.v3)
|
||||
.watchOS(.v3),
|
||||
],
|
||||
products: [
|
||||
// Products define the executables and libraries produced by a package, and make them visible to other packages.
|
||||
.library(
|
||||
name: "PetstoreClient",
|
||||
targets: ["PetstoreClient"])
|
||||
targets: ["PetstoreClient"]
|
||||
),
|
||||
],
|
||||
dependencies: [
|
||||
// Dependencies declare other packages that this package depends on.
|
||||
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "4.9.1")
|
||||
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "4.9.1"),
|
||||
],
|
||||
targets: [
|
||||
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
|
||||
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
|
||||
.target(
|
||||
name: "PetstoreClient",
|
||||
dependencies: ["Alamofire" ],
|
||||
dependencies: ["Alamofire", ],
|
||||
path: "PetstoreClient/Classes"
|
||||
)
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -408,7 +408,7 @@
|
||||
);
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
@@ -495,7 +495,7 @@
|
||||
);
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
|
||||
@@ -8,7 +8,7 @@ import Foundation
|
||||
|
||||
public struct APIHelper {
|
||||
public static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
|
||||
let destination = source.reduce(into: [String: Any]()) { (result, item) in
|
||||
let destination = source.reduce(into: [String: Any]()) { result, item in
|
||||
if let value = item.value {
|
||||
result[item.key] = value
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public struct APIHelper {
|
||||
}
|
||||
|
||||
public static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
|
||||
return source.reduce(into: [String: String]()) { (result, item) in
|
||||
return source.reduce(into: [String: String]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
result[item.key] = collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
} else if let value: Any = item.value {
|
||||
@@ -35,27 +35,27 @@ public struct APIHelper {
|
||||
return nil
|
||||
}
|
||||
|
||||
return source.reduce(into: [String: Any](), { (result, item) in
|
||||
return source.reduce(into: [String: Any]()) { result, item in
|
||||
switch item.value {
|
||||
case let x as Bool:
|
||||
result[item.key] = x.description
|
||||
default:
|
||||
result[item.key] = item.value
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public static func mapValueToPathItem(_ source: Any) -> Any {
|
||||
if let collection = source as? [Any?] {
|
||||
return collection.filter { $0 != nil }.map {"\($0!)"}.joined(separator: ",")
|
||||
return collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
public static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
|
||||
let destination = source.filter({ $0.value != nil}).reduce(into: [URLQueryItem]()) { (result, item) in
|
||||
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
collection.filter { $0 != nil }.map {"\($0!)"}.forEach { value in
|
||||
collection.filter { $0 != nil }.map { "\($0!)" }.forEach { value in
|
||||
result.append(URLQueryItem(name: item.key, value: value))
|
||||
}
|
||||
} else if let value = item.value {
|
||||
|
||||
@@ -49,7 +49,7 @@ open class RequestBuilder<T> {
|
||||
}
|
||||
|
||||
open func addCredential() -> Self {
|
||||
self.credential = PetstoreClientAPI.credential
|
||||
credential = PetstoreClientAPI.credential
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,11 @@ import Alamofire
|
||||
|
||||
class AlamofireRequestBuilderFactory: RequestBuilderFactory {
|
||||
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
|
||||
return AlamofireRequestBuilder<T>.self
|
||||
AlamofireRequestBuilder<T>.self
|
||||
}
|
||||
|
||||
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type {
|
||||
return AlamofireDecodableRequestBuilder<T>.self
|
||||
AlamofireDecodableRequestBuilder<T>.self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
the file extension). Return the desired Content-Type otherwise.
|
||||
*/
|
||||
open func contentTypeForFormPart(fileURL: URL) -> String? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,7 +73,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
configuration (e.g. to override the cache policy).
|
||||
*/
|
||||
open func makeRequest(manager: SessionManager, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) -> DataRequest {
|
||||
return manager.request(URLString, method: method, parameters: parameters, encoding: encoding, headers: headers)
|
||||
manager.request(URLString, method: method, parameters: parameters, encoding: encoding, headers: headers)
|
||||
}
|
||||
|
||||
override open func execute(_ apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue, _ completion: @escaping (_ result: Swift.Result<Response<T>, Error>) -> Void) {
|
||||
@@ -117,14 +117,14 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
fatalError("Unprocessable value \(v) with key \(k)")
|
||||
}
|
||||
}
|
||||
}, to: URLString, method: xMethod, headers: nil, encodingCompletion: { encodingResult in
|
||||
}, to: URLString, method: xMethod, headers: nil, encodingCompletion: { encodingResult in
|
||||
switch encodingResult {
|
||||
case .success(let upload, _, _):
|
||||
case let .success(upload, _, _):
|
||||
if let onProgressReady = self.onProgressReady {
|
||||
onProgressReady(upload.uploadProgress)
|
||||
}
|
||||
self.processRequest(request: upload, managerId, apiResponseQueue, completion)
|
||||
case .failure(let encodingError):
|
||||
case let .failure(encodingError):
|
||||
apiResponseQueue.async {
|
||||
completion(.failure(ErrorResponse.error(415, nil, nil, encodingError)))
|
||||
}
|
||||
@@ -159,7 +159,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
switch T.self {
|
||||
case is String.Type:
|
||||
validatedRequest.responseString(queue: apiResponseQueue, completionHandler: { (stringResponse) in
|
||||
validatedRequest.responseString(queue: apiResponseQueue, completionHandler: { stringResponse in
|
||||
cleanupRequest()
|
||||
|
||||
switch stringResponse.result {
|
||||
@@ -171,7 +171,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
})
|
||||
case is URL.Type:
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { (dataResponse) in
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { dataResponse in
|
||||
cleanupRequest()
|
||||
|
||||
do {
|
||||
@@ -209,13 +209,13 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, dataResponse.data, dataResponse.response, requestParserError)))
|
||||
} catch let error {
|
||||
} catch {
|
||||
completion(.failure(ErrorResponse.error(400, dataResponse.data, dataResponse.response, error)))
|
||||
}
|
||||
return
|
||||
})
|
||||
case is Void.Type:
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { (voidResponse) in
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { voidResponse in
|
||||
cleanupRequest()
|
||||
|
||||
switch voidResponse.result {
|
||||
@@ -227,7 +227,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
})
|
||||
default:
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { (dataResponse) in
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { dataResponse in
|
||||
cleanupRequest()
|
||||
|
||||
switch dataResponse.result {
|
||||
@@ -243,7 +243,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
open func buildHeaders() -> [String: String] {
|
||||
var httpHeaders = SessionManager.defaultHTTPHeaders
|
||||
for (key, value) in self.headers {
|
||||
for (key, value) in headers {
|
||||
httpHeaders[key] = value
|
||||
}
|
||||
return httpHeaders
|
||||
@@ -317,7 +317,7 @@ open class AlamofireDecodableRequestBuilder<T: Decodable>: AlamofireRequestBuild
|
||||
|
||||
switch T.self {
|
||||
case is String.Type:
|
||||
validatedRequest.responseString(queue: apiResponseQueue, completionHandler: { (stringResponse) in
|
||||
validatedRequest.responseString(queue: apiResponseQueue, completionHandler: { stringResponse in
|
||||
cleanupRequest()
|
||||
|
||||
switch stringResponse.result {
|
||||
@@ -329,7 +329,7 @@ open class AlamofireDecodableRequestBuilder<T: Decodable>: AlamofireRequestBuild
|
||||
|
||||
})
|
||||
case is Void.Type:
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { (voidResponse) in
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { voidResponse in
|
||||
cleanupRequest()
|
||||
|
||||
switch voidResponse.result {
|
||||
@@ -341,7 +341,7 @@ open class AlamofireDecodableRequestBuilder<T: Decodable>: AlamofireRequestBuild
|
||||
|
||||
})
|
||||
case is Data.Type:
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { (dataResponse) in
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { dataResponse in
|
||||
cleanupRequest()
|
||||
|
||||
switch dataResponse.result {
|
||||
@@ -402,6 +402,6 @@ extension JSONDataEncoding: ParameterEncoding {
|
||||
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
|
||||
let urlRequest = try urlRequest.asURLRequest()
|
||||
|
||||
return self.encode(urlRequest, with: parameters)
|
||||
return encode(urlRequest, with: parameters)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,41 +8,42 @@
|
||||
import Foundation
|
||||
|
||||
open class CodableHelper {
|
||||
|
||||
private static var customDateFormatter: DateFormatter?
|
||||
private static var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter()
|
||||
|
||||
private static var customJSONDecoder: JSONDecoder?
|
||||
private static var defaultJSONDecoder: JSONDecoder = {
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
return decoder
|
||||
}()
|
||||
|
||||
private static var customJSONEncoder: JSONEncoder?
|
||||
private static var defaultJSONEncoder: JSONEncoder = {
|
||||
let encoder = JSONEncoder()
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
return encoder
|
||||
}()
|
||||
|
||||
public static var dateFormatter: DateFormatter {
|
||||
get { return self.customDateFormatter ?? self.defaultDateFormatter }
|
||||
set { self.customDateFormatter = newValue }
|
||||
get { return customDateFormatter ?? defaultDateFormatter }
|
||||
set { customDateFormatter = newValue }
|
||||
}
|
||||
public static var jsonDecoder: JSONDecoder {
|
||||
get { return self.customJSONDecoder ?? self.defaultJSONDecoder }
|
||||
set { self.customJSONDecoder = newValue }
|
||||
get { return customJSONDecoder ?? defaultJSONDecoder }
|
||||
set { customJSONDecoder = newValue }
|
||||
}
|
||||
public static var jsonEncoder: JSONEncoder {
|
||||
get { return self.customJSONEncoder ?? self.defaultJSONEncoder }
|
||||
set { self.customJSONEncoder = newValue }
|
||||
get { return customJSONEncoder ?? defaultJSONEncoder }
|
||||
set { customJSONEncoder = newValue }
|
||||
}
|
||||
|
||||
open class func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable {
|
||||
return Swift.Result { try self.jsonDecoder.decode(type, from: data) }
|
||||
return Swift.Result { try jsonDecoder.decode(type, from: data) }
|
||||
}
|
||||
|
||||
open class func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable {
|
||||
return Swift.Result { try self.jsonEncoder.encode(value) }
|
||||
return Swift.Result { try jsonEncoder.encode(value) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,8 @@
|
||||
import Foundation
|
||||
|
||||
open class Configuration {
|
||||
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
|
||||
|
||||
}
|
||||
|
||||
@@ -7,35 +7,35 @@
|
||||
import Foundation
|
||||
|
||||
extension Bool: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Float: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int32: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int32) }
|
||||
}
|
||||
|
||||
extension Int64: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int64) }
|
||||
}
|
||||
|
||||
extension Double: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension String: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension RawRepresentable where RawValue: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self.rawValue as Any }
|
||||
func encodeToJSON() -> Any { self.rawValue as Any }
|
||||
}
|
||||
|
||||
private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
@@ -48,7 +48,7 @@ private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
|
||||
extension Array: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.map(encodeIfPossible)
|
||||
self.map(encodeIfPossible)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,32 +64,32 @@ extension Dictionary: JSONEncodable {
|
||||
|
||||
extension Data: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
}
|
||||
}
|
||||
|
||||
extension Date: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return CodableHelper.dateFormatter.string(from: self) as Any
|
||||
CodableHelper.dateFormatter.string(from: self) as Any
|
||||
}
|
||||
}
|
||||
|
||||
extension URL: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
extension UUID: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.uuidString
|
||||
uuidString
|
||||
}
|
||||
}
|
||||
|
||||
extension String: CodingKey {
|
||||
|
||||
public var stringValue: String {
|
||||
return self
|
||||
self
|
||||
}
|
||||
|
||||
public init?(stringValue: String) {
|
||||
@@ -97,11 +97,11 @@ extension String: CodingKey {
|
||||
}
|
||||
|
||||
public var intValue: Int? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
public init?(intValue: Int) {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
}
|
||||
@@ -174,6 +174,6 @@ extension KeyedDecodingContainerProtocol {
|
||||
|
||||
extension HTTPURLResponse {
|
||||
var isStatusCodeSuccessful: Bool {
|
||||
return Array(200 ..< 300).contains(statusCode)
|
||||
Array(200 ..< 300).contains(statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,13 +8,14 @@ let package = Package(
|
||||
.iOS(.v9),
|
||||
.macOS(.v10_11),
|
||||
.tvOS(.v9),
|
||||
.watchOS(.v3)
|
||||
.watchOS(.v3),
|
||||
],
|
||||
products: [
|
||||
// Products define the executables and libraries produced by a package, and make them visible to other packages.
|
||||
.library(
|
||||
name: "PetstoreClient",
|
||||
targets: ["PetstoreClient"])
|
||||
targets: ["PetstoreClient"]
|
||||
),
|
||||
],
|
||||
dependencies: [
|
||||
// Dependencies declare other packages that this package depends on.
|
||||
@@ -26,6 +27,6 @@ let package = Package(
|
||||
name: "PetstoreClient",
|
||||
dependencies: [],
|
||||
path: "PetstoreClient/Classes"
|
||||
)
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -364,7 +364,7 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
@@ -447,7 +447,7 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
|
||||
@@ -8,7 +8,7 @@ import Foundation
|
||||
|
||||
public struct APIHelper {
|
||||
public static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
|
||||
let destination = source.reduce(into: [String: Any]()) { (result, item) in
|
||||
let destination = source.reduce(into: [String: Any]()) { result, item in
|
||||
if let value = item.value {
|
||||
result[item.key] = value
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public struct APIHelper {
|
||||
}
|
||||
|
||||
public static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
|
||||
return source.reduce(into: [String: String]()) { (result, item) in
|
||||
return source.reduce(into: [String: String]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
result[item.key] = collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
} else if let value: Any = item.value {
|
||||
@@ -35,27 +35,27 @@ public struct APIHelper {
|
||||
return nil
|
||||
}
|
||||
|
||||
return source.reduce(into: [String: Any](), { (result, item) in
|
||||
return source.reduce(into: [String: Any]()) { result, item in
|
||||
switch item.value {
|
||||
case let x as Bool:
|
||||
result[item.key] = x.description
|
||||
default:
|
||||
result[item.key] = item.value
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public static func mapValueToPathItem(_ source: Any) -> Any {
|
||||
if let collection = source as? [Any?] {
|
||||
return collection.filter { $0 != nil }.map {"\($0!)"}.joined(separator: ",")
|
||||
return collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
public static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
|
||||
let destination = source.filter({ $0.value != nil}).reduce(into: [URLQueryItem]()) { (result, item) in
|
||||
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
collection.filter { $0 != nil }.map {"\($0!)"}.forEach { value in
|
||||
collection.filter { $0 != nil }.map { "\($0!)" }.forEach { value in
|
||||
result.append(URLQueryItem(name: item.key, value: value))
|
||||
}
|
||||
} else if let value = item.value {
|
||||
|
||||
@@ -51,7 +51,7 @@ open class RequestBuilder<T> {
|
||||
}
|
||||
|
||||
open func addCredential() -> Self {
|
||||
self.credential = PetstoreClientAPI.credential
|
||||
credential = PetstoreClientAPI.credential
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,41 +8,42 @@
|
||||
import Foundation
|
||||
|
||||
open class CodableHelper {
|
||||
|
||||
private static var customDateFormatter: DateFormatter?
|
||||
private static var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter()
|
||||
|
||||
private static var customJSONDecoder: JSONDecoder?
|
||||
private static var defaultJSONDecoder: JSONDecoder = {
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
return decoder
|
||||
}()
|
||||
|
||||
private static var customJSONEncoder: JSONEncoder?
|
||||
private static var defaultJSONEncoder: JSONEncoder = {
|
||||
let encoder = JSONEncoder()
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
return encoder
|
||||
}()
|
||||
|
||||
public static var dateFormatter: DateFormatter {
|
||||
get { return self.customDateFormatter ?? self.defaultDateFormatter }
|
||||
set { self.customDateFormatter = newValue }
|
||||
get { return customDateFormatter ?? defaultDateFormatter }
|
||||
set { customDateFormatter = newValue }
|
||||
}
|
||||
public static var jsonDecoder: JSONDecoder {
|
||||
get { return self.customJSONDecoder ?? self.defaultJSONDecoder }
|
||||
set { self.customJSONDecoder = newValue }
|
||||
get { return customJSONDecoder ?? defaultJSONDecoder }
|
||||
set { customJSONDecoder = newValue }
|
||||
}
|
||||
public static var jsonEncoder: JSONEncoder {
|
||||
get { return self.customJSONEncoder ?? self.defaultJSONEncoder }
|
||||
set { self.customJSONEncoder = newValue }
|
||||
get { return customJSONEncoder ?? defaultJSONEncoder }
|
||||
set { customJSONEncoder = newValue }
|
||||
}
|
||||
|
||||
open class func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable {
|
||||
return Swift.Result { try self.jsonDecoder.decode(type, from: data) }
|
||||
return Swift.Result { try jsonDecoder.decode(type, from: data) }
|
||||
}
|
||||
|
||||
open class func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable {
|
||||
return Swift.Result { try self.jsonEncoder.encode(value) }
|
||||
return Swift.Result { try jsonEncoder.encode(value) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,8 @@
|
||||
import Foundation
|
||||
|
||||
open class Configuration {
|
||||
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
|
||||
|
||||
}
|
||||
|
||||
@@ -7,35 +7,35 @@
|
||||
import Foundation
|
||||
|
||||
extension Bool: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Float: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int32: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int32) }
|
||||
}
|
||||
|
||||
extension Int64: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int64) }
|
||||
}
|
||||
|
||||
extension Double: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension String: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension RawRepresentable where RawValue: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self.rawValue as Any }
|
||||
func encodeToJSON() -> Any { self.rawValue as Any }
|
||||
}
|
||||
|
||||
private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
@@ -48,7 +48,7 @@ private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
|
||||
extension Array: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.map(encodeIfPossible)
|
||||
self.map(encodeIfPossible)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,32 +64,32 @@ extension Dictionary: JSONEncodable {
|
||||
|
||||
extension Data: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
}
|
||||
}
|
||||
|
||||
extension Date: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return CodableHelper.dateFormatter.string(from: self) as Any
|
||||
CodableHelper.dateFormatter.string(from: self) as Any
|
||||
}
|
||||
}
|
||||
|
||||
extension URL: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
extension UUID: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.uuidString
|
||||
uuidString
|
||||
}
|
||||
}
|
||||
|
||||
extension String: CodingKey {
|
||||
|
||||
public var stringValue: String {
|
||||
return self
|
||||
self
|
||||
}
|
||||
|
||||
public init?(stringValue: String) {
|
||||
@@ -97,11 +97,11 @@ extension String: CodingKey {
|
||||
}
|
||||
|
||||
public var intValue: Int? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
public init?(intValue: Int) {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
}
|
||||
@@ -174,6 +174,6 @@ extension KeyedDecodingContainerProtocol {
|
||||
|
||||
extension HTTPURLResponse {
|
||||
var isStatusCodeSuccessful: Bool {
|
||||
return Array(200 ..< 300).contains(statusCode)
|
||||
Array(200 ..< 300).contains(statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,11 @@ import MobileCoreServices
|
||||
|
||||
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
|
||||
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionRequestBuilder<T>.self
|
||||
URLSessionRequestBuilder<T>.self
|
||||
}
|
||||
|
||||
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionDecodableRequestBuilder<T>.self
|
||||
URLSessionDecodableRequestBuilder<T>.self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
the file extension). Return the desired Content-Type otherwise.
|
||||
*/
|
||||
open func contentTypeForFormPart(fileURL: URL) -> String? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -209,11 +209,11 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
let fileManager = FileManager.default
|
||||
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
|
||||
let requestURL = try self.getURL(from: urlRequest)
|
||||
let requestURL = try getURL(from: urlRequest)
|
||||
|
||||
var requestPath = try self.getPath(from: requestURL)
|
||||
var requestPath = try getPath(from: requestURL)
|
||||
|
||||
if let headerFileName = self.getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
requestPath = requestPath.appending("/\(headerFileName)")
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
} catch let error {
|
||||
} catch {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, error)))
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
open func buildHeaders() -> [String: String] {
|
||||
var httpHeaders: [String: String] = [:]
|
||||
for (key, value) in self.headers {
|
||||
for (key, value) in headers {
|
||||
httpHeaders[key] = value
|
||||
}
|
||||
for (key, value) in PetstoreClientAPI.customHeaders {
|
||||
@@ -390,13 +390,13 @@ private class SessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
|
||||
|
||||
public enum HTTPMethod: String {
|
||||
case options = "OPTIONS"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case connect = "CONNECT"
|
||||
}
|
||||
|
||||
@@ -499,7 +499,7 @@ private class FormDataEncoding: ParameterEncoding {
|
||||
|
||||
let fileData = try Data(contentsOf: fileURL)
|
||||
|
||||
let mimetype = self.contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
let mimetype = contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
|
||||
let fileName = fileURL.lastPathComponent
|
||||
|
||||
|
||||
@@ -8,13 +8,14 @@ let package = Package(
|
||||
.iOS(.v9),
|
||||
.macOS(.v10_11),
|
||||
.tvOS(.v9),
|
||||
.watchOS(.v3)
|
||||
.watchOS(.v3),
|
||||
],
|
||||
products: [
|
||||
// Products define the executables and libraries produced by a package, and make them visible to other packages.
|
||||
.library(
|
||||
name: "PetstoreClient",
|
||||
targets: ["PetstoreClient"])
|
||||
targets: ["PetstoreClient"]
|
||||
),
|
||||
],
|
||||
dependencies: [
|
||||
// Dependencies declare other packages that this package depends on.
|
||||
@@ -26,6 +27,6 @@ let package = Package(
|
||||
name: "PetstoreClient",
|
||||
dependencies: [],
|
||||
path: "PetstoreClient/Classes"
|
||||
)
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -364,7 +364,7 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
@@ -447,7 +447,7 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
|
||||
@@ -8,7 +8,7 @@ import Foundation
|
||||
|
||||
public struct APIHelper {
|
||||
public static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
|
||||
let destination = source.reduce(into: [String: Any]()) { (result, item) in
|
||||
let destination = source.reduce(into: [String: Any]()) { result, item in
|
||||
if let value = item.value {
|
||||
result[item.key] = value
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public struct APIHelper {
|
||||
}
|
||||
|
||||
public static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
|
||||
return source.reduce(into: [String: String]()) { (result, item) in
|
||||
return source.reduce(into: [String: String]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
result[item.key] = collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
} else if let value: Any = item.value {
|
||||
@@ -35,27 +35,27 @@ public struct APIHelper {
|
||||
return nil
|
||||
}
|
||||
|
||||
return source.reduce(into: [String: Any](), { (result, item) in
|
||||
return source.reduce(into: [String: Any]()) { result, item in
|
||||
switch item.value {
|
||||
case let x as Bool:
|
||||
result[item.key] = x.description
|
||||
default:
|
||||
result[item.key] = item.value
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public static func mapValueToPathItem(_ source: Any) -> Any {
|
||||
if let collection = source as? [Any?] {
|
||||
return collection.filter { $0 != nil }.map {"\($0!)"}.joined(separator: ",")
|
||||
return collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
public static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
|
||||
let destination = source.filter({ $0.value != nil}).reduce(into: [URLQueryItem]()) { (result, item) in
|
||||
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
collection.filter { $0 != nil }.map {"\($0!)"}.forEach { value in
|
||||
collection.filter { $0 != nil }.map { "\($0!)" }.forEach { value in
|
||||
result.append(URLQueryItem(name: item.key, value: value))
|
||||
}
|
||||
} else if let value = item.value {
|
||||
|
||||
@@ -51,7 +51,7 @@ open class RequestBuilder<T> {
|
||||
}
|
||||
|
||||
open func addCredential() -> Self {
|
||||
self.credential = PetstoreClientAPI.credential
|
||||
credential = PetstoreClientAPI.credential
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,41 +8,42 @@
|
||||
import Foundation
|
||||
|
||||
open class CodableHelper {
|
||||
|
||||
private static var customDateFormatter: DateFormatter?
|
||||
private static var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter()
|
||||
|
||||
private static var customJSONDecoder: JSONDecoder?
|
||||
private static var defaultJSONDecoder: JSONDecoder = {
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
return decoder
|
||||
}()
|
||||
|
||||
private static var customJSONEncoder: JSONEncoder?
|
||||
private static var defaultJSONEncoder: JSONEncoder = {
|
||||
let encoder = JSONEncoder()
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
return encoder
|
||||
}()
|
||||
|
||||
public static var dateFormatter: DateFormatter {
|
||||
get { return self.customDateFormatter ?? self.defaultDateFormatter }
|
||||
set { self.customDateFormatter = newValue }
|
||||
get { return customDateFormatter ?? defaultDateFormatter }
|
||||
set { customDateFormatter = newValue }
|
||||
}
|
||||
public static var jsonDecoder: JSONDecoder {
|
||||
get { return self.customJSONDecoder ?? self.defaultJSONDecoder }
|
||||
set { self.customJSONDecoder = newValue }
|
||||
get { return customJSONDecoder ?? defaultJSONDecoder }
|
||||
set { customJSONDecoder = newValue }
|
||||
}
|
||||
public static var jsonEncoder: JSONEncoder {
|
||||
get { return self.customJSONEncoder ?? self.defaultJSONEncoder }
|
||||
set { self.customJSONEncoder = newValue }
|
||||
get { return customJSONEncoder ?? defaultJSONEncoder }
|
||||
set { customJSONEncoder = newValue }
|
||||
}
|
||||
|
||||
open class func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable {
|
||||
return Swift.Result { try self.jsonDecoder.decode(type, from: data) }
|
||||
return Swift.Result { try jsonDecoder.decode(type, from: data) }
|
||||
}
|
||||
|
||||
open class func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable {
|
||||
return Swift.Result { try self.jsonEncoder.encode(value) }
|
||||
return Swift.Result { try jsonEncoder.encode(value) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,8 @@
|
||||
import Foundation
|
||||
|
||||
open class Configuration {
|
||||
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
|
||||
|
||||
}
|
||||
|
||||
@@ -7,35 +7,35 @@
|
||||
import Foundation
|
||||
|
||||
extension Bool: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Float: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int32: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int32) }
|
||||
}
|
||||
|
||||
extension Int64: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int64) }
|
||||
}
|
||||
|
||||
extension Double: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension String: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension RawRepresentable where RawValue: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self.rawValue as Any }
|
||||
func encodeToJSON() -> Any { self.rawValue as Any }
|
||||
}
|
||||
|
||||
private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
@@ -48,7 +48,7 @@ private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
|
||||
extension Array: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.map(encodeIfPossible)
|
||||
self.map(encodeIfPossible)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,32 +64,32 @@ extension Dictionary: JSONEncodable {
|
||||
|
||||
extension Data: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
}
|
||||
}
|
||||
|
||||
extension Date: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return CodableHelper.dateFormatter.string(from: self) as Any
|
||||
CodableHelper.dateFormatter.string(from: self) as Any
|
||||
}
|
||||
}
|
||||
|
||||
extension URL: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
extension UUID: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.uuidString
|
||||
uuidString
|
||||
}
|
||||
}
|
||||
|
||||
extension String: CodingKey {
|
||||
|
||||
public var stringValue: String {
|
||||
return self
|
||||
self
|
||||
}
|
||||
|
||||
public init?(stringValue: String) {
|
||||
@@ -97,11 +97,11 @@ extension String: CodingKey {
|
||||
}
|
||||
|
||||
public var intValue: Int? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
public init?(intValue: Int) {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
}
|
||||
@@ -174,6 +174,6 @@ extension KeyedDecodingContainerProtocol {
|
||||
|
||||
extension HTTPURLResponse {
|
||||
var isStatusCodeSuccessful: Bool {
|
||||
return Array(200 ..< 300).contains(statusCode)
|
||||
Array(200 ..< 300).contains(statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,11 @@ import MobileCoreServices
|
||||
|
||||
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
|
||||
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionRequestBuilder<T>.self
|
||||
URLSessionRequestBuilder<T>.self
|
||||
}
|
||||
|
||||
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionDecodableRequestBuilder<T>.self
|
||||
URLSessionDecodableRequestBuilder<T>.self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
the file extension). Return the desired Content-Type otherwise.
|
||||
*/
|
||||
open func contentTypeForFormPart(fileURL: URL) -> String? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -209,11 +209,11 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
let fileManager = FileManager.default
|
||||
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
|
||||
let requestURL = try self.getURL(from: urlRequest)
|
||||
let requestURL = try getURL(from: urlRequest)
|
||||
|
||||
var requestPath = try self.getPath(from: requestURL)
|
||||
var requestPath = try getPath(from: requestURL)
|
||||
|
||||
if let headerFileName = self.getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
requestPath = requestPath.appending("/\(headerFileName)")
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
} catch let error {
|
||||
} catch {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, error)))
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
open func buildHeaders() -> [String: String] {
|
||||
var httpHeaders: [String: String] = [:]
|
||||
for (key, value) in self.headers {
|
||||
for (key, value) in headers {
|
||||
httpHeaders[key] = value
|
||||
}
|
||||
for (key, value) in PetstoreClientAPI.customHeaders {
|
||||
@@ -390,13 +390,13 @@ private class SessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
|
||||
|
||||
public enum HTTPMethod: String {
|
||||
case options = "OPTIONS"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case connect = "CONNECT"
|
||||
}
|
||||
|
||||
@@ -499,7 +499,7 @@ private class FormDataEncoding: ParameterEncoding {
|
||||
|
||||
let fileData = try Data(contentsOf: fileURL)
|
||||
|
||||
let mimetype = self.contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
let mimetype = contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
|
||||
let fileName = fileURL.lastPathComponent
|
||||
|
||||
|
||||
@@ -8,13 +8,14 @@ let package = Package(
|
||||
.iOS(.v9),
|
||||
.macOS(.v10_11),
|
||||
.tvOS(.v9),
|
||||
.watchOS(.v3)
|
||||
.watchOS(.v3),
|
||||
],
|
||||
products: [
|
||||
// Products define the executables and libraries produced by a package, and make them visible to other packages.
|
||||
.library(
|
||||
name: "PetstoreClient",
|
||||
targets: ["PetstoreClient"])
|
||||
targets: ["PetstoreClient"]
|
||||
),
|
||||
],
|
||||
dependencies: [
|
||||
// Dependencies declare other packages that this package depends on.
|
||||
@@ -26,6 +27,6 @@ let package = Package(
|
||||
name: "PetstoreClient",
|
||||
dependencies: [],
|
||||
path: "PetstoreClient/Classes"
|
||||
)
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -224,7 +224,7 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
@@ -307,7 +307,7 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
|
||||
@@ -8,7 +8,7 @@ import Foundation
|
||||
|
||||
public struct APIHelper {
|
||||
public static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
|
||||
let destination = source.reduce(into: [String: Any]()) { (result, item) in
|
||||
let destination = source.reduce(into: [String: Any]()) { result, item in
|
||||
if let value = item.value {
|
||||
result[item.key] = value
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public struct APIHelper {
|
||||
}
|
||||
|
||||
public static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
|
||||
return source.reduce(into: [String: String]()) { (result, item) in
|
||||
return source.reduce(into: [String: String]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
result[item.key] = collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
} else if let value: Any = item.value {
|
||||
@@ -35,27 +35,27 @@ public struct APIHelper {
|
||||
return nil
|
||||
}
|
||||
|
||||
return source.reduce(into: [String: Any](), { (result, item) in
|
||||
return source.reduce(into: [String: Any]()) { result, item in
|
||||
switch item.value {
|
||||
case let x as Bool:
|
||||
result[item.key] = x.description
|
||||
default:
|
||||
result[item.key] = item.value
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public static func mapValueToPathItem(_ source: Any) -> Any {
|
||||
if let collection = source as? [Any?] {
|
||||
return collection.filter { $0 != nil }.map {"\($0!)"}.joined(separator: ",")
|
||||
return collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
public static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
|
||||
let destination = source.filter({ $0.value != nil}).reduce(into: [URLQueryItem]()) { (result, item) in
|
||||
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
collection.filter { $0 != nil }.map {"\($0!)"}.forEach { value in
|
||||
collection.filter { $0 != nil }.map { "\($0!)" }.forEach { value in
|
||||
result.append(URLQueryItem(name: item.key, value: value))
|
||||
}
|
||||
} else if let value = item.value {
|
||||
|
||||
@@ -51,7 +51,7 @@ open class RequestBuilder<T> {
|
||||
}
|
||||
|
||||
open func addCredential() -> Self {
|
||||
self.credential = PetstoreClientAPI.credential
|
||||
credential = PetstoreClientAPI.credential
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,41 +8,42 @@
|
||||
import Foundation
|
||||
|
||||
open class CodableHelper {
|
||||
|
||||
private static var customDateFormatter: DateFormatter?
|
||||
private static var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter()
|
||||
|
||||
private static var customJSONDecoder: JSONDecoder?
|
||||
private static var defaultJSONDecoder: JSONDecoder = {
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
return decoder
|
||||
}()
|
||||
|
||||
private static var customJSONEncoder: JSONEncoder?
|
||||
private static var defaultJSONEncoder: JSONEncoder = {
|
||||
let encoder = JSONEncoder()
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
return encoder
|
||||
}()
|
||||
|
||||
public static var dateFormatter: DateFormatter {
|
||||
get { return self.customDateFormatter ?? self.defaultDateFormatter }
|
||||
set { self.customDateFormatter = newValue }
|
||||
get { return customDateFormatter ?? defaultDateFormatter }
|
||||
set { customDateFormatter = newValue }
|
||||
}
|
||||
public static var jsonDecoder: JSONDecoder {
|
||||
get { return self.customJSONDecoder ?? self.defaultJSONDecoder }
|
||||
set { self.customJSONDecoder = newValue }
|
||||
get { return customJSONDecoder ?? defaultJSONDecoder }
|
||||
set { customJSONDecoder = newValue }
|
||||
}
|
||||
public static var jsonEncoder: JSONEncoder {
|
||||
get { return self.customJSONEncoder ?? self.defaultJSONEncoder }
|
||||
set { self.customJSONEncoder = newValue }
|
||||
get { return customJSONEncoder ?? defaultJSONEncoder }
|
||||
set { customJSONEncoder = newValue }
|
||||
}
|
||||
|
||||
open class func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable {
|
||||
return Swift.Result { try self.jsonDecoder.decode(type, from: data) }
|
||||
return Swift.Result { try jsonDecoder.decode(type, from: data) }
|
||||
}
|
||||
|
||||
open class func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable {
|
||||
return Swift.Result { try self.jsonEncoder.encode(value) }
|
||||
return Swift.Result { try jsonEncoder.encode(value) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,8 @@
|
||||
import Foundation
|
||||
|
||||
open class Configuration {
|
||||
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
|
||||
|
||||
}
|
||||
|
||||
@@ -7,35 +7,35 @@
|
||||
import Foundation
|
||||
|
||||
extension Bool: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Float: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int32: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int32) }
|
||||
}
|
||||
|
||||
extension Int64: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int64) }
|
||||
}
|
||||
|
||||
extension Double: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension String: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension RawRepresentable where RawValue: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self.rawValue as Any }
|
||||
func encodeToJSON() -> Any { self.rawValue as Any }
|
||||
}
|
||||
|
||||
private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
@@ -48,7 +48,7 @@ private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
|
||||
extension Array: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.map(encodeIfPossible)
|
||||
self.map(encodeIfPossible)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,32 +64,32 @@ extension Dictionary: JSONEncodable {
|
||||
|
||||
extension Data: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
}
|
||||
}
|
||||
|
||||
extension Date: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return CodableHelper.dateFormatter.string(from: self) as Any
|
||||
CodableHelper.dateFormatter.string(from: self) as Any
|
||||
}
|
||||
}
|
||||
|
||||
extension URL: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
extension UUID: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.uuidString
|
||||
uuidString
|
||||
}
|
||||
}
|
||||
|
||||
extension String: CodingKey {
|
||||
|
||||
public var stringValue: String {
|
||||
return self
|
||||
self
|
||||
}
|
||||
|
||||
public init?(stringValue: String) {
|
||||
@@ -97,11 +97,11 @@ extension String: CodingKey {
|
||||
}
|
||||
|
||||
public var intValue: Int? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
public init?(intValue: Int) {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
}
|
||||
@@ -174,6 +174,6 @@ extension KeyedDecodingContainerProtocol {
|
||||
|
||||
extension HTTPURLResponse {
|
||||
var isStatusCodeSuccessful: Bool {
|
||||
return Array(200 ..< 300).contains(statusCode)
|
||||
Array(200 ..< 300).contains(statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,11 @@ import MobileCoreServices
|
||||
|
||||
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
|
||||
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionRequestBuilder<T>.self
|
||||
URLSessionRequestBuilder<T>.self
|
||||
}
|
||||
|
||||
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionDecodableRequestBuilder<T>.self
|
||||
URLSessionDecodableRequestBuilder<T>.self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
the file extension). Return the desired Content-Type otherwise.
|
||||
*/
|
||||
open func contentTypeForFormPart(fileURL: URL) -> String? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -209,11 +209,11 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
let fileManager = FileManager.default
|
||||
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
|
||||
let requestURL = try self.getURL(from: urlRequest)
|
||||
let requestURL = try getURL(from: urlRequest)
|
||||
|
||||
var requestPath = try self.getPath(from: requestURL)
|
||||
var requestPath = try getPath(from: requestURL)
|
||||
|
||||
if let headerFileName = self.getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
requestPath = requestPath.appending("/\(headerFileName)")
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
} catch let error {
|
||||
} catch {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, error)))
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
open func buildHeaders() -> [String: String] {
|
||||
var httpHeaders: [String: String] = [:]
|
||||
for (key, value) in self.headers {
|
||||
for (key, value) in headers {
|
||||
httpHeaders[key] = value
|
||||
}
|
||||
for (key, value) in PetstoreClientAPI.customHeaders {
|
||||
@@ -390,13 +390,13 @@ private class SessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
|
||||
|
||||
public enum HTTPMethod: String {
|
||||
case options = "OPTIONS"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case connect = "CONNECT"
|
||||
}
|
||||
|
||||
@@ -499,7 +499,7 @@ private class FormDataEncoding: ParameterEncoding {
|
||||
|
||||
let fileData = try Data(contentsOf: fileURL)
|
||||
|
||||
let mimetype = self.contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
let mimetype = contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
|
||||
let fileName = fileURL.lastPathComponent
|
||||
|
||||
|
||||
@@ -8,13 +8,14 @@ let package = Package(
|
||||
.iOS(.v9),
|
||||
.macOS(.v10_11),
|
||||
.tvOS(.v9),
|
||||
.watchOS(.v3)
|
||||
.watchOS(.v3),
|
||||
],
|
||||
products: [
|
||||
// Products define the executables and libraries produced by a package, and make them visible to other packages.
|
||||
.library(
|
||||
name: "PetstoreClient",
|
||||
targets: ["PetstoreClient"])
|
||||
targets: ["PetstoreClient"]
|
||||
),
|
||||
],
|
||||
dependencies: [
|
||||
// Dependencies declare other packages that this package depends on.
|
||||
@@ -26,6 +27,6 @@ let package = Package(
|
||||
name: "PetstoreClient",
|
||||
dependencies: [],
|
||||
path: "PetstoreClient/Classes"
|
||||
)
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -364,7 +364,7 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
@@ -447,7 +447,7 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
|
||||
@@ -8,7 +8,7 @@ import Foundation
|
||||
|
||||
internal struct APIHelper {
|
||||
internal static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
|
||||
let destination = source.reduce(into: [String: Any]()) { (result, item) in
|
||||
let destination = source.reduce(into: [String: Any]()) { result, item in
|
||||
if let value = item.value {
|
||||
result[item.key] = value
|
||||
}
|
||||
@@ -21,7 +21,7 @@ internal struct APIHelper {
|
||||
}
|
||||
|
||||
internal static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
|
||||
return source.reduce(into: [String: String]()) { (result, item) in
|
||||
return source.reduce(into: [String: String]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
result[item.key] = collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
} else if let value: Any = item.value {
|
||||
@@ -35,27 +35,27 @@ internal struct APIHelper {
|
||||
return nil
|
||||
}
|
||||
|
||||
return source.reduce(into: [String: Any](), { (result, item) in
|
||||
return source.reduce(into: [String: Any]()) { result, item in
|
||||
switch item.value {
|
||||
case let x as Bool:
|
||||
result[item.key] = x.description
|
||||
default:
|
||||
result[item.key] = item.value
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
internal static func mapValueToPathItem(_ source: Any) -> Any {
|
||||
if let collection = source as? [Any?] {
|
||||
return collection.filter { $0 != nil }.map {"\($0!)"}.joined(separator: ",")
|
||||
return collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
internal static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
|
||||
let destination = source.filter({ $0.value != nil}).reduce(into: [URLQueryItem]()) { (result, item) in
|
||||
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
collection.filter { $0 != nil }.map {"\($0!)"}.forEach { value in
|
||||
collection.filter { $0 != nil }.map { "\($0!)" }.forEach { value in
|
||||
result.append(URLQueryItem(name: item.key, value: value))
|
||||
}
|
||||
} else if let value = item.value {
|
||||
|
||||
@@ -51,7 +51,7 @@ internal class RequestBuilder<T> {
|
||||
}
|
||||
|
||||
internal func addCredential() -> Self {
|
||||
self.credential = PetstoreClientAPI.credential
|
||||
credential = PetstoreClientAPI.credential
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,41 +8,42 @@
|
||||
import Foundation
|
||||
|
||||
internal class CodableHelper {
|
||||
|
||||
private static var customDateFormatter: DateFormatter?
|
||||
private static var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter()
|
||||
|
||||
private static var customJSONDecoder: JSONDecoder?
|
||||
private static var defaultJSONDecoder: JSONDecoder = {
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
return decoder
|
||||
}()
|
||||
|
||||
private static var customJSONEncoder: JSONEncoder?
|
||||
private static var defaultJSONEncoder: JSONEncoder = {
|
||||
let encoder = JSONEncoder()
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
return encoder
|
||||
}()
|
||||
|
||||
internal static var dateFormatter: DateFormatter {
|
||||
get { return self.customDateFormatter ?? self.defaultDateFormatter }
|
||||
set { self.customDateFormatter = newValue }
|
||||
get { return customDateFormatter ?? defaultDateFormatter }
|
||||
set { customDateFormatter = newValue }
|
||||
}
|
||||
internal static var jsonDecoder: JSONDecoder {
|
||||
get { return self.customJSONDecoder ?? self.defaultJSONDecoder }
|
||||
set { self.customJSONDecoder = newValue }
|
||||
get { return customJSONDecoder ?? defaultJSONDecoder }
|
||||
set { customJSONDecoder = newValue }
|
||||
}
|
||||
internal static var jsonEncoder: JSONEncoder {
|
||||
get { return self.customJSONEncoder ?? self.defaultJSONEncoder }
|
||||
set { self.customJSONEncoder = newValue }
|
||||
get { return customJSONEncoder ?? defaultJSONEncoder }
|
||||
set { customJSONEncoder = newValue }
|
||||
}
|
||||
|
||||
internal class func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable {
|
||||
return Swift.Result { try self.jsonDecoder.decode(type, from: data) }
|
||||
return Swift.Result { try jsonDecoder.decode(type, from: data) }
|
||||
}
|
||||
|
||||
internal class func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable {
|
||||
return Swift.Result { try self.jsonEncoder.encode(value) }
|
||||
return Swift.Result { try jsonEncoder.encode(value) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,8 @@
|
||||
import Foundation
|
||||
|
||||
internal class Configuration {
|
||||
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
internal static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
|
||||
|
||||
}
|
||||
|
||||
@@ -7,35 +7,35 @@
|
||||
import Foundation
|
||||
|
||||
extension Bool: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Float: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int32: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int32) }
|
||||
}
|
||||
|
||||
extension Int64: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int64) }
|
||||
}
|
||||
|
||||
extension Double: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension String: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension RawRepresentable where RawValue: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self.rawValue as Any }
|
||||
func encodeToJSON() -> Any { self.rawValue as Any }
|
||||
}
|
||||
|
||||
private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
@@ -48,7 +48,7 @@ private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
|
||||
extension Array: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.map(encodeIfPossible)
|
||||
self.map(encodeIfPossible)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,32 +64,32 @@ extension Dictionary: JSONEncodable {
|
||||
|
||||
extension Data: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
}
|
||||
}
|
||||
|
||||
extension Date: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return CodableHelper.dateFormatter.string(from: self) as Any
|
||||
CodableHelper.dateFormatter.string(from: self) as Any
|
||||
}
|
||||
}
|
||||
|
||||
extension URL: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
extension UUID: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.uuidString
|
||||
uuidString
|
||||
}
|
||||
}
|
||||
|
||||
extension String: CodingKey {
|
||||
|
||||
public var stringValue: String {
|
||||
return self
|
||||
self
|
||||
}
|
||||
|
||||
public init?(stringValue: String) {
|
||||
@@ -97,11 +97,11 @@ extension String: CodingKey {
|
||||
}
|
||||
|
||||
public var intValue: Int? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
public init?(intValue: Int) {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
}
|
||||
@@ -174,6 +174,6 @@ extension KeyedDecodingContainerProtocol {
|
||||
|
||||
extension HTTPURLResponse {
|
||||
var isStatusCodeSuccessful: Bool {
|
||||
return Array(200 ..< 300).contains(statusCode)
|
||||
Array(200 ..< 300).contains(statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,11 @@ import MobileCoreServices
|
||||
|
||||
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
|
||||
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionRequestBuilder<T>.self
|
||||
URLSessionRequestBuilder<T>.self
|
||||
}
|
||||
|
||||
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionDecodableRequestBuilder<T>.self
|
||||
URLSessionDecodableRequestBuilder<T>.self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ internal class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
the file extension). Return the desired Content-Type otherwise.
|
||||
*/
|
||||
internal func contentTypeForFormPart(fileURL: URL) -> String? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -209,11 +209,11 @@ internal class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
let fileManager = FileManager.default
|
||||
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
|
||||
let requestURL = try self.getURL(from: urlRequest)
|
||||
let requestURL = try getURL(from: urlRequest)
|
||||
|
||||
var requestPath = try self.getPath(from: requestURL)
|
||||
var requestPath = try getPath(from: requestURL)
|
||||
|
||||
if let headerFileName = self.getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
requestPath = requestPath.appending("/\(headerFileName)")
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@ internal class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
} catch let error {
|
||||
} catch {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, error)))
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ internal class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
internal func buildHeaders() -> [String: String] {
|
||||
var httpHeaders: [String: String] = [:]
|
||||
for (key, value) in self.headers {
|
||||
for (key, value) in headers {
|
||||
httpHeaders[key] = value
|
||||
}
|
||||
for (key, value) in PetstoreClientAPI.customHeaders {
|
||||
@@ -390,13 +390,13 @@ private class SessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
|
||||
|
||||
internal enum HTTPMethod: String {
|
||||
case options = "OPTIONS"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case connect = "CONNECT"
|
||||
}
|
||||
|
||||
@@ -499,7 +499,7 @@ private class FormDataEncoding: ParameterEncoding {
|
||||
|
||||
let fileData = try Data(contentsOf: fileURL)
|
||||
|
||||
let mimetype = self.contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
let mimetype = contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
|
||||
let fileName = fileURL.lastPathComponent
|
||||
|
||||
|
||||
@@ -8,13 +8,14 @@ let package = Package(
|
||||
.iOS(.v9),
|
||||
.macOS(.v10_11),
|
||||
.tvOS(.v9),
|
||||
.watchOS(.v3)
|
||||
.watchOS(.v3),
|
||||
],
|
||||
products: [
|
||||
// Products define the executables and libraries produced by a package, and make them visible to other packages.
|
||||
.library(
|
||||
name: "PetstoreClient",
|
||||
targets: ["PetstoreClient"])
|
||||
targets: ["PetstoreClient"]
|
||||
),
|
||||
],
|
||||
dependencies: [
|
||||
// Dependencies declare other packages that this package depends on.
|
||||
@@ -26,6 +27,6 @@ let package = Package(
|
||||
name: "PetstoreClient",
|
||||
dependencies: [],
|
||||
path: "PetstoreClient/Classes"
|
||||
)
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -364,7 +364,7 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
@@ -447,7 +447,7 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
|
||||
@@ -8,7 +8,7 @@ import Foundation
|
||||
|
||||
public struct APIHelper {
|
||||
public static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
|
||||
let destination = source.reduce(into: [String: Any]()) { (result, item) in
|
||||
let destination = source.reduce(into: [String: Any]()) { result, item in
|
||||
if let value = item.value {
|
||||
result[item.key] = value
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public struct APIHelper {
|
||||
}
|
||||
|
||||
public static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
|
||||
return source.reduce(into: [String: String]()) { (result, item) in
|
||||
return source.reduce(into: [String: String]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
result[item.key] = collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
} else if let value: Any = item.value {
|
||||
@@ -35,27 +35,27 @@ public struct APIHelper {
|
||||
return nil
|
||||
}
|
||||
|
||||
return source.reduce(into: [String: Any](), { (result, item) in
|
||||
return source.reduce(into: [String: Any]()) { result, item in
|
||||
switch item.value {
|
||||
case let x as Bool:
|
||||
result[item.key] = x.description
|
||||
default:
|
||||
result[item.key] = item.value
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public static func mapValueToPathItem(_ source: Any) -> Any {
|
||||
if let collection = source as? [Any?] {
|
||||
return collection.filter { $0 != nil }.map {"\($0!)"}.joined(separator: ",")
|
||||
return collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
public static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
|
||||
let destination = source.filter({ $0.value != nil}).reduce(into: [URLQueryItem]()) { (result, item) in
|
||||
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
collection.filter { $0 != nil }.map {"\($0!)"}.forEach { value in
|
||||
collection.filter { $0 != nil }.map { "\($0!)" }.forEach { value in
|
||||
result.append(URLQueryItem(name: item.key, value: value))
|
||||
}
|
||||
} else if let value = item.value {
|
||||
|
||||
@@ -51,7 +51,7 @@ open class RequestBuilder<T> {
|
||||
}
|
||||
|
||||
open func addCredential() -> Self {
|
||||
self.credential = PetstoreClientAPI.credential
|
||||
credential = PetstoreClientAPI.credential
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,41 +8,42 @@
|
||||
import Foundation
|
||||
|
||||
open class CodableHelper {
|
||||
|
||||
private static var customDateFormatter: DateFormatter?
|
||||
private static var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter()
|
||||
|
||||
private static var customJSONDecoder: JSONDecoder?
|
||||
private static var defaultJSONDecoder: JSONDecoder = {
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
return decoder
|
||||
}()
|
||||
|
||||
private static var customJSONEncoder: JSONEncoder?
|
||||
private static var defaultJSONEncoder: JSONEncoder = {
|
||||
let encoder = JSONEncoder()
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
return encoder
|
||||
}()
|
||||
|
||||
public static var dateFormatter: DateFormatter {
|
||||
get { return self.customDateFormatter ?? self.defaultDateFormatter }
|
||||
set { self.customDateFormatter = newValue }
|
||||
get { return customDateFormatter ?? defaultDateFormatter }
|
||||
set { customDateFormatter = newValue }
|
||||
}
|
||||
public static var jsonDecoder: JSONDecoder {
|
||||
get { return self.customJSONDecoder ?? self.defaultJSONDecoder }
|
||||
set { self.customJSONDecoder = newValue }
|
||||
get { return customJSONDecoder ?? defaultJSONDecoder }
|
||||
set { customJSONDecoder = newValue }
|
||||
}
|
||||
public static var jsonEncoder: JSONEncoder {
|
||||
get { return self.customJSONEncoder ?? self.defaultJSONEncoder }
|
||||
set { self.customJSONEncoder = newValue }
|
||||
get { return customJSONEncoder ?? defaultJSONEncoder }
|
||||
set { customJSONEncoder = newValue }
|
||||
}
|
||||
|
||||
open class func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable {
|
||||
return Swift.Result { try self.jsonDecoder.decode(type, from: data) }
|
||||
return Swift.Result { try jsonDecoder.decode(type, from: data) }
|
||||
}
|
||||
|
||||
open class func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable {
|
||||
return Swift.Result { try self.jsonEncoder.encode(value) }
|
||||
return Swift.Result { try jsonEncoder.encode(value) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,8 @@
|
||||
import Foundation
|
||||
|
||||
open class Configuration {
|
||||
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
|
||||
|
||||
}
|
||||
|
||||
@@ -7,35 +7,35 @@
|
||||
import Foundation
|
||||
|
||||
extension Bool: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Float: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int32: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int32) }
|
||||
}
|
||||
|
||||
extension Int64: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int64) }
|
||||
}
|
||||
|
||||
extension Double: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension String: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension RawRepresentable where RawValue: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self.rawValue as Any }
|
||||
func encodeToJSON() -> Any { self.rawValue as Any }
|
||||
}
|
||||
|
||||
private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
@@ -48,7 +48,7 @@ private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
|
||||
extension Array: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.map(encodeIfPossible)
|
||||
self.map(encodeIfPossible)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,32 +64,32 @@ extension Dictionary: JSONEncodable {
|
||||
|
||||
extension Data: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
}
|
||||
}
|
||||
|
||||
extension Date: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return CodableHelper.dateFormatter.string(from: self) as Any
|
||||
CodableHelper.dateFormatter.string(from: self) as Any
|
||||
}
|
||||
}
|
||||
|
||||
extension URL: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
extension UUID: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.uuidString
|
||||
uuidString
|
||||
}
|
||||
}
|
||||
|
||||
extension String: CodingKey {
|
||||
|
||||
public var stringValue: String {
|
||||
return self
|
||||
self
|
||||
}
|
||||
|
||||
public init?(stringValue: String) {
|
||||
@@ -97,11 +97,11 @@ extension String: CodingKey {
|
||||
}
|
||||
|
||||
public var intValue: Int? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
public init?(intValue: Int) {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
}
|
||||
@@ -174,6 +174,6 @@ extension KeyedDecodingContainerProtocol {
|
||||
|
||||
extension HTTPURLResponse {
|
||||
var isStatusCodeSuccessful: Bool {
|
||||
return Array(200 ..< 300).contains(statusCode)
|
||||
Array(200 ..< 300).contains(statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,11 @@ import MobileCoreServices
|
||||
|
||||
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
|
||||
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionRequestBuilder<T>.self
|
||||
URLSessionRequestBuilder<T>.self
|
||||
}
|
||||
|
||||
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionDecodableRequestBuilder<T>.self
|
||||
URLSessionDecodableRequestBuilder<T>.self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
the file extension). Return the desired Content-Type otherwise.
|
||||
*/
|
||||
open func contentTypeForFormPart(fileURL: URL) -> String? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -209,11 +209,11 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
let fileManager = FileManager.default
|
||||
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
|
||||
let requestURL = try self.getURL(from: urlRequest)
|
||||
let requestURL = try getURL(from: urlRequest)
|
||||
|
||||
var requestPath = try self.getPath(from: requestURL)
|
||||
var requestPath = try getPath(from: requestURL)
|
||||
|
||||
if let headerFileName = self.getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
requestPath = requestPath.appending("/\(headerFileName)")
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
} catch let error {
|
||||
} catch {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, error)))
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
open func buildHeaders() -> [String: String] {
|
||||
var httpHeaders: [String: String] = [:]
|
||||
for (key, value) in self.headers {
|
||||
for (key, value) in headers {
|
||||
httpHeaders[key] = value
|
||||
}
|
||||
for (key, value) in PetstoreClientAPI.customHeaders {
|
||||
@@ -390,13 +390,13 @@ private class SessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
|
||||
|
||||
public enum HTTPMethod: String {
|
||||
case options = "OPTIONS"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case connect = "CONNECT"
|
||||
}
|
||||
|
||||
@@ -499,7 +499,7 @@ private class FormDataEncoding: ParameterEncoding {
|
||||
|
||||
let fileData = try Data(contentsOf: fileURL)
|
||||
|
||||
let mimetype = self.contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
let mimetype = contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
|
||||
let fileName = fileURL.lastPathComponent
|
||||
|
||||
|
||||
@@ -8,25 +8,26 @@ let package = Package(
|
||||
.iOS(.v9),
|
||||
.macOS(.v10_11),
|
||||
.tvOS(.v9),
|
||||
.watchOS(.v3)
|
||||
.watchOS(.v3),
|
||||
],
|
||||
products: [
|
||||
// Products define the executables and libraries produced by a package, and make them visible to other packages.
|
||||
.library(
|
||||
name: "PetstoreClient",
|
||||
targets: ["PetstoreClient"])
|
||||
targets: ["PetstoreClient"]
|
||||
),
|
||||
],
|
||||
dependencies: [
|
||||
// Dependencies declare other packages that this package depends on.
|
||||
.package(url: "https://github.com/mxcl/PromiseKit.git", from: "6.12.0")
|
||||
.package(url: "https://github.com/mxcl/PromiseKit.git", from: "6.12.0"),
|
||||
],
|
||||
targets: [
|
||||
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
|
||||
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
|
||||
.target(
|
||||
name: "PetstoreClient",
|
||||
dependencies: ["PromiseKit" ],
|
||||
dependencies: ["PromiseKit", ],
|
||||
path: "PetstoreClient/Classes"
|
||||
)
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -408,7 +408,7 @@
|
||||
);
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
@@ -495,7 +495,7 @@
|
||||
);
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
|
||||
@@ -8,7 +8,7 @@ import Foundation
|
||||
|
||||
public struct APIHelper {
|
||||
public static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
|
||||
let destination = source.reduce(into: [String: Any]()) { (result, item) in
|
||||
let destination = source.reduce(into: [String: Any]()) { result, item in
|
||||
if let value = item.value {
|
||||
result[item.key] = value
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public struct APIHelper {
|
||||
}
|
||||
|
||||
public static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
|
||||
return source.reduce(into: [String: String]()) { (result, item) in
|
||||
return source.reduce(into: [String: String]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
result[item.key] = collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
} else if let value: Any = item.value {
|
||||
@@ -35,27 +35,27 @@ public struct APIHelper {
|
||||
return nil
|
||||
}
|
||||
|
||||
return source.reduce(into: [String: Any](), { (result, item) in
|
||||
return source.reduce(into: [String: Any]()) { result, item in
|
||||
switch item.value {
|
||||
case let x as Bool:
|
||||
result[item.key] = x.description
|
||||
default:
|
||||
result[item.key] = item.value
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public static func mapValueToPathItem(_ source: Any) -> Any {
|
||||
if let collection = source as? [Any?] {
|
||||
return collection.filter { $0 != nil }.map {"\($0!)"}.joined(separator: ",")
|
||||
return collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
public static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
|
||||
let destination = source.filter({ $0.value != nil}).reduce(into: [URLQueryItem]()) { (result, item) in
|
||||
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
collection.filter { $0 != nil }.map {"\($0!)"}.forEach { value in
|
||||
collection.filter { $0 != nil }.map { "\($0!)" }.forEach { value in
|
||||
result.append(URLQueryItem(name: item.key, value: value))
|
||||
}
|
||||
} else if let value = item.value {
|
||||
|
||||
@@ -51,7 +51,7 @@ open class RequestBuilder<T> {
|
||||
}
|
||||
|
||||
open func addCredential() -> Self {
|
||||
self.credential = PetstoreClientAPI.credential
|
||||
credential = PetstoreClientAPI.credential
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,41 +8,42 @@
|
||||
import Foundation
|
||||
|
||||
open class CodableHelper {
|
||||
|
||||
private static var customDateFormatter: DateFormatter?
|
||||
private static var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter()
|
||||
|
||||
private static var customJSONDecoder: JSONDecoder?
|
||||
private static var defaultJSONDecoder: JSONDecoder = {
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
return decoder
|
||||
}()
|
||||
|
||||
private static var customJSONEncoder: JSONEncoder?
|
||||
private static var defaultJSONEncoder: JSONEncoder = {
|
||||
let encoder = JSONEncoder()
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
return encoder
|
||||
}()
|
||||
|
||||
public static var dateFormatter: DateFormatter {
|
||||
get { return self.customDateFormatter ?? self.defaultDateFormatter }
|
||||
set { self.customDateFormatter = newValue }
|
||||
get { return customDateFormatter ?? defaultDateFormatter }
|
||||
set { customDateFormatter = newValue }
|
||||
}
|
||||
public static var jsonDecoder: JSONDecoder {
|
||||
get { return self.customJSONDecoder ?? self.defaultJSONDecoder }
|
||||
set { self.customJSONDecoder = newValue }
|
||||
get { return customJSONDecoder ?? defaultJSONDecoder }
|
||||
set { customJSONDecoder = newValue }
|
||||
}
|
||||
public static var jsonEncoder: JSONEncoder {
|
||||
get { return self.customJSONEncoder ?? self.defaultJSONEncoder }
|
||||
set { self.customJSONEncoder = newValue }
|
||||
get { return customJSONEncoder ?? defaultJSONEncoder }
|
||||
set { customJSONEncoder = newValue }
|
||||
}
|
||||
|
||||
open class func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable {
|
||||
return Swift.Result { try self.jsonDecoder.decode(type, from: data) }
|
||||
return Swift.Result { try jsonDecoder.decode(type, from: data) }
|
||||
}
|
||||
|
||||
open class func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable {
|
||||
return Swift.Result { try self.jsonEncoder.encode(value) }
|
||||
return Swift.Result { try jsonEncoder.encode(value) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,8 @@
|
||||
import Foundation
|
||||
|
||||
open class Configuration {
|
||||
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
|
||||
|
||||
}
|
||||
|
||||
@@ -8,35 +8,35 @@ import Foundation
|
||||
import PromiseKit
|
||||
|
||||
extension Bool: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Float: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int32: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int32) }
|
||||
}
|
||||
|
||||
extension Int64: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int64) }
|
||||
}
|
||||
|
||||
extension Double: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension String: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension RawRepresentable where RawValue: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self.rawValue as Any }
|
||||
func encodeToJSON() -> Any { self.rawValue as Any }
|
||||
}
|
||||
|
||||
private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
@@ -49,7 +49,7 @@ private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
|
||||
extension Array: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.map(encodeIfPossible)
|
||||
self.map(encodeIfPossible)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,32 +65,32 @@ extension Dictionary: JSONEncodable {
|
||||
|
||||
extension Data: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
}
|
||||
}
|
||||
|
||||
extension Date: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return CodableHelper.dateFormatter.string(from: self) as Any
|
||||
CodableHelper.dateFormatter.string(from: self) as Any
|
||||
}
|
||||
}
|
||||
|
||||
extension URL: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
extension UUID: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.uuidString
|
||||
uuidString
|
||||
}
|
||||
}
|
||||
|
||||
extension String: CodingKey {
|
||||
|
||||
public var stringValue: String {
|
||||
return self
|
||||
self
|
||||
}
|
||||
|
||||
public init?(stringValue: String) {
|
||||
@@ -98,11 +98,11 @@ extension String: CodingKey {
|
||||
}
|
||||
|
||||
public var intValue: Int? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
public init?(intValue: Int) {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
}
|
||||
@@ -175,7 +175,7 @@ extension KeyedDecodingContainerProtocol {
|
||||
|
||||
extension HTTPURLResponse {
|
||||
var isStatusCodeSuccessful: Bool {
|
||||
return Array(200 ..< 300).contains(statusCode)
|
||||
Array(200 ..< 300).contains(statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,11 +11,11 @@ import MobileCoreServices
|
||||
|
||||
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
|
||||
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionRequestBuilder<T>.self
|
||||
URLSessionRequestBuilder<T>.self
|
||||
}
|
||||
|
||||
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionDecodableRequestBuilder<T>.self
|
||||
URLSessionDecodableRequestBuilder<T>.self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
the file extension). Return the desired Content-Type otherwise.
|
||||
*/
|
||||
open func contentTypeForFormPart(fileURL: URL) -> String? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -209,11 +209,11 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
let fileManager = FileManager.default
|
||||
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
|
||||
let requestURL = try self.getURL(from: urlRequest)
|
||||
let requestURL = try getURL(from: urlRequest)
|
||||
|
||||
var requestPath = try self.getPath(from: requestURL)
|
||||
var requestPath = try getPath(from: requestURL)
|
||||
|
||||
if let headerFileName = self.getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
requestPath = requestPath.appending("/\(headerFileName)")
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
} catch let error {
|
||||
} catch {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, error)))
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
open func buildHeaders() -> [String: String] {
|
||||
var httpHeaders: [String: String] = [:]
|
||||
for (key, value) in self.headers {
|
||||
for (key, value) in headers {
|
||||
httpHeaders[key] = value
|
||||
}
|
||||
for (key, value) in PetstoreClientAPI.customHeaders {
|
||||
@@ -390,13 +390,13 @@ private class SessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
|
||||
|
||||
public enum HTTPMethod: String {
|
||||
case options = "OPTIONS"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case connect = "CONNECT"
|
||||
}
|
||||
|
||||
@@ -499,7 +499,7 @@ private class FormDataEncoding: ParameterEncoding {
|
||||
|
||||
let fileData = try Data(contentsOf: fileURL)
|
||||
|
||||
let mimetype = self.contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
let mimetype = contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
|
||||
let fileName = fileURL.lastPathComponent
|
||||
|
||||
|
||||
@@ -8,13 +8,14 @@ let package = Package(
|
||||
.iOS(.v9),
|
||||
.macOS(.v10_11),
|
||||
.tvOS(.v9),
|
||||
.watchOS(.v3)
|
||||
.watchOS(.v3),
|
||||
],
|
||||
products: [
|
||||
// Products define the executables and libraries produced by a package, and make them visible to other packages.
|
||||
.library(
|
||||
name: "PetstoreClient",
|
||||
targets: ["PetstoreClient"])
|
||||
targets: ["PetstoreClient"]
|
||||
),
|
||||
],
|
||||
dependencies: [
|
||||
// Dependencies declare other packages that this package depends on.
|
||||
@@ -26,6 +27,6 @@ let package = Package(
|
||||
name: "PetstoreClient",
|
||||
dependencies: [],
|
||||
path: "PetstoreClient/Classes"
|
||||
)
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -364,7 +364,7 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
@@ -447,7 +447,7 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
|
||||
@@ -8,7 +8,7 @@ import Foundation
|
||||
|
||||
public struct APIHelper {
|
||||
public static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
|
||||
let destination = source.reduce(into: [String: Any]()) { (result, item) in
|
||||
let destination = source.reduce(into: [String: Any]()) { result, item in
|
||||
if let value = item.value {
|
||||
result[item.key] = value
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public struct APIHelper {
|
||||
}
|
||||
|
||||
public static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
|
||||
return source.reduce(into: [String: String]()) { (result, item) in
|
||||
return source.reduce(into: [String: String]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
result[item.key] = collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
} else if let value: Any = item.value {
|
||||
@@ -35,27 +35,27 @@ public struct APIHelper {
|
||||
return nil
|
||||
}
|
||||
|
||||
return source.reduce(into: [String: Any](), { (result, item) in
|
||||
return source.reduce(into: [String: Any]()) { result, item in
|
||||
switch item.value {
|
||||
case let x as Bool:
|
||||
result[item.key] = x.description
|
||||
default:
|
||||
result[item.key] = item.value
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public static func mapValueToPathItem(_ source: Any) -> Any {
|
||||
if let collection = source as? [Any?] {
|
||||
return collection.filter { $0 != nil }.map {"\($0!)"}.joined(separator: ",")
|
||||
return collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
public static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
|
||||
let destination = source.filter({ $0.value != nil}).reduce(into: [URLQueryItem]()) { (result, item) in
|
||||
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
collection.filter { $0 != nil }.map {"\($0!)"}.forEach { value in
|
||||
collection.filter { $0 != nil }.map { "\($0!)" }.forEach { value in
|
||||
result.append(URLQueryItem(name: item.key, value: value))
|
||||
}
|
||||
} else if let value = item.value {
|
||||
|
||||
@@ -51,7 +51,7 @@ open class RequestBuilder<T> {
|
||||
}
|
||||
|
||||
open func addCredential() -> Self {
|
||||
self.credential = PetstoreClientAPI.credential
|
||||
credential = PetstoreClientAPI.credential
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,41 +8,42 @@
|
||||
import Foundation
|
||||
|
||||
open class CodableHelper {
|
||||
|
||||
private static var customDateFormatter: DateFormatter?
|
||||
private static var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter()
|
||||
|
||||
private static var customJSONDecoder: JSONDecoder?
|
||||
private static var defaultJSONDecoder: JSONDecoder = {
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
return decoder
|
||||
}()
|
||||
|
||||
private static var customJSONEncoder: JSONEncoder?
|
||||
private static var defaultJSONEncoder: JSONEncoder = {
|
||||
let encoder = JSONEncoder()
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
return encoder
|
||||
}()
|
||||
|
||||
public static var dateFormatter: DateFormatter {
|
||||
get { return self.customDateFormatter ?? self.defaultDateFormatter }
|
||||
set { self.customDateFormatter = newValue }
|
||||
get { return customDateFormatter ?? defaultDateFormatter }
|
||||
set { customDateFormatter = newValue }
|
||||
}
|
||||
public static var jsonDecoder: JSONDecoder {
|
||||
get { return self.customJSONDecoder ?? self.defaultJSONDecoder }
|
||||
set { self.customJSONDecoder = newValue }
|
||||
get { return customJSONDecoder ?? defaultJSONDecoder }
|
||||
set { customJSONDecoder = newValue }
|
||||
}
|
||||
public static var jsonEncoder: JSONEncoder {
|
||||
get { return self.customJSONEncoder ?? self.defaultJSONEncoder }
|
||||
set { self.customJSONEncoder = newValue }
|
||||
get { return customJSONEncoder ?? defaultJSONEncoder }
|
||||
set { customJSONEncoder = newValue }
|
||||
}
|
||||
|
||||
open class func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable {
|
||||
return Swift.Result { try self.jsonDecoder.decode(type, from: data) }
|
||||
return Swift.Result { try jsonDecoder.decode(type, from: data) }
|
||||
}
|
||||
|
||||
open class func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable {
|
||||
return Swift.Result { try self.jsonEncoder.encode(value) }
|
||||
return Swift.Result { try jsonEncoder.encode(value) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,8 @@
|
||||
import Foundation
|
||||
|
||||
open class Configuration {
|
||||
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
|
||||
|
||||
}
|
||||
|
||||
@@ -7,35 +7,35 @@
|
||||
import Foundation
|
||||
|
||||
extension Bool: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Float: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int32: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int32) }
|
||||
}
|
||||
|
||||
extension Int64: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int64) }
|
||||
}
|
||||
|
||||
extension Double: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension String: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension RawRepresentable where RawValue: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self.rawValue as Any }
|
||||
func encodeToJSON() -> Any { self.rawValue as Any }
|
||||
}
|
||||
|
||||
private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
@@ -48,7 +48,7 @@ private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
|
||||
extension Array: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.map(encodeIfPossible)
|
||||
self.map(encodeIfPossible)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,32 +64,32 @@ extension Dictionary: JSONEncodable {
|
||||
|
||||
extension Data: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
}
|
||||
}
|
||||
|
||||
extension Date: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return CodableHelper.dateFormatter.string(from: self) as Any
|
||||
CodableHelper.dateFormatter.string(from: self) as Any
|
||||
}
|
||||
}
|
||||
|
||||
extension URL: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
extension UUID: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.uuidString
|
||||
uuidString
|
||||
}
|
||||
}
|
||||
|
||||
extension String: CodingKey {
|
||||
|
||||
public var stringValue: String {
|
||||
return self
|
||||
self
|
||||
}
|
||||
|
||||
public init?(stringValue: String) {
|
||||
@@ -97,11 +97,11 @@ extension String: CodingKey {
|
||||
}
|
||||
|
||||
public var intValue: Int? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
public init?(intValue: Int) {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
}
|
||||
@@ -174,6 +174,6 @@ extension KeyedDecodingContainerProtocol {
|
||||
|
||||
extension HTTPURLResponse {
|
||||
var isStatusCodeSuccessful: Bool {
|
||||
return Array(200 ..< 300).contains(statusCode)
|
||||
Array(200 ..< 300).contains(statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,11 @@ import MobileCoreServices
|
||||
|
||||
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
|
||||
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionRequestBuilder<T>.self
|
||||
URLSessionRequestBuilder<T>.self
|
||||
}
|
||||
|
||||
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionDecodableRequestBuilder<T>.self
|
||||
URLSessionDecodableRequestBuilder<T>.self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
the file extension). Return the desired Content-Type otherwise.
|
||||
*/
|
||||
open func contentTypeForFormPart(fileURL: URL) -> String? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -209,11 +209,11 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
let fileManager = FileManager.default
|
||||
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
|
||||
let requestURL = try self.getURL(from: urlRequest)
|
||||
let requestURL = try getURL(from: urlRequest)
|
||||
|
||||
var requestPath = try self.getPath(from: requestURL)
|
||||
var requestPath = try getPath(from: requestURL)
|
||||
|
||||
if let headerFileName = self.getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
requestPath = requestPath.appending("/\(headerFileName)")
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
} catch let error {
|
||||
} catch {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, error)))
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
open func buildHeaders() -> [String: String] {
|
||||
var httpHeaders: [String: String] = [:]
|
||||
for (key, value) in self.headers {
|
||||
for (key, value) in headers {
|
||||
httpHeaders[key] = value
|
||||
}
|
||||
for (key, value) in PetstoreClientAPI.customHeaders {
|
||||
@@ -390,13 +390,13 @@ private class SessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
|
||||
|
||||
public enum HTTPMethod: String {
|
||||
case options = "OPTIONS"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case connect = "CONNECT"
|
||||
}
|
||||
|
||||
@@ -499,7 +499,7 @@ private class FormDataEncoding: ParameterEncoding {
|
||||
|
||||
let fileData = try Data(contentsOf: fileURL)
|
||||
|
||||
let mimetype = self.contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
let mimetype = contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
|
||||
let fileName = fileURL.lastPathComponent
|
||||
|
||||
|
||||
@@ -8,13 +8,14 @@ let package = Package(
|
||||
.iOS(.v9),
|
||||
.macOS(.v10_11),
|
||||
.tvOS(.v9),
|
||||
.watchOS(.v3)
|
||||
.watchOS(.v3),
|
||||
],
|
||||
products: [
|
||||
// Products define the executables and libraries produced by a package, and make them visible to other packages.
|
||||
.library(
|
||||
name: "PetstoreClient",
|
||||
targets: ["PetstoreClient"])
|
||||
targets: ["PetstoreClient"]
|
||||
),
|
||||
],
|
||||
dependencies: [
|
||||
// Dependencies declare other packages that this package depends on.
|
||||
@@ -26,6 +27,6 @@ let package = Package(
|
||||
name: "PetstoreClient",
|
||||
dependencies: [],
|
||||
path: "PetstoreClient/Classes"
|
||||
)
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -364,7 +364,7 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
@@ -447,7 +447,7 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
|
||||
@@ -8,7 +8,7 @@ import Foundation
|
||||
|
||||
public struct APIHelper {
|
||||
public static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
|
||||
let destination = source.reduce(into: [String: Any]()) { (result, item) in
|
||||
let destination = source.reduce(into: [String: Any]()) { result, item in
|
||||
if let value = item.value {
|
||||
result[item.key] = value
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public struct APIHelper {
|
||||
}
|
||||
|
||||
public static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
|
||||
return source.reduce(into: [String: String]()) { (result, item) in
|
||||
return source.reduce(into: [String: String]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
result[item.key] = collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
} else if let value: Any = item.value {
|
||||
@@ -35,27 +35,27 @@ public struct APIHelper {
|
||||
return nil
|
||||
}
|
||||
|
||||
return source.reduce(into: [String: Any](), { (result, item) in
|
||||
return source.reduce(into: [String: Any]()) { result, item in
|
||||
switch item.value {
|
||||
case let x as Bool:
|
||||
result[item.key] = x.description
|
||||
default:
|
||||
result[item.key] = item.value
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public static func mapValueToPathItem(_ source: Any) -> Any {
|
||||
if let collection = source as? [Any?] {
|
||||
return collection.filter { $0 != nil }.map {"\($0!)"}.joined(separator: ",")
|
||||
return collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
public static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
|
||||
let destination = source.filter({ $0.value != nil}).reduce(into: [URLQueryItem]()) { (result, item) in
|
||||
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
collection.filter { $0 != nil }.map {"\($0!)"}.forEach { value in
|
||||
collection.filter { $0 != nil }.map { "\($0!)" }.forEach { value in
|
||||
result.append(URLQueryItem(name: item.key, value: value))
|
||||
}
|
||||
} else if let value = item.value {
|
||||
|
||||
@@ -51,7 +51,7 @@ open class RequestBuilder<T> {
|
||||
}
|
||||
|
||||
open func addCredential() -> Self {
|
||||
self.credential = PetstoreClientAPI.credential
|
||||
credential = PetstoreClientAPI.credential
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,41 +8,42 @@
|
||||
import Foundation
|
||||
|
||||
open class CodableHelper {
|
||||
|
||||
private static var customDateFormatter: DateFormatter?
|
||||
private static var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter()
|
||||
|
||||
private static var customJSONDecoder: JSONDecoder?
|
||||
private static var defaultJSONDecoder: JSONDecoder = {
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
return decoder
|
||||
}()
|
||||
|
||||
private static var customJSONEncoder: JSONEncoder?
|
||||
private static var defaultJSONEncoder: JSONEncoder = {
|
||||
let encoder = JSONEncoder()
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
return encoder
|
||||
}()
|
||||
|
||||
public static var dateFormatter: DateFormatter {
|
||||
get { return self.customDateFormatter ?? self.defaultDateFormatter }
|
||||
set { self.customDateFormatter = newValue }
|
||||
get { return customDateFormatter ?? defaultDateFormatter }
|
||||
set { customDateFormatter = newValue }
|
||||
}
|
||||
public static var jsonDecoder: JSONDecoder {
|
||||
get { return self.customJSONDecoder ?? self.defaultJSONDecoder }
|
||||
set { self.customJSONDecoder = newValue }
|
||||
get { return customJSONDecoder ?? defaultJSONDecoder }
|
||||
set { customJSONDecoder = newValue }
|
||||
}
|
||||
public static var jsonEncoder: JSONEncoder {
|
||||
get { return self.customJSONEncoder ?? self.defaultJSONEncoder }
|
||||
set { self.customJSONEncoder = newValue }
|
||||
get { return customJSONEncoder ?? defaultJSONEncoder }
|
||||
set { customJSONEncoder = newValue }
|
||||
}
|
||||
|
||||
open class func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable {
|
||||
return Swift.Result { try self.jsonDecoder.decode(type, from: data) }
|
||||
return Swift.Result { try jsonDecoder.decode(type, from: data) }
|
||||
}
|
||||
|
||||
open class func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable {
|
||||
return Swift.Result { try self.jsonEncoder.encode(value) }
|
||||
return Swift.Result { try jsonEncoder.encode(value) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,8 @@
|
||||
import Foundation
|
||||
|
||||
open class Configuration {
|
||||
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
|
||||
|
||||
}
|
||||
|
||||
@@ -7,35 +7,35 @@
|
||||
import Foundation
|
||||
|
||||
extension Bool: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Float: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int32: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int32) }
|
||||
}
|
||||
|
||||
extension Int64: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int64) }
|
||||
}
|
||||
|
||||
extension Double: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension String: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension RawRepresentable where RawValue: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self.rawValue as Any }
|
||||
func encodeToJSON() -> Any { self.rawValue as Any }
|
||||
}
|
||||
|
||||
private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
@@ -48,7 +48,7 @@ private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
|
||||
extension Array: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.map(encodeIfPossible)
|
||||
self.map(encodeIfPossible)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,32 +64,32 @@ extension Dictionary: JSONEncodable {
|
||||
|
||||
extension Data: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
}
|
||||
}
|
||||
|
||||
extension Date: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return CodableHelper.dateFormatter.string(from: self) as Any
|
||||
CodableHelper.dateFormatter.string(from: self) as Any
|
||||
}
|
||||
}
|
||||
|
||||
extension URL: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
extension UUID: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.uuidString
|
||||
uuidString
|
||||
}
|
||||
}
|
||||
|
||||
extension String: CodingKey {
|
||||
|
||||
public var stringValue: String {
|
||||
return self
|
||||
self
|
||||
}
|
||||
|
||||
public init?(stringValue: String) {
|
||||
@@ -97,11 +97,11 @@ extension String: CodingKey {
|
||||
}
|
||||
|
||||
public var intValue: Int? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
public init?(intValue: Int) {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
}
|
||||
@@ -174,6 +174,6 @@ extension KeyedDecodingContainerProtocol {
|
||||
|
||||
extension HTTPURLResponse {
|
||||
var isStatusCodeSuccessful: Bool {
|
||||
return Array(200 ..< 300).contains(statusCode)
|
||||
Array(200 ..< 300).contains(statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,11 @@ import MobileCoreServices
|
||||
|
||||
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
|
||||
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionRequestBuilder<T>.self
|
||||
URLSessionRequestBuilder<T>.self
|
||||
}
|
||||
|
||||
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionDecodableRequestBuilder<T>.self
|
||||
URLSessionDecodableRequestBuilder<T>.self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
the file extension). Return the desired Content-Type otherwise.
|
||||
*/
|
||||
open func contentTypeForFormPart(fileURL: URL) -> String? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -209,11 +209,11 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
let fileManager = FileManager.default
|
||||
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
|
||||
let requestURL = try self.getURL(from: urlRequest)
|
||||
let requestURL = try getURL(from: urlRequest)
|
||||
|
||||
var requestPath = try self.getPath(from: requestURL)
|
||||
var requestPath = try getPath(from: requestURL)
|
||||
|
||||
if let headerFileName = self.getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
requestPath = requestPath.appending("/\(headerFileName)")
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
} catch let error {
|
||||
} catch {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, error)))
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
open func buildHeaders() -> [String: String] {
|
||||
var httpHeaders: [String: String] = [:]
|
||||
for (key, value) in self.headers {
|
||||
for (key, value) in headers {
|
||||
httpHeaders[key] = value
|
||||
}
|
||||
for (key, value) in PetstoreClientAPI.customHeaders {
|
||||
@@ -390,13 +390,13 @@ private class SessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
|
||||
|
||||
public enum HTTPMethod: String {
|
||||
case options = "OPTIONS"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case connect = "CONNECT"
|
||||
}
|
||||
|
||||
@@ -499,7 +499,7 @@ private class FormDataEncoding: ParameterEncoding {
|
||||
|
||||
let fileData = try Data(contentsOf: fileURL)
|
||||
|
||||
let mimetype = self.contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
let mimetype = contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
|
||||
let fileName = fileURL.lastPathComponent
|
||||
|
||||
|
||||
@@ -8,17 +8,18 @@ let package = Package(
|
||||
.iOS(.v9),
|
||||
.macOS(.v10_11),
|
||||
.tvOS(.v9),
|
||||
.watchOS(.v3)
|
||||
.watchOS(.v3),
|
||||
],
|
||||
products: [
|
||||
// Products define the executables and libraries produced by a package, and make them visible to other packages.
|
||||
.library(
|
||||
name: "PetstoreClient",
|
||||
targets: ["PetstoreClient"])
|
||||
targets: ["PetstoreClient"]
|
||||
),
|
||||
],
|
||||
dependencies: [
|
||||
// Dependencies declare other packages that this package depends on.
|
||||
.package(url: "https://github.com/ReactiveX/RxSwift.git", from: "5.0.0")
|
||||
.package(url: "https://github.com/ReactiveX/RxSwift.git", from: "5.0.0"),
|
||||
],
|
||||
targets: [
|
||||
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
|
||||
@@ -27,6 +28,6 @@ let package = Package(
|
||||
name: "PetstoreClient",
|
||||
dependencies: ["RxSwift"],
|
||||
path: "PetstoreClient/Classes"
|
||||
)
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -408,7 +408,7 @@
|
||||
);
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
@@ -495,7 +495,7 @@
|
||||
);
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
|
||||
@@ -8,7 +8,7 @@ import Foundation
|
||||
|
||||
public struct APIHelper {
|
||||
public static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
|
||||
let destination = source.reduce(into: [String: Any]()) { (result, item) in
|
||||
let destination = source.reduce(into: [String: Any]()) { result, item in
|
||||
if let value = item.value {
|
||||
result[item.key] = value
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public struct APIHelper {
|
||||
}
|
||||
|
||||
public static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
|
||||
return source.reduce(into: [String: String]()) { (result, item) in
|
||||
return source.reduce(into: [String: String]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
result[item.key] = collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
} else if let value: Any = item.value {
|
||||
@@ -35,27 +35,27 @@ public struct APIHelper {
|
||||
return nil
|
||||
}
|
||||
|
||||
return source.reduce(into: [String: Any](), { (result, item) in
|
||||
return source.reduce(into: [String: Any]()) { result, item in
|
||||
switch item.value {
|
||||
case let x as Bool:
|
||||
result[item.key] = x.description
|
||||
default:
|
||||
result[item.key] = item.value
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public static func mapValueToPathItem(_ source: Any) -> Any {
|
||||
if let collection = source as? [Any?] {
|
||||
return collection.filter { $0 != nil }.map {"\($0!)"}.joined(separator: ",")
|
||||
return collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
public static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
|
||||
let destination = source.filter({ $0.value != nil}).reduce(into: [URLQueryItem]()) { (result, item) in
|
||||
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
collection.filter { $0 != nil }.map {"\($0!)"}.forEach { value in
|
||||
collection.filter { $0 != nil }.map { "\($0!)" }.forEach { value in
|
||||
result.append(URLQueryItem(name: item.key, value: value))
|
||||
}
|
||||
} else if let value = item.value {
|
||||
|
||||
@@ -51,7 +51,7 @@ open class RequestBuilder<T> {
|
||||
}
|
||||
|
||||
open func addCredential() -> Self {
|
||||
self.credential = PetstoreClientAPI.credential
|
||||
credential = PetstoreClientAPI.credential
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,41 +8,42 @@
|
||||
import Foundation
|
||||
|
||||
open class CodableHelper {
|
||||
|
||||
private static var customDateFormatter: DateFormatter?
|
||||
private static var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter()
|
||||
|
||||
private static var customJSONDecoder: JSONDecoder?
|
||||
private static var defaultJSONDecoder: JSONDecoder = {
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
return decoder
|
||||
}()
|
||||
|
||||
private static var customJSONEncoder: JSONEncoder?
|
||||
private static var defaultJSONEncoder: JSONEncoder = {
|
||||
let encoder = JSONEncoder()
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
return encoder
|
||||
}()
|
||||
|
||||
public static var dateFormatter: DateFormatter {
|
||||
get { return self.customDateFormatter ?? self.defaultDateFormatter }
|
||||
set { self.customDateFormatter = newValue }
|
||||
get { return customDateFormatter ?? defaultDateFormatter }
|
||||
set { customDateFormatter = newValue }
|
||||
}
|
||||
public static var jsonDecoder: JSONDecoder {
|
||||
get { return self.customJSONDecoder ?? self.defaultJSONDecoder }
|
||||
set { self.customJSONDecoder = newValue }
|
||||
get { return customJSONDecoder ?? defaultJSONDecoder }
|
||||
set { customJSONDecoder = newValue }
|
||||
}
|
||||
public static var jsonEncoder: JSONEncoder {
|
||||
get { return self.customJSONEncoder ?? self.defaultJSONEncoder }
|
||||
set { self.customJSONEncoder = newValue }
|
||||
get { return customJSONEncoder ?? defaultJSONEncoder }
|
||||
set { customJSONEncoder = newValue }
|
||||
}
|
||||
|
||||
open class func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable {
|
||||
return Swift.Result { try self.jsonDecoder.decode(type, from: data) }
|
||||
return Swift.Result { try jsonDecoder.decode(type, from: data) }
|
||||
}
|
||||
|
||||
open class func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable {
|
||||
return Swift.Result { try self.jsonEncoder.encode(value) }
|
||||
return Swift.Result { try jsonEncoder.encode(value) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,8 @@
|
||||
import Foundation
|
||||
|
||||
open class Configuration {
|
||||
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
|
||||
|
||||
}
|
||||
|
||||
@@ -7,35 +7,35 @@
|
||||
import Foundation
|
||||
|
||||
extension Bool: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Float: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int32: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int32) }
|
||||
}
|
||||
|
||||
extension Int64: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int64) }
|
||||
}
|
||||
|
||||
extension Double: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension String: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension RawRepresentable where RawValue: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self.rawValue as Any }
|
||||
func encodeToJSON() -> Any { self.rawValue as Any }
|
||||
}
|
||||
|
||||
private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
@@ -48,7 +48,7 @@ private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
|
||||
extension Array: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.map(encodeIfPossible)
|
||||
self.map(encodeIfPossible)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,32 +64,32 @@ extension Dictionary: JSONEncodable {
|
||||
|
||||
extension Data: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
}
|
||||
}
|
||||
|
||||
extension Date: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return CodableHelper.dateFormatter.string(from: self) as Any
|
||||
CodableHelper.dateFormatter.string(from: self) as Any
|
||||
}
|
||||
}
|
||||
|
||||
extension URL: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
extension UUID: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.uuidString
|
||||
uuidString
|
||||
}
|
||||
}
|
||||
|
||||
extension String: CodingKey {
|
||||
|
||||
public var stringValue: String {
|
||||
return self
|
||||
self
|
||||
}
|
||||
|
||||
public init?(stringValue: String) {
|
||||
@@ -97,11 +97,11 @@ extension String: CodingKey {
|
||||
}
|
||||
|
||||
public var intValue: Int? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
public init?(intValue: Int) {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
}
|
||||
@@ -174,6 +174,6 @@ extension KeyedDecodingContainerProtocol {
|
||||
|
||||
extension HTTPURLResponse {
|
||||
var isStatusCodeSuccessful: Bool {
|
||||
return Array(200 ..< 300).contains(statusCode)
|
||||
Array(200 ..< 300).contains(statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,11 @@ import MobileCoreServices
|
||||
|
||||
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
|
||||
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionRequestBuilder<T>.self
|
||||
URLSessionRequestBuilder<T>.self
|
||||
}
|
||||
|
||||
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionDecodableRequestBuilder<T>.self
|
||||
URLSessionDecodableRequestBuilder<T>.self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
the file extension). Return the desired Content-Type otherwise.
|
||||
*/
|
||||
open func contentTypeForFormPart(fileURL: URL) -> String? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -209,11 +209,11 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
let fileManager = FileManager.default
|
||||
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
|
||||
let requestURL = try self.getURL(from: urlRequest)
|
||||
let requestURL = try getURL(from: urlRequest)
|
||||
|
||||
var requestPath = try self.getPath(from: requestURL)
|
||||
var requestPath = try getPath(from: requestURL)
|
||||
|
||||
if let headerFileName = self.getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
requestPath = requestPath.appending("/\(headerFileName)")
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
} catch let error {
|
||||
} catch {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, error)))
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
open func buildHeaders() -> [String: String] {
|
||||
var httpHeaders: [String: String] = [:]
|
||||
for (key, value) in self.headers {
|
||||
for (key, value) in headers {
|
||||
httpHeaders[key] = value
|
||||
}
|
||||
for (key, value) in PetstoreClientAPI.customHeaders {
|
||||
@@ -390,13 +390,13 @@ private class SessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
|
||||
|
||||
public enum HTTPMethod: String {
|
||||
case options = "OPTIONS"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case connect = "CONNECT"
|
||||
}
|
||||
|
||||
@@ -499,7 +499,7 @@ private class FormDataEncoding: ParameterEncoding {
|
||||
|
||||
let fileData = try Data(contentsOf: fileURL)
|
||||
|
||||
let mimetype = self.contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
let mimetype = contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
|
||||
let fileName = fileURL.lastPathComponent
|
||||
|
||||
|
||||
@@ -8,13 +8,14 @@ let package = Package(
|
||||
.iOS(.v9),
|
||||
.macOS(.v10_11),
|
||||
.tvOS(.v9),
|
||||
.watchOS(.v3)
|
||||
.watchOS(.v3),
|
||||
],
|
||||
products: [
|
||||
// Products define the executables and libraries produced by a package, and make them visible to other packages.
|
||||
.library(
|
||||
name: "PetstoreClient",
|
||||
targets: ["PetstoreClient"])
|
||||
targets: ["PetstoreClient"]
|
||||
),
|
||||
],
|
||||
dependencies: [
|
||||
// Dependencies declare other packages that this package depends on.
|
||||
@@ -26,6 +27,6 @@ let package = Package(
|
||||
name: "PetstoreClient",
|
||||
dependencies: [],
|
||||
path: "PetstoreClient/Classes"
|
||||
)
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -364,7 +364,7 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
@@ -447,7 +447,7 @@
|
||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
||||
INFOPLIST_FILE = Info.plist;
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
|
||||
@@ -8,7 +8,7 @@ import Foundation
|
||||
|
||||
public struct APIHelper {
|
||||
public static func rejectNil(_ source: [String: Any?]) -> [String: Any]? {
|
||||
let destination = source.reduce(into: [String: Any]()) { (result, item) in
|
||||
let destination = source.reduce(into: [String: Any]()) { result, item in
|
||||
if let value = item.value {
|
||||
result[item.key] = value
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public struct APIHelper {
|
||||
}
|
||||
|
||||
public static func rejectNilHeaders(_ source: [String: Any?]) -> [String: String] {
|
||||
return source.reduce(into: [String: String]()) { (result, item) in
|
||||
return source.reduce(into: [String: String]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
result[item.key] = collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
} else if let value: Any = item.value {
|
||||
@@ -35,27 +35,27 @@ public struct APIHelper {
|
||||
return nil
|
||||
}
|
||||
|
||||
return source.reduce(into: [String: Any](), { (result, item) in
|
||||
return source.reduce(into: [String: Any]()) { result, item in
|
||||
switch item.value {
|
||||
case let x as Bool:
|
||||
result[item.key] = x.description
|
||||
default:
|
||||
result[item.key] = item.value
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public static func mapValueToPathItem(_ source: Any) -> Any {
|
||||
if let collection = source as? [Any?] {
|
||||
return collection.filter { $0 != nil }.map {"\($0!)"}.joined(separator: ",")
|
||||
return collection.filter { $0 != nil }.map { "\($0!)" }.joined(separator: ",")
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
public static func mapValuesToQueryItems(_ source: [String: Any?]) -> [URLQueryItem]? {
|
||||
let destination = source.filter({ $0.value != nil}).reduce(into: [URLQueryItem]()) { (result, item) in
|
||||
let destination = source.filter { $0.value != nil }.reduce(into: [URLQueryItem]()) { result, item in
|
||||
if let collection = item.value as? [Any?] {
|
||||
collection.filter { $0 != nil }.map {"\($0!)"}.forEach { value in
|
||||
collection.filter { $0 != nil }.map { "\($0!)" }.forEach { value in
|
||||
result.append(URLQueryItem(name: item.key, value: value))
|
||||
}
|
||||
} else if let value = item.value {
|
||||
|
||||
@@ -51,7 +51,7 @@ open class RequestBuilder<T> {
|
||||
}
|
||||
|
||||
open func addCredential() -> Self {
|
||||
self.credential = PetstoreClientAPI.credential
|
||||
credential = PetstoreClientAPI.credential
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,41 +8,42 @@
|
||||
import Foundation
|
||||
|
||||
open class CodableHelper {
|
||||
|
||||
private static var customDateFormatter: DateFormatter?
|
||||
private static var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter()
|
||||
|
||||
private static var customJSONDecoder: JSONDecoder?
|
||||
private static var defaultJSONDecoder: JSONDecoder = {
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
return decoder
|
||||
}()
|
||||
|
||||
private static var customJSONEncoder: JSONEncoder?
|
||||
private static var defaultJSONEncoder: JSONEncoder = {
|
||||
let encoder = JSONEncoder()
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .formatted(CodableHelper.dateFormatter)
|
||||
encoder.outputFormatting = .prettyPrinted
|
||||
return encoder
|
||||
}()
|
||||
|
||||
public static var dateFormatter: DateFormatter {
|
||||
get { return self.customDateFormatter ?? self.defaultDateFormatter }
|
||||
set { self.customDateFormatter = newValue }
|
||||
get { return customDateFormatter ?? defaultDateFormatter }
|
||||
set { customDateFormatter = newValue }
|
||||
}
|
||||
public static var jsonDecoder: JSONDecoder {
|
||||
get { return self.customJSONDecoder ?? self.defaultJSONDecoder }
|
||||
set { self.customJSONDecoder = newValue }
|
||||
get { return customJSONDecoder ?? defaultJSONDecoder }
|
||||
set { customJSONDecoder = newValue }
|
||||
}
|
||||
public static var jsonEncoder: JSONEncoder {
|
||||
get { return self.customJSONEncoder ?? self.defaultJSONEncoder }
|
||||
set { self.customJSONEncoder = newValue }
|
||||
get { return customJSONEncoder ?? defaultJSONEncoder }
|
||||
set { customJSONEncoder = newValue }
|
||||
}
|
||||
|
||||
open class func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable {
|
||||
return Swift.Result { try self.jsonDecoder.decode(type, from: data) }
|
||||
return Swift.Result { try jsonDecoder.decode(type, from: data) }
|
||||
}
|
||||
|
||||
open class func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable {
|
||||
return Swift.Result { try self.jsonEncoder.encode(value) }
|
||||
return Swift.Result { try jsonEncoder.encode(value) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,8 @@
|
||||
import Foundation
|
||||
|
||||
open class Configuration {
|
||||
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
// This value is used to configure the date formatter that is used to serialize dates into JSON format.
|
||||
// You must set it prior to encoding any dates, and it will only be read once.
|
||||
@available(*, unavailable, message: "To set a different date format, use CodableHelper.dateFormatter instead.")
|
||||
public static var dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
|
||||
|
||||
}
|
||||
|
||||
@@ -7,35 +7,35 @@
|
||||
import Foundation
|
||||
|
||||
extension Bool: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Float: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension Int32: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int32) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int32) }
|
||||
}
|
||||
|
||||
extension Int64: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return NSNumber(value: self as Int64) }
|
||||
func encodeToJSON() -> Any { NSNumber(value: self as Int64) }
|
||||
}
|
||||
|
||||
extension Double: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension String: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self as Any }
|
||||
func encodeToJSON() -> Any { self as Any }
|
||||
}
|
||||
|
||||
extension RawRepresentable where RawValue: JSONEncodable {
|
||||
func encodeToJSON() -> Any { return self.rawValue as Any }
|
||||
func encodeToJSON() -> Any { self.rawValue as Any }
|
||||
}
|
||||
|
||||
private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
@@ -48,7 +48,7 @@ private func encodeIfPossible<T>(_ object: T) -> Any {
|
||||
|
||||
extension Array: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.map(encodeIfPossible)
|
||||
self.map(encodeIfPossible)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,32 +64,32 @@ extension Dictionary: JSONEncodable {
|
||||
|
||||
extension Data: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
self.base64EncodedString(options: Data.Base64EncodingOptions())
|
||||
}
|
||||
}
|
||||
|
||||
extension Date: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return CodableHelper.dateFormatter.string(from: self) as Any
|
||||
CodableHelper.dateFormatter.string(from: self) as Any
|
||||
}
|
||||
}
|
||||
|
||||
extension URL: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
extension UUID: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return self.uuidString
|
||||
uuidString
|
||||
}
|
||||
}
|
||||
|
||||
extension String: CodingKey {
|
||||
|
||||
public var stringValue: String {
|
||||
return self
|
||||
self
|
||||
}
|
||||
|
||||
public init?(stringValue: String) {
|
||||
@@ -97,11 +97,11 @@ extension String: CodingKey {
|
||||
}
|
||||
|
||||
public var intValue: Int? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
public init?(intValue: Int) {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
}
|
||||
@@ -174,6 +174,6 @@ extension KeyedDecodingContainerProtocol {
|
||||
|
||||
extension HTTPURLResponse {
|
||||
var isStatusCodeSuccessful: Bool {
|
||||
return Array(200 ..< 300).contains(statusCode)
|
||||
Array(200 ..< 300).contains(statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,11 +11,11 @@ import MobileCoreServices
|
||||
|
||||
class URLSessionRequestBuilderFactory: RequestBuilderFactory {
|
||||
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionRequestBuilder<T>.self
|
||||
URLSessionRequestBuilder<T>.self
|
||||
}
|
||||
|
||||
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type {
|
||||
return URLSessionDecodableRequestBuilder<T>.self
|
||||
URLSessionDecodableRequestBuilder<T>.self
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
the file extension). Return the desired Content-Type otherwise.
|
||||
*/
|
||||
open func contentTypeForFormPart(fileURL: URL) -> String? {
|
||||
return nil
|
||||
nil
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -209,11 +209,11 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
let fileManager = FileManager.default
|
||||
let documentsDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
|
||||
let requestURL = try self.getURL(from: urlRequest)
|
||||
let requestURL = try getURL(from: urlRequest)
|
||||
|
||||
var requestPath = try self.getPath(from: requestURL)
|
||||
var requestPath = try getPath(from: requestURL)
|
||||
|
||||
if let headerFileName = self.getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
if let headerFileName = getFileName(fromContentDisposition: httpResponse.allHeaderFields["Content-Disposition"] as? String) {
|
||||
requestPath = requestPath.appending("/\(headerFileName)")
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
} catch let requestParserError as DownloadException {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, requestParserError)))
|
||||
} catch let error {
|
||||
} catch {
|
||||
completion(.failure(ErrorResponse.error(400, data, response, error)))
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
open func buildHeaders() -> [String: String] {
|
||||
var httpHeaders: [String: String] = [:]
|
||||
for (key, value) in self.headers {
|
||||
for (key, value) in headers {
|
||||
httpHeaders[key] = value
|
||||
}
|
||||
for (key, value) in PetstoreClientAPI.customHeaders {
|
||||
@@ -390,13 +390,13 @@ private class SessionDelegate: NSObject, URLSessionDelegate, URLSessionDataDeleg
|
||||
|
||||
public enum HTTPMethod: String {
|
||||
case options = "OPTIONS"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case get = "GET"
|
||||
case head = "HEAD"
|
||||
case post = "POST"
|
||||
case put = "PUT"
|
||||
case patch = "PATCH"
|
||||
case delete = "DELETE"
|
||||
case trace = "TRACE"
|
||||
case connect = "CONNECT"
|
||||
}
|
||||
|
||||
@@ -499,7 +499,7 @@ private class FormDataEncoding: ParameterEncoding {
|
||||
|
||||
let fileData = try Data(contentsOf: fileURL)
|
||||
|
||||
let mimetype = self.contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
let mimetype = contentTypeForFormPart(fileURL) ?? mimeType(for: fileURL)
|
||||
|
||||
let fileName = fileURL.lastPathComponent
|
||||
|
||||
|
||||
Reference in New Issue
Block a user