Skip to content

Commit

Permalink
Merge pull request #63 from n13/master
Browse files Browse the repository at this point in the history
Support for RequestRetrier and RequestAdapter
  • Loading branch information
s4cha authored Apr 2, 2019
2 parents d145348 + 351a2c7 commit 80567df
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
6 changes: 5 additions & 1 deletion ws/WS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ open class WS {

open var baseURL = ""
open var headers = [String: String]()

open var requestAdapter: RequestAdapter?
open var requestRetrier: RequestRetrier?

/**
Create a webservice instance.
@param Pass the base url of your webservice, E.g : "http://jsonplaceholder.typicode.com"
Expand All @@ -73,6 +75,8 @@ open class WS {
r.postParameterEncoding = postParameterEncoding
r.showsNetworkActivityIndicator = showsNetworkActivityIndicator
r.headers = headers
r.requestAdapter = requestAdapter
r.requestRetrier = requestRetrier
r.errorHandler = errorHandler
return r
}
Expand Down
39 changes: 35 additions & 4 deletions ws/WSRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ open class WSRequest {
open var postParameterEncoding: ParameterEncoding = URLEncoding()
open var showsNetworkActivityIndicator = true
open var errorHandler: ((JSON) -> Error?)?

open var requestAdapter: RequestAdapter?
open var requestRetrier: RequestRetrier?

private let logger = WSLogger()

fileprivate var req: DataRequest?//Alamofire.Request?
Expand Down Expand Up @@ -64,7 +66,36 @@ open class WSRequest {
}
return request ?? r
}

func wsSessionManager() -> SessionManager {
let sessionManager = Alamofire.SessionManager.default
if let adapter = requestAdapter {
sessionManager.adapter = adapter
}
if let retrier = requestRetrier {
sessionManager.retrier = retrier
}
return sessionManager
}

func wsRequest(_ urlRequest: URLRequestConvertible) -> DataRequest {
return wsSessionManager().request(urlRequest)
}

func wsUpload(
multipartFormData: @escaping (MultipartFormData) -> Void,
usingThreshold encodingMemoryThreshold: UInt64 = SessionManager.multipartFormDataEncodingMemoryThreshold,
with urlRequest: URLRequestConvertible,
encodingCompletion: ((SessionManager.MultipartFormDataEncodingResult) -> Void)?)
{
return wsSessionManager().upload(
multipartFormData: multipartFormData,
usingThreshold: encodingMemoryThreshold,
with: urlRequest,
encodingCompletion: encodingCompletion
)
}

/// Returns Promise containing JSON
open func fetch() -> Promise<JSON> {
return fetch().registerThen { (_, _, json) in json }
Expand All @@ -91,7 +122,7 @@ open class WSRequest {
func sendMultipartRequest(_ resolve: @escaping (_ result: (Int, [AnyHashable: Any], JSON)) -> Void,
reject: @escaping (_ error: Error) -> Void,
progress:@escaping (Float) -> Void) {
upload(multipartFormData: { formData in
wsUpload(multipartFormData: { formData in
for (key, value) in self.params {
let str: String
switch value {
Expand Down Expand Up @@ -129,7 +160,7 @@ open class WSRequest {

func sendRequest(_ resolve:@escaping (_ result: (Int, [AnyHashable: Any], JSON)) -> Void,
reject: @escaping (_ error: Error) -> Void) {
self.req = request(self.buildRequest())
self.req = wsRequest(self.buildRequest())
logger.logRequest(self.req!)
let bgQueue = DispatchQueue.global(qos: DispatchQoS.QoSClass.default)
req?.validate().response(queue: bgQueue) { response in
Expand All @@ -146,7 +177,7 @@ open class WSRequest {

func sendJSONRequest(_ resolve: @escaping (_ result: (Int, [AnyHashable: Any], JSON)) -> Void,
reject: @escaping (_ error: Error) -> Void) {
self.req = request(self.buildRequest())
self.req = wsRequest(self.buildRequest())
logger.logRequest(self.req!)
let bgQueue = DispatchQueue.global(qos: DispatchQoS.QoSClass.default)
req?.validate().responseJSON(queue: bgQueue) { r in
Expand Down

0 comments on commit 80567df

Please sign in to comment.