[0.1.8] Decodable requires api configuration as a parameter

This commit is contained in:
Shial 2018-05-25 12:12:27 +10:00
parent 7b57b140a7
commit f62f208efa
3 changed files with 60 additions and 77 deletions

View File

@ -1,99 +1,99 @@
import Foundation
extension Decodable {
public static func request(path: String, method: HTTPMethod, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
public static func request<C: LazeConfiguration>(config: C.Type, path: String, method: HTTPMethod, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.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: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
public static func request<C: LazeConfiguration, T: Encodable>(config: C.Type, path: String, method: HTTPMethod, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.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: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: method, queryItems: queryItems, body: body, handler: handler)
public static func request<C: LazeConfiguration, T: Encodable>(config: C.Type, path: String, method: HTTPMethod, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.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: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: method, queryItems: queryItems, handler: handler)
public static func request<C: LazeConfiguration>(config: C.Type, path: String, method: HTTPMethod, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: method, queryItems: queryItems, handler: handler)
}
public static func get(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
public static func get<C: LazeConfiguration>(config: C.Type, path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.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: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
public static func get<C: LazeConfiguration, T: Encodable>(config: C.Type, path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.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: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
public static func get<C: LazeConfiguration, T: Encodable>(config: C.Type, path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, queryItems: queryItems, body: body, handler: handler)
}
public static func get(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, queryItems: queryItems, handler: handler)
public static func get<C: LazeConfiguration>(config: C.Type, path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, queryItems: queryItems, handler: handler)
}
public static func post(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
public static func post<C: LazeConfiguration>(config: C.Type, path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.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: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
public static func post<C: LazeConfiguration, T: Encodable>(config: C.Type, path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.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: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
public static func post<C: LazeConfiguration, T: Encodable>(config: C.Type, path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .POST, queryItems: queryItems, body: body, handler: handler)
}
public static func post(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: .POST, queryItems: queryItems, handler: handler)
public static func post<C: LazeConfiguration>(config: C.Type, path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .POST, queryItems: queryItems, handler: handler)
}
public static func put(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
public static func put<C: LazeConfiguration>(config: C.Type, path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.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: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
public static func put<C: LazeConfiguration, T: Encodable>(config: C.Type, path: String, queryItems: [URLQueryItem]? = nil, body: T, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.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: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
public static func put<C: LazeConfiguration, T: Encodable>(config: C.Type, path: String, queryItems: [URLQueryItem]? = nil, body: [T], handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .PUT, queryItems: queryItems, body: body, handler: handler)
}
public static func put(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: .PUT, queryItems: queryItems, handler: handler)
public static func put<C: LazeConfiguration>(config: C.Type, path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .PUT, queryItems: queryItems, handler: handler)
}
public static func patch(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler)
public static func patch<C: LazeConfiguration>(config: C.Type, path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler)
}
public static func patch<T: Encodable>(path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler)
public static func patch<C: LazeConfiguration, T: Encodable>(config: C.Type, path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler)
}
public static func patch<T: Encodable>(path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler)
public static func patch<C: LazeConfiguration, T: Encodable>(config: C.Type, path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .PATCH, queryItems: queryItems, body: body, handler: handler)
}
public static func patch(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: .PATCH, queryItems: queryItems, handler: handler)
public static func patch<C: LazeConfiguration>(config: C.Type, path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .PATCH, queryItems: queryItems, handler: handler)
}
public static func delete(path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
public static func delete<C: LazeConfiguration>(config: C.Type, path: String, body: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.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: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
public static func delete<C: LazeConfiguration, T: Encodable>(config: C.Type, path: String, body: T, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.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: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
public static func delete<C: LazeConfiguration, T: Encodable>(config: C.Type, path: String, body: [T], queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .DELETE, queryItems: queryItems, body: body, handler: handler)
}
public static func delete(path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<DefaultLazeConfiguration>.networkTask(path: path, method: .DELETE, queryItems: queryItems, handler: handler)
public static func delete<C: LazeConfiguration>(config: C.Type, path: String, queryItems: [URLQueryItem]? = nil, handler: @escaping (_ response: NetworkResponse, _ result: Self?, _ error: Error?) -> Void) -> URLSessionDataTask? {
return SLazeKit<C>.networkTask(path: path, method: .DELETE, queryItems: queryItems, handler: handler)
}
}

View File

@ -30,8 +30,19 @@ public protocol LazeConfiguration {
/// - Parameter request: `HTTPURLResponse` object to handle
static func handle(_ response: HTTPURLResponse?)
/// Required override of this method which will provide Context for bacground execution.
/// Override of this method which will provide Context for bacground execution.
///
/// - Returns: NSManagedObjectContext
static func newBackgroundContext() -> NSManagedObjectContext?
}
extension LazeConfiguration {
public static var basePort: Int? { return nil }
public static var decoder: JSONDecoder { return JSONDecoder() }
public static var urlSession: URLSession { return URLSession.shared }
public static func setup(_ request: URLRequest) -> URLRequest { return request }
public static func handle(_ response: HTTPURLResponse?) {}
public static func newBackgroundContext() -> NSManagedObjectContext? { return nil }
}

View File

@ -12,34 +12,6 @@ public enum HTTPMethod {
case GET, POST, PUT, PATCH, DELETE, COPY, HEAD, OPTIONS, LINK, UNLINK, PURGE, LOCK, UNLOCK, PROPFIND, VIEW
}
/// Default laze configuration
open class DefaultLazeConfiguration: LazeConfiguration {
/// Provide base path for your API requests.
open static var basePath: String? { return nil }
/// Additional you can set port for your requests.
open static var basePort: Int? { return nil }
/// Optional provider for JSONDecoder instance.
open static var decoder: JSONDecoder { return JSONDecoder() }
/// Optional provider for JSONDecoder instance.
open static var urlSession: URLSession { return URLSession.shared }
/// Global outgoing `URLRequest` customization. Called everytime request is created before executed.
///
/// - Parameter request: `URLRequest` object to setup
/// - Returns: already setup and customize URLRequest object
open static func setup(_ request: URLRequest) -> URLRequest { return request }
/// Global handler for `HTTPURLResponse`. Called everytime response is capture.
///
/// - Parameter request: `HTTPURLResponse` object to handle
open static func handle(_ response: HTTPURLResponse?) {}
/// Required override of this method which will provide Context for bacground execution.
///
/// - Returns: NSManagedObjectContext
open static func newBackgroundContext() -> NSManagedObjectContext? { return nil }
}
/// SLazeKit is an easy to use restful collection of extensions and classes. Maps your rest api request into models and provides coredata serialization.
public class SLazeKit<Config: LazeConfiguration> {
class func networkTask(request: URLRequest, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> Void) -> URLSessionDataTask? {