Moya: Would it be possible to use TargetType in request closures to be able to set timeouts based on target?

Created on 17 Jan 2017  路  3Comments  路  Source: Moya/Moya

Using Moya 8.0

I would like to be able to set timeouts for request based on TargetType because some of my calls need much longer timeouts than others. I'm using Moya to communicate with custom hardware were certain calls should fail quickly while others are known to take their time (like firmware update over Wifi).

I'm using a ReactiveCocoaMoyaProvider like this: ReactiveCocoaMoyaProvider(endpointClosure: endpointClosure, requestClosure: requestClosure, stubClosure: stubClosure, plugins: plugins)

With a requestClosure like this:

fileprivate static let requestClosure: ((Moya.Endpoint<StoveTarget>, @escaping Moya.MoyaProvider.RequestResultClosure) -> Void) = { (endpoint, done) -> Void in
    guard var urlRequest = endpoint.urlRequest else { return }
    urlRequest.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData
    urlRequest.timeoutInterval = Config.TimeOuts.pollIntervalHTTP / 2.0
    let result: Result<URLRequest, Moya.Error> = .success(urlRequest)
    done(result)
}

Unfortunately i have only the URL but no target to determine the timeout in this closure. Would it be possible to support this?

question

Most helpful comment

Thanks a lot for the hint. I must have overlooked that feature. I've created a plugin that modifies the timeouts according to the target, works like a charm. Thanks again!

All 3 comments

That's a good question, @gunterhager.

Before we investigate making this information available via the request closure, can you see if creating a custom PluginType would suit your needs? A PluginType has a prepare function that is called once the URLRequest has been created and is given an opportunity to return a modified request. prepare also accepts the target, so I believe you have the information needed there to meet this use case. What do you think?

Thanks a lot for the hint. I must have overlooked that feature. I've created a plugin that modifies the timeouts according to the target, works like a charm. Thanks again!

Great @gunterhager! Happy to help.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jeehut picture Jeehut  路  26Comments

gperdomor picture gperdomor  路  23Comments

BasThomas picture BasThomas  路  27Comments

justinmakaila picture justinmakaila  路  26Comments

jonlambert picture jonlambert  路  31Comments