Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for RequestRetrier and RequestAdapter #63

Merged
merged 3 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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