[0.1.0] Create SLazeKit class
This commit is contained in:
parent
4447a0e437
commit
463240fbb4
|
@ -29,7 +29,6 @@
|
|||
OBJ_46 /* NSManagedObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_12 /* NSManagedObject.swift */; };
|
||||
OBJ_47 /* NSManagedObjectContext.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_13 /* NSManagedObjectContext.swift */; };
|
||||
OBJ_48 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_14 /* String.swift */; };
|
||||
OBJ_49 /* URLSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_15 /* URLSession.swift */; };
|
||||
OBJ_50 /* SLazeKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_16 /* SLazeKit.swift */; };
|
||||
OBJ_51 /* StringInitializable.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_17 /* StringInitializable.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
@ -56,7 +55,6 @@
|
|||
OBJ_12 /* NSManagedObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSManagedObject.swift; sourceTree = "<group>"; };
|
||||
OBJ_13 /* NSManagedObjectContext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSManagedObjectContext.swift; sourceTree = "<group>"; };
|
||||
OBJ_14 /* String.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = String.swift; sourceTree = "<group>"; };
|
||||
OBJ_15 /* URLSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSession.swift; sourceTree = "<group>"; };
|
||||
OBJ_16 /* SLazeKit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SLazeKit.swift; sourceTree = "<group>"; };
|
||||
OBJ_17 /* StringInitializable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringInitializable.swift; sourceTree = "<group>"; };
|
||||
OBJ_20 /* SLazeKitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SLazeKitTests.swift; sourceTree = "<group>"; };
|
||||
|
@ -92,7 +90,6 @@
|
|||
OBJ_12 /* NSManagedObject.swift */,
|
||||
OBJ_13 /* NSManagedObjectContext.swift */,
|
||||
OBJ_14 /* String.swift */,
|
||||
OBJ_15 /* URLSession.swift */,
|
||||
);
|
||||
path = Extensions;
|
||||
sourceTree = "<group>";
|
||||
|
@ -123,7 +120,7 @@
|
|||
name = Products;
|
||||
sourceTree = BUILT_PRODUCTS_DIR;
|
||||
};
|
||||
OBJ_5 /* */ = {
|
||||
OBJ_5 = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
OBJ_6 /* Package.swift */,
|
||||
|
@ -131,7 +128,6 @@
|
|||
OBJ_18 /* Tests */,
|
||||
OBJ_21 /* Products */,
|
||||
);
|
||||
name = "";
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
OBJ_7 /* Sources */ = {
|
||||
|
@ -219,7 +215,7 @@
|
|||
knownRegions = (
|
||||
en,
|
||||
);
|
||||
mainGroup = OBJ_5 /* */;
|
||||
mainGroup = OBJ_5;
|
||||
productRefGroup = OBJ_21 /* Products */;
|
||||
projectDirPath = "";
|
||||
projectRoot = "";
|
||||
|
@ -258,7 +254,6 @@
|
|||
OBJ_46 /* NSManagedObject.swift in Sources */,
|
||||
OBJ_47 /* NSManagedObjectContext.swift in Sources */,
|
||||
OBJ_48 /* String.swift in Sources */,
|
||||
OBJ_49 /* URLSession.swift in Sources */,
|
||||
OBJ_50 /* SLazeKit.swift in Sources */,
|
||||
OBJ_51 /* StringInitializable.swift in Sources */,
|
||||
);
|
||||
|
|
|
@ -2,62 +2,62 @@ import Foundation
|
|||
|
||||
extension Decodable {
|
||||
public static func request<T: Encodable>(path: String, method: HTTPMethod, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
|
||||
return SLazeKit.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public static func request<T: Encodable>(path: String, method: HTTPMethod, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
|
||||
return SLazeKit.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public static func request(path: String, method: HTTPMethod, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: method, queryItems: queryItems, handler: handler)
|
||||
return SLazeKit.networkTask(path: path, method: method, queryItems: queryItems, handler: handler)
|
||||
}
|
||||
|
||||
public static func get<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
|
||||
return SLazeKit.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public static func get<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
|
||||
return SLazeKit.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public static func get(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, queryItems: queryItems, handler: handler)
|
||||
return SLazeKit.networkTask(path: path, queryItems: queryItems, handler: handler)
|
||||
}
|
||||
|
||||
public static func post<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
|
||||
return SLazeKit.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public static func post<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
|
||||
return SLazeKit.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public static func post(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .POST, queryItems: queryItems, handler: handler)
|
||||
return SLazeKit.networkTask(path: path, method: .POST, queryItems: queryItems, handler: handler)
|
||||
}
|
||||
|
||||
public static func put<T: Encodable>(path: String, queryItems: [URLQueryItem]? = nil, body: T, handler: @escaping (_ response: HTTPURLResponse?, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
|
||||
return SLazeKit.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public static func put<T: Encodable>(path: String, queryItems: [URLQueryItem]? = nil, body: [T], handler: @escaping (_ response: HTTPURLResponse?, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
|
||||
return SLazeKit.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public static func put(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .PUT, queryItems: queryItems, handler: handler)
|
||||
return SLazeKit.networkTask(path: path, method: .PUT, queryItems: queryItems, handler: handler)
|
||||
}
|
||||
|
||||
public static func delete<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
|
||||
return SLazeKit.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public static func delete<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
|
||||
return SLazeKit.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public static func delete(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ result: Self?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .DELETE, queryItems: queryItems, handler: handler)
|
||||
return SLazeKit.networkTask(path: path, method: .DELETE, queryItems: queryItems, handler: handler)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,229 +0,0 @@
|
|||
import Foundation
|
||||
|
||||
public typealias HTTPMethod = String
|
||||
|
||||
extension HTTPMethod {
|
||||
public static var GET: String { return "GET" }
|
||||
public static var POST: String { return "POST" }
|
||||
public static var PUT: String { return "PUT" }
|
||||
public static var PATCH: String { return "PATCH" }
|
||||
public static var DELETE: String { return "DELETE" }
|
||||
public static var COPY: String { return "COPY" }
|
||||
public static var HEAD: String { return "HEAD" }
|
||||
public static var OPTIONS: String { return "OPTIONS" }
|
||||
public static var LINK: String { return "LINK" }
|
||||
public static var UNLINK: String { return "UNLINK" }
|
||||
public static var PURGE: String { return "PURGE" }
|
||||
public static var LOCK: String { return "LOCK" }
|
||||
public static var UNLOCK: String { return "UNLOCK" }
|
||||
public static var PROPFIND: String { return "PROPFIND" }
|
||||
public static var VIEW: String { return "VIEW" }
|
||||
}
|
||||
|
||||
//MARK: network tasks implementation
|
||||
extension URLSession {
|
||||
class func networkTask(request: URLRequest, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
let task = urlSession.dataTask(with: setup(request)) { (data, response, error) in
|
||||
handle(response as? HTTPURLResponse)
|
||||
handler(response as? HTTPURLResponse, error)
|
||||
}
|
||||
task.resume()
|
||||
return task
|
||||
}
|
||||
|
||||
class func networkTask<T: Decodable>(request: URLRequest, handler: @escaping (_ response: HTTPURLResponse?, _ result: T?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
let task = urlSession.dataTask(with: setup(request)) { (data, response, error) in
|
||||
handle(response as? HTTPURLResponse)
|
||||
if let data = data, error == nil {
|
||||
do {
|
||||
let object = try decoder.decode(T.self, from: data)
|
||||
if #available(iOS 10.0, *) {
|
||||
try synchronize(object)
|
||||
}
|
||||
handler(response as? HTTPURLResponse, object, nil)
|
||||
} catch {
|
||||
handler(response as? HTTPURLResponse, nil, error)
|
||||
}
|
||||
} else {
|
||||
handler(response as? HTTPURLResponse, nil, error)
|
||||
}
|
||||
}
|
||||
task.resume()
|
||||
return task
|
||||
}
|
||||
|
||||
class func networkTask<B: Encodable>(path: String, method: HTTPMethod? = nil, queryItems: [URLQueryItem]? = nil, body: B, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
guard let url = components(for: path, queryItems: queryItems)?.url else {
|
||||
handler(nil, NSError(domain: "Unable to get url from components", code: NSURLErrorBadURL, userInfo: nil))
|
||||
return nil
|
||||
}
|
||||
|
||||
var request = urlRequest(url, method: method)
|
||||
do {
|
||||
request.httpBody = try JSONEncoder().encode(body)
|
||||
} catch {
|
||||
handler(nil, error)
|
||||
return nil
|
||||
}
|
||||
|
||||
return networkTask(request: request, handler: handler)
|
||||
}
|
||||
|
||||
class func networkTask<T: Decodable, B: Encodable>(path: String, method: HTTPMethod? = nil, queryItems: [URLQueryItem]? = nil, body: B, handler: @escaping (_ response: HTTPURLResponse?, _ result: T?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
guard let url = components(for: path, queryItems: queryItems)?.url else {
|
||||
handler(nil, nil, NSError(domain: "Unable to get url from components", code: NSURLErrorBadURL, userInfo: nil))
|
||||
return nil
|
||||
}
|
||||
|
||||
var request = urlRequest(url, method: method)
|
||||
do {
|
||||
request.httpBody = try JSONEncoder().encode(body)
|
||||
} catch {
|
||||
handler(nil, nil, error)
|
||||
return nil
|
||||
}
|
||||
|
||||
return networkTask(request: request, handler: handler)
|
||||
}
|
||||
|
||||
class func networkTask<B: Encodable>(path: String, method: HTTPMethod? = nil, queryItems: [URLQueryItem]? = nil, body: [B], handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
guard let url = components(for: path, queryItems: queryItems)?.url else {
|
||||
handler(nil, NSError(domain: "Unable to get url from components", code: NSURLErrorBadURL, userInfo: nil))
|
||||
return nil
|
||||
}
|
||||
|
||||
var request = urlRequest(url, method: method)
|
||||
do {
|
||||
request.httpBody = try JSONEncoder().encode(body)
|
||||
} catch {
|
||||
handler(nil, error)
|
||||
return nil
|
||||
}
|
||||
|
||||
return networkTask(request: request, handler: handler)
|
||||
}
|
||||
|
||||
class func networkTask<T: Decodable, B: Encodable>(path: String, method: HTTPMethod? = nil, queryItems: [URLQueryItem]? = nil, body: [B], handler: @escaping (_ response: HTTPURLResponse?, _ result: T?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
guard let url = components(for: path, queryItems: queryItems)?.url else {
|
||||
handler(nil, nil, NSError(domain: "Unable to get url from components", code: NSURLErrorBadURL, userInfo: nil))
|
||||
return nil
|
||||
}
|
||||
|
||||
var request = urlRequest(url, method: method)
|
||||
do {
|
||||
request.httpBody = try JSONEncoder().encode(body)
|
||||
} catch {
|
||||
handler(nil, nil, error)
|
||||
return nil
|
||||
}
|
||||
|
||||
return networkTask(request: request, handler: handler)
|
||||
}
|
||||
|
||||
class func networkTask(path: String, method: HTTPMethod? = nil, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
guard let url = components(for: path, queryItems: queryItems)?.url else {
|
||||
handler(nil, NSError(domain: "Unable to get url from components", code: NSURLErrorBadURL, userInfo: nil))
|
||||
return nil
|
||||
}
|
||||
return networkTask(request: urlRequest(url, method: method), handler: handler)
|
||||
}
|
||||
|
||||
class func networkTask<T: Decodable>(path: String, method: HTTPMethod? = nil, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ result: T?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
guard let url = components(for: path, queryItems: queryItems)?.url else {
|
||||
handler(nil, nil, NSError(domain: "Unable to get url from components", code: NSURLErrorBadURL, userInfo: nil))
|
||||
return nil
|
||||
}
|
||||
return networkTask(request: urlRequest(url, method: method), handler: handler)
|
||||
}
|
||||
|
||||
private class func urlRequest(_ url: URL, method: HTTPMethod?) -> URLRequest {
|
||||
var request = URLRequest(url: url)
|
||||
if let httpMethod = method {
|
||||
request.httpMethod = httpMethod
|
||||
}
|
||||
return request
|
||||
}
|
||||
|
||||
private class func components(for path: String, queryItems: [URLQueryItem]?) -> URLComponents? {
|
||||
var urlComponents = URLComponents(string: (basePath ?? "") + path)
|
||||
urlComponents?.port = basePort
|
||||
urlComponents?.queryItems = queryItems
|
||||
return urlComponents
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: CoreData mapping support
|
||||
extension URLSession {
|
||||
@available(iOS 10.0, *)
|
||||
fileprivate class func synchronize(_ obj: Any) throws {
|
||||
if let array = obj as? [EntityMapping] {
|
||||
array.forEach({_ = try? $0.map()})
|
||||
} else {
|
||||
guard let mapper = obj as? EntityMapping else { return }
|
||||
_ = try mapper.map()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: Friendly methods
|
||||
extension URLSession {
|
||||
public class func request<T: Encodable>(path: String, method: HTTPMethod, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func request<T: Encodable>(path: String, method: HTTPMethod, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func request(path: String, method: HTTPMethod, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: method, queryItems: queryItems, handler: handler)
|
||||
}
|
||||
|
||||
public class func get<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func get<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func get(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, queryItems: queryItems, handler: handler)
|
||||
}
|
||||
|
||||
public class func post<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func post<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func post(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .POST, queryItems: queryItems, handler: handler)
|
||||
}
|
||||
|
||||
public class func put<T: Encodable>(path: String, queryItems: [URLQueryItem]? = nil, body: T, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func put<T: Encodable>(path: String, queryItems: [URLQueryItem]? = nil, body: [T], handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func put(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .PUT, queryItems: queryItems, handler: handler)
|
||||
}
|
||||
|
||||
public class func delete<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func delete<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func delete(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return URLSession.networkTask(path: path, method: .DELETE, queryItems: queryItems, handler: handler)
|
||||
}
|
||||
}
|
|
@ -2,37 +2,244 @@ import Foundation
|
|||
import CoreData
|
||||
|
||||
public typealias EntityMappingDecodable = EntityMapping & Decodable
|
||||
public typealias HTTPMethod = String
|
||||
|
||||
extension HTTPMethod {
|
||||
public static var GET: String { return "GET" }
|
||||
public static var POST: String { return "POST" }
|
||||
public static var PUT: String { return "PUT" }
|
||||
public static var PATCH: String { return "PATCH" }
|
||||
public static var DELETE: String { return "DELETE" }
|
||||
public static var COPY: String { return "COPY" }
|
||||
public static var HEAD: String { return "HEAD" }
|
||||
public static var OPTIONS: String { return "OPTIONS" }
|
||||
public static var LINK: String { return "LINK" }
|
||||
public static var UNLINK: String { return "UNLINK" }
|
||||
public static var PURGE: String { return "PURGE" }
|
||||
public static var LOCK: String { return "LOCK" }
|
||||
public static var UNLOCK: String { return "UNLOCK" }
|
||||
public static var PROPFIND: String { return "PROPFIND" }
|
||||
public static var VIEW: String { return "VIEW" }
|
||||
}
|
||||
|
||||
@available(iOS 10.0, *)
|
||||
extension EntityMapping {
|
||||
public static var persistentContainer: NSPersistentContainer? { return nil }
|
||||
}
|
||||
|
||||
//MARK: URLSession class variables
|
||||
extension URLSession {
|
||||
open class var basePath: String? {
|
||||
return nil
|
||||
class SLazeKit {
|
||||
open class var basePath: String? { return nil }
|
||||
open class var basePort: Int? { return nil }
|
||||
open class var decoder: JSONDecoder { return JSONDecoder() }
|
||||
open class var urlSession: URLSession { return URLSession.shared }
|
||||
|
||||
open class func setup(_ request: URLRequest) -> URLRequest { return request }
|
||||
open class func handle(_ response: HTTPURLResponse?) {}
|
||||
}
|
||||
|
||||
//MARK: network tasks implementation
|
||||
extension SLazeKit {
|
||||
class func networkTask(request: URLRequest, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
let task = urlSession.dataTask(with: setup(request)) { (data, response, error) in
|
||||
handle(response as? HTTPURLResponse)
|
||||
handler(response as? HTTPURLResponse, error)
|
||||
}
|
||||
task.resume()
|
||||
return task
|
||||
}
|
||||
|
||||
open class var basePort: Int? {
|
||||
return nil
|
||||
class func networkTask<T: Decodable>(request: URLRequest, handler: @escaping (_ response: HTTPURLResponse?, _ result: T?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
let task = urlSession.dataTask(with: setup(request)) { (data, response, error) in
|
||||
handle(response as? HTTPURLResponse)
|
||||
if let data = data, error == nil {
|
||||
do {
|
||||
let object = try decoder.decode(T.self, from: data)
|
||||
if #available(iOS 10.0, *) {
|
||||
try synchronize(object)
|
||||
}
|
||||
handler(response as? HTTPURLResponse, object, nil)
|
||||
} catch {
|
||||
handler(response as? HTTPURLResponse, nil, error)
|
||||
}
|
||||
} else {
|
||||
handler(response as? HTTPURLResponse, nil, error)
|
||||
}
|
||||
}
|
||||
task.resume()
|
||||
return task
|
||||
}
|
||||
|
||||
open class var token: String {
|
||||
return ""
|
||||
class func networkTask<B: Encodable>(path: String, method: HTTPMethod? = nil, queryItems: [URLQueryItem]? = nil, body: B, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
guard let url = components(for: path, queryItems: queryItems)?.url else {
|
||||
handler(nil, NSError(domain: "Unable to get url from components", code: NSURLErrorBadURL, userInfo: nil))
|
||||
return nil
|
||||
}
|
||||
|
||||
var request = urlRequest(url, method: method)
|
||||
do {
|
||||
request.httpBody = try JSONEncoder().encode(body)
|
||||
} catch {
|
||||
handler(nil, error)
|
||||
return nil
|
||||
}
|
||||
|
||||
return networkTask(request: request, handler: handler)
|
||||
}
|
||||
|
||||
open class var decoder: JSONDecoder {
|
||||
return JSONDecoder()
|
||||
class func networkTask<T: Decodable, B: Encodable>(path: String, method: HTTPMethod? = nil, queryItems: [URLQueryItem]? = nil, body: B, handler: @escaping (_ response: HTTPURLResponse?, _ result: T?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
guard let url = components(for: path, queryItems: queryItems)?.url else {
|
||||
handler(nil, nil, NSError(domain: "Unable to get url from components", code: NSURLErrorBadURL, userInfo: nil))
|
||||
return nil
|
||||
}
|
||||
|
||||
var request = urlRequest(url, method: method)
|
||||
do {
|
||||
request.httpBody = try JSONEncoder().encode(body)
|
||||
} catch {
|
||||
handler(nil, nil, error)
|
||||
return nil
|
||||
}
|
||||
|
||||
return networkTask(request: request, handler: handler)
|
||||
}
|
||||
|
||||
open class var urlSession: URLSession {
|
||||
return URLSession.shared
|
||||
class func networkTask<B: Encodable>(path: String, method: HTTPMethod? = nil, queryItems: [URLQueryItem]? = nil, body: [B], handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
guard let url = components(for: path, queryItems: queryItems)?.url else {
|
||||
handler(nil, NSError(domain: "Unable to get url from components", code: NSURLErrorBadURL, userInfo: nil))
|
||||
return nil
|
||||
}
|
||||
|
||||
var request = urlRequest(url, method: method)
|
||||
do {
|
||||
request.httpBody = try JSONEncoder().encode(body)
|
||||
} catch {
|
||||
handler(nil, error)
|
||||
return nil
|
||||
}
|
||||
|
||||
return networkTask(request: request, handler: handler)
|
||||
}
|
||||
|
||||
open class func setup(_ request: URLRequest) -> URLRequest {
|
||||
class func networkTask<T: Decodable, B: Encodable>(path: String, method: HTTPMethod? = nil, queryItems: [URLQueryItem]? = nil, body: [B], handler: @escaping (_ response: HTTPURLResponse?, _ result: T?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
guard let url = components(for: path, queryItems: queryItems)?.url else {
|
||||
handler(nil, nil, NSError(domain: "Unable to get url from components", code: NSURLErrorBadURL, userInfo: nil))
|
||||
return nil
|
||||
}
|
||||
|
||||
var request = urlRequest(url, method: method)
|
||||
do {
|
||||
request.httpBody = try JSONEncoder().encode(body)
|
||||
} catch {
|
||||
handler(nil, nil, error)
|
||||
return nil
|
||||
}
|
||||
|
||||
return networkTask(request: request, handler: handler)
|
||||
}
|
||||
|
||||
class func networkTask(path: String, method: HTTPMethod? = nil, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
guard let url = components(for: path, queryItems: queryItems)?.url else {
|
||||
handler(nil, NSError(domain: "Unable to get url from components", code: NSURLErrorBadURL, userInfo: nil))
|
||||
return nil
|
||||
}
|
||||
return networkTask(request: urlRequest(url, method: method), handler: handler)
|
||||
}
|
||||
|
||||
class func networkTask<T: Decodable>(path: String, method: HTTPMethod? = nil, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ result: T?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
guard let url = components(for: path, queryItems: queryItems)?.url else {
|
||||
handler(nil, nil, NSError(domain: "Unable to get url from components", code: NSURLErrorBadURL, userInfo: nil))
|
||||
return nil
|
||||
}
|
||||
return networkTask(request: urlRequest(url, method: method), handler: handler)
|
||||
}
|
||||
|
||||
private class func urlRequest(_ url: URL, method: HTTPMethod?) -> URLRequest {
|
||||
var request = URLRequest(url: url)
|
||||
if let httpMethod = method {
|
||||
request.httpMethod = httpMethod
|
||||
}
|
||||
return request
|
||||
}
|
||||
|
||||
open class func handle(_ response: HTTPURLResponse?) {}
|
||||
private class func components(for path: String, queryItems: [URLQueryItem]?) -> URLComponents? {
|
||||
var urlComponents = URLComponents(string: (basePath ?? "") + path)
|
||||
urlComponents?.port = basePort
|
||||
urlComponents?.queryItems = queryItems
|
||||
return urlComponents
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: CoreData mapping support
|
||||
extension SLazeKit {
|
||||
@available(iOS 10.0, *)
|
||||
fileprivate class func synchronize(_ obj: Any) throws {
|
||||
if let array = obj as? [EntityMapping] {
|
||||
array.forEach({_ = try? $0.map()})
|
||||
} else {
|
||||
guard let mapper = obj as? EntityMapping else { return }
|
||||
_ = try mapper.map()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension SLazeKit {
|
||||
public class func request<T: Encodable>(path: String, method: HTTPMethod, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return SLazeKit.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func request<T: Encodable>(path: String, method: HTTPMethod, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return SLazeKit.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func request(path: String, method: HTTPMethod, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return SLazeKit.networkTask(path: path, method: method, queryItems: queryItems, handler: handler)
|
||||
}
|
||||
|
||||
public class func get<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return SLazeKit.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func get<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return SLazeKit.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func get(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return SLazeKit.networkTask(path: path, queryItems: queryItems, handler: handler)
|
||||
}
|
||||
|
||||
public class func post<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return SLazeKit.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func post<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return SLazeKit.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func post(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return SLazeKit.networkTask(path: path, method: .POST, queryItems: queryItems, handler: handler)
|
||||
}
|
||||
|
||||
public class func put<T: Encodable>(path: String, queryItems: [URLQueryItem]? = nil, body: T, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return SLazeKit.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func put<T: Encodable>(path: String, queryItems: [URLQueryItem]? = nil, body: [T], handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return SLazeKit.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func put(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return SLazeKit.networkTask(path: path, method: .PUT, queryItems: queryItems, handler: handler)
|
||||
}
|
||||
|
||||
public class func delete<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return SLazeKit.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func delete<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return SLazeKit.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
|
||||
}
|
||||
|
||||
public class func delete(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: HTTPURLResponse?, _ error: Error?) -> ()) -> URLSessionDataTask? {
|
||||
return SLazeKit.networkTask(path: path, method: .DELETE, queryItems: queryItems, handler: handler)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ extension Int: StringInitializable {
|
|||
guard let value = Int(rawValue) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
self = value
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +18,6 @@ extension Double: StringInitializable {
|
|||
guard let value = Double(rawValue) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
self = value
|
||||
}
|
||||
}
|
||||
|
@ -29,21 +27,28 @@ extension KeyedDecodingContainerProtocol {
|
|||
guard contains(key) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
guard !(try decodeNil(forKey: key)) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
if let string = try? decode(String.self, forKey: key) {
|
||||
guard let value = T.init(rawValue: string) else {
|
||||
throw DecodingError.dataCorruptedError(forKey: key,
|
||||
in: self,
|
||||
debugDescription: "Found string that cannot be converted to \(T.self)")
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
return try decode(T.self, forKey: key)
|
||||
}
|
||||
func decodeUnstable<T: Decodable & StringInitializable>(_ type: T.Type = T.self, forKey key: Key) throws -> T {
|
||||
if let string = try? decode(String.self, forKey: key) {
|
||||
guard let value = T.init(rawValue: string) else {
|
||||
throw DecodingError.dataCorruptedError(forKey: key,
|
||||
in: self,
|
||||
debugDescription: "Found string that cannot be converted to \(T.self)")
|
||||
}
|
||||
return value
|
||||
}
|
||||
return try decode(T.self, forKey: key)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue