[0.1.9] Allow user to cancel request during clinet setup
This commit is contained in:
parent
ae18d59467
commit
7fd74e3094
|
@ -23,7 +23,7 @@ public protocol LazeConfiguration {
|
||||||
///
|
///
|
||||||
/// - Parameter request: `URLRequest` object to setup
|
/// - Parameter request: `URLRequest` object to setup
|
||||||
/// - Returns: already setup and customize URLRequest object
|
/// - Returns: already setup and customize URLRequest object
|
||||||
static func setup(_ request: URLRequest) -> URLRequest
|
static func setup(_ request: URLRequest) -> URLRequest?
|
||||||
|
|
||||||
/// Global handler for `HTTPURLResponse`. Called everytime response is capture.
|
/// Global handler for `HTTPURLResponse`. Called everytime response is capture.
|
||||||
///
|
///
|
||||||
|
|
|
@ -15,7 +15,12 @@ public enum HTTPMethod {
|
||||||
/// SLazeKit is an easy to use restful collection of extensions and classes. Maps your rest api request into models and provides coredata serialization.
|
/// 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> {
|
public class SLazeKit<Config: LazeConfiguration> {
|
||||||
class func networkTask(request: URLRequest, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> Void) -> URLSessionDataTask? {
|
class func networkTask(request: URLRequest, handler: @escaping (_ response: NetworkResponse, _ error: Error?) -> Void) -> URLSessionDataTask? {
|
||||||
let task = Config.urlSession.dataTask(with: Config.setup(request)) { (data, response, error) in
|
guard let req = Config.setup(request) else {
|
||||||
|
handler((nil,nil), NSError(domain: "", code: NSURLErrorCancelled,
|
||||||
|
userInfo: ["reason":"Client config return nil request"]))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
let task = Config.urlSession.dataTask(with: req) { (data, response, error) in
|
||||||
Config.handle(response as? HTTPURLResponse)
|
Config.handle(response as? HTTPURLResponse)
|
||||||
handler((data, response as? HTTPURLResponse), error)
|
handler((data, response as? HTTPURLResponse), error)
|
||||||
}
|
}
|
||||||
|
@ -24,7 +29,12 @@ public class SLazeKit<Config: LazeConfiguration> {
|
||||||
}
|
}
|
||||||
|
|
||||||
class func networkTask<T: Decodable>(request: URLRequest, handler: @escaping (_ response: NetworkResponse, _ result: T?, _ error: Error?) -> Void) -> URLSessionDataTask? {
|
class func networkTask<T: Decodable>(request: URLRequest, handler: @escaping (_ response: NetworkResponse, _ result: T?, _ error: Error?) -> Void) -> URLSessionDataTask? {
|
||||||
let task = Config.urlSession.dataTask(with: Config.setup(request)) { (data, response, error) in
|
guard let req = Config.setup(request) else {
|
||||||
|
handler((nil,nil),nil, NSError(domain: "", code: NSURLErrorCancelled,
|
||||||
|
userInfo: ["reason":"Client config return nil request"]))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
let task = Config.urlSession.dataTask(with: req) { (data, response, error) in
|
||||||
Config.handle(response as? HTTPURLResponse)
|
Config.handle(response as? HTTPURLResponse)
|
||||||
if let data = data, error == nil {
|
if let data = data, error == nil {
|
||||||
do {
|
do {
|
||||||
|
|
Loading…
Reference in New Issue