Skip to content

Commit

Permalink
added convenient progress handler for all requests
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusGeffarth authored Jun 15, 2018
1 parent 754859e commit 661ff94
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions Source/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ open class HTTP {
Class method to run a GET request that handles the URLRequest and parameter encoding for you.
*/
@discardableResult open class func GET(_ url: String, parameters: HTTPParameterProtocol? = nil, headers: [String:String]? = nil,
requestSerializer: HTTPSerializeProtocol = HTTPParameterSerializer(), completionHandler: ((Response) -> Void)? = nil) -> HTTP? {
return Run(url, method: .GET, parameters: parameters, headers: headers, requestSerializer: requestSerializer, completionHandler: completionHandler)
requestSerializer: HTTPSerializeProtocol = HTTPParameterSerializer(), progressHandler: ((Float) -> Void)? = nil, completionHandler: ((Response) -> Void)? = nil) -> HTTP? {
return Run(url, method: .GET, parameters: parameters, headers: headers, requestSerializer: requestSerializer, progressHandler: progressHandler, completionHandler: completionHandler)
}

/**
Expand All @@ -233,27 +233,27 @@ open class HTTP {
/**
Class method to run a POST request that handles the URLRequest and parameter encoding for you.
*/
@discardableResult open class func POST(_ url: String, parameters: HTTPParameterProtocol? = nil, headers: [String:String]? = nil, requestSerializer: HTTPSerializeProtocol = HTTPParameterSerializer(), completionHandler: ((Response) -> Void)? = nil) -> HTTP? {
return Run(url, method: .POST, parameters: parameters, headers: headers, requestSerializer: requestSerializer, completionHandler: completionHandler)
@discardableResult open class func POST(_ url: String, parameters: HTTPParameterProtocol? = nil, headers: [String:String]? = nil, requestSerializer: HTTPSerializeProtocol = HTTPParameterSerializer(), progressHandler: ((Float) -> Void)? = nil, completionHandler: ((Response) -> Void)? = nil) -> HTTP? {
return Run(url, method: .POST, parameters: parameters, headers: headers, requestSerializer: requestSerializer, progressHandler: progressHandler, completionHandler: completionHandler)
}

/**
Class method to run a PUT request that handles the URLRequest and parameter encoding for you.
*/
@discardableResult open class func PUT(_ url: String, parameters: HTTPParameterProtocol? = nil, headers: [String:String]? = nil,
requestSerializer: HTTPSerializeProtocol = HTTPParameterSerializer(), completionHandler: ((Response) -> Void)? = nil) -> HTTP? {
return Run(url, method: .PUT, parameters: parameters, headers: headers, requestSerializer: requestSerializer, completionHandler: completionHandler)
requestSerializer: HTTPSerializeProtocol = HTTPParameterSerializer(), progressHandler: ((Float) -> Void)? = nil, completionHandler: ((Response) -> Void)? = nil) -> HTTP? {
return Run(url, method: .PUT, parameters: parameters, headers: headers, requestSerializer: requestSerializer, progressHandler: progressHandler, completionHandler: completionHandler)
}

/**
Class method to run a PUT request that handles the URLRequest and parameter encoding for you.
Class method to run a PATCH request that handles the URLRequest and parameter encoding for you.
*/
@discardableResult open class func PATCH(_ url: String, parameters: HTTPParameterProtocol? = nil, headers: [String:String]? = nil, requestSerializer: HTTPSerializeProtocol = HTTPParameterSerializer(), completionHandler: ((Response) -> Void)? = nil) -> HTTP? {
return Run(url, method: .PATCH, parameters: parameters, headers: headers, requestSerializer: requestSerializer, completionHandler: completionHandler)
}

@discardableResult class func Run(_ url: String, method: HTTPVerb, parameters: HTTPParameterProtocol? = nil, headers: [String:String]? = nil, requestSerializer: HTTPSerializeProtocol = HTTPParameterSerializer(), completionHandler: ((Response) -> Void)? = nil) -> HTTP? {
guard let task = HTTP.New(url, method: method, parameters: parameters, headers: headers, requestSerializer: requestSerializer, completionHandler: completionHandler) else {return nil}
@discardableResult class func Run(_ url: String, method: HTTPVerb, parameters: HTTPParameterProtocol? = nil, headers: [String:String]? = nil, requestSerializer: HTTPSerializeProtocol = HTTPParameterSerializer(), progressHandler: ((Float) -> Void)? = nil, completionHandler: ((Response) -> Void)? = nil) -> HTTP? {
guard let task = HTTP.New(url, method: method, parameters: parameters, headers: headers, requestSerializer: requestSerializer, progressHandler: progressHandler, completionHandler: completionHandler) else {return nil}
task.run()
return task
}
Expand All @@ -262,16 +262,16 @@ open class HTTP {
Class method to create a Download request that handles the URLRequest and parameter encoding for you.
*/
open class func Download(_ url: String, parameters: HTTPParameterProtocol? = nil, headers: [String:String]? = nil,
requestSerializer: HTTPSerializeProtocol = HTTPParameterSerializer(), completion:@escaping ((Response, URL) -> Void)) {
guard let task = HTTP.New(url, method: .GET, parameters: parameters, headers: headers, requestSerializer: requestSerializer) else {return}
requestSerializer: HTTPSerializeProtocol = HTTPParameterSerializer(), progressHandler: ((Float) -> Void)? = nil, completion:@escaping ((Response, URL) -> Void)) {
guard let task = HTTP.New(url, method: .GET, parameters: parameters, headers: headers, requestSerializer: requestSerializer, progressHandler: progressHandler) else {return}
task.downloadHandler = completion
task.run()
}

/**
Class method to create a HTTP request that handles the URLRequest and parameter encoding for you.
*/
open class func New(_ url: String, method: HTTPVerb, parameters: HTTPParameterProtocol? = nil, headers: [String:String]? = nil, requestSerializer: HTTPSerializeProtocol = HTTPParameterSerializer(), completionHandler: ((Response) -> Void)? = nil) -> HTTP? {
open class func New(_ url: String, method: HTTPVerb, parameters: HTTPParameterProtocol? = nil, headers: [String:String]? = nil, requestSerializer: HTTPSerializeProtocol = HTTPParameterSerializer(), progressHandler: ((Float) -> Void)? = nil, completionHandler: ((Response) -> Void)? = nil) -> HTTP? {
guard var req = URLRequest(urlString: url, headers: headers) else {
guard let handler = completionHandler else { return nil }
let resp = Response()
Expand All @@ -293,6 +293,7 @@ open class HTTP {
}
}
let httpReq = HTTP(req)
httpReq.progress = progressHandler
httpReq.onFinish = completionHandler
return httpReq
}
Expand Down

0 comments on commit 661ff94

Please sign in to comment.