From 69001063223ffef5f0ff7a14cdae75d846f78bc9 Mon Sep 17 00:00:00 2001 From: n13 Date: Sun, 24 Mar 2019 12:24:21 +0100 Subject: [PATCH 1/3] Support for RequestAdapter --- ws/WSRequest.swift | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/ws/WSRequest.swift b/ws/WSRequest.swift index d7e7deb..fbca4ab 100644 --- a/ws/WSRequest.swift +++ b/ws/WSRequest.swift @@ -34,7 +34,8 @@ open class WSRequest { open var postParameterEncoding: ParameterEncoding = URLEncoding() open var showsNetworkActivityIndicator = true open var errorHandler: ((JSON) -> Error?)? - + open var requestAdapter: RequestAdapter? + private let logger = WSLogger() fileprivate var req: DataRequest?//Alamofire.Request? @@ -64,7 +65,33 @@ open class WSRequest { } return request ?? r } - + + func wsRequest(_ urlRequest: URLRequestConvertible) -> DataRequest { + let sessionManager = Alamofire.SessionManager.default + if let adapter = requestAdapter { + sessionManager.adapter = adapter + } + return sessionManager.request(urlRequest) + } + + func wsUpload( + multipartFormData: @escaping (MultipartFormData) -> Void, + usingThreshold encodingMemoryThreshold: UInt64 = SessionManager.multipartFormDataEncodingMemoryThreshold, + with urlRequest: URLRequestConvertible, + encodingCompletion: ((SessionManager.MultipartFormDataEncodingResult) -> Void)?) + { + let sessionManager = Alamofire.SessionManager.default + if let adapter = requestAdapter { + sessionManager.adapter = adapter + } + return sessionManager.upload( + multipartFormData: multipartFormData, + usingThreshold: encodingMemoryThreshold, + with: urlRequest, + encodingCompletion: encodingCompletion + ) + } + /// Returns Promise containing JSON open func fetch() -> Promise { return fetch().registerThen { (_, _, json) in json } @@ -91,7 +118,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 { @@ -129,7 +156,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 @@ -146,7 +173,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 From 2639b5eab66d18bec16a0709172a45bcb5c30200 Mon Sep 17 00:00:00 2001 From: n13 Date: Sun, 24 Mar 2019 12:46:45 +0100 Subject: [PATCH 2/3] added requestAdapter --- ws/WS.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ws/WS.swift b/ws/WS.swift index 0b42d50..8cf141c 100644 --- a/ws/WS.swift +++ b/ws/WS.swift @@ -46,7 +46,8 @@ open class WS { open var baseURL = "" open var headers = [String: String]() - + open var requestAdapter: RequestAdapter? + /** Create a webservice instance. @param Pass the base url of your webservice, E.g : "http://jsonplaceholder.typicode.com" @@ -73,6 +74,7 @@ open class WS { r.postParameterEncoding = postParameterEncoding r.showsNetworkActivityIndicator = showsNetworkActivityIndicator r.headers = headers + r.requestAdapter = requestAdapter r.errorHandler = errorHandler return r } From 351a2c77ca6ee95b1ac00848dd535a47c68c51ac Mon Sep 17 00:00:00 2001 From: n13 Date: Mon, 25 Mar 2019 18:26:10 +0100 Subject: [PATCH 3/3] add support for RequestRetrier --- ws/WS.swift | 2 ++ ws/WSRequest.swift | 18 +++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/ws/WS.swift b/ws/WS.swift index 8cf141c..fb7933b 100644 --- a/ws/WS.swift +++ b/ws/WS.swift @@ -47,6 +47,7 @@ open class WS { open var baseURL = "" open var headers = [String: String]() open var requestAdapter: RequestAdapter? + open var requestRetrier: RequestRetrier? /** Create a webservice instance. @@ -75,6 +76,7 @@ open class WS { r.showsNetworkActivityIndicator = showsNetworkActivityIndicator r.headers = headers r.requestAdapter = requestAdapter + r.requestRetrier = requestRetrier r.errorHandler = errorHandler return r } diff --git a/ws/WSRequest.swift b/ws/WSRequest.swift index fbca4ab..fc0012f 100644 --- a/ws/WSRequest.swift +++ b/ws/WSRequest.swift @@ -35,6 +35,7 @@ open class WSRequest { open var showsNetworkActivityIndicator = true open var errorHandler: ((JSON) -> Error?)? open var requestAdapter: RequestAdapter? + open var requestRetrier: RequestRetrier? private let logger = WSLogger() @@ -66,12 +67,19 @@ open class WSRequest { return request ?? r } - func wsRequest(_ urlRequest: URLRequestConvertible) -> DataRequest { + func wsSessionManager() -> SessionManager { let sessionManager = Alamofire.SessionManager.default if let adapter = requestAdapter { sessionManager.adapter = adapter } - return sessionManager.request(urlRequest) + if let retrier = requestRetrier { + sessionManager.retrier = retrier + } + return sessionManager + } + + func wsRequest(_ urlRequest: URLRequestConvertible) -> DataRequest { + return wsSessionManager().request(urlRequest) } func wsUpload( @@ -80,11 +88,7 @@ open class WSRequest { with urlRequest: URLRequestConvertible, encodingCompletion: ((SessionManager.MultipartFormDataEncodingResult) -> Void)?) { - let sessionManager = Alamofire.SessionManager.default - if let adapter = requestAdapter { - sessionManager.adapter = adapter - } - return sessionManager.upload( + return wsSessionManager().upload( multipartFormData: multipartFormData, usingThreshold: encodingMemoryThreshold, with: urlRequest,