Skip to content

Commit

Permalink
[0.1.9] Allow user to cancel request during clinet setup
Browse files Browse the repository at this point in the history
  • Loading branch information
shial4 committed Jun 20, 2018
1 parent ae18d59 commit 7fd74e3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Sources/SLazeKit/Protocols/LazeConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public protocol LazeConfiguration {
///
/// - Parameter request: `URLRequest` object to setup
/// - 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.
///
Expand Down
14 changes: 12 additions & 2 deletions Sources/SLazeKit/SLazeKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
public class SLazeKit<Config: LazeConfiguration> {
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)
handler((data, response as? HTTPURLResponse), error)
}
Expand All @@ -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? {
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)
if let data = data, error == nil {
do {
Expand Down

0 comments on commit 7fd74e3

Please sign in to comment.