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?
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.
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!