square / okhttp

Square’s meticulous HTTP client for the JVM, Android, and GraalVM.
https://square.github.io/okhttp/
Apache License 2.0
45.86k stars 9.16k forks source link

Have more fine grained control over client on runtime #3259

Closed yoavgray closed 7 years ago

yoavgray commented 7 years ago

What kind of issue is this?

Feature Request: If I use Dagger & Retrofit and I want that one of my API requests will have different timeouts set, for example, I need to either inject another OkHttpClient -> Retrofit -> Service or build them on runtime when I make the request, which then is less performant (because I have to keep building it every time). That's a little annoying but fine for 2 different requests. If I want to be very careful with how my users are seeing errors and how I deal with timeouts, I could eventually need 4-5-6 different requests with different settings for the client.

Is there a way to set the timeout on runtime without having multiple OkHttpClient instances?

Thank you very much for this amazing library.

kurisu commented 7 years ago

This would be very helpful for user experiences with variable response times, such as a bidding process where we would want to show quick responses for initial bids, then progressively longer timeouts to get responses from other bidders who might take longer to respond.

swankjesse commented 7 years ago

Planning on doing this here: https://github.com/square/okhttp/issues/3039

yoavgray commented 6 years ago

So there's no documentation for what one is suppose to do when there's a need to change a call's timeout on flight.

This doesn't seem to work:

Request original = chain.request();
if (original.headers().get(TIMEOUT_HEADER) != null) {
    original = chain
        .withReadTimeout(Integer.valueOf(original.headers().get(TIMEOUT_HEADER)), TimeUnit.MILLISECONDS)
        .request();
}

Any suggestion?

JakeWharton commented 6 years ago

You need to use the new chain to invoke the request. You're basically thowing it away and using the original.

On Mon, Jan 15, 2018, 8:26 PM Yoav Gray notifications@github.com wrote:

So there's no documentation for what one is suppose to do when there's a need to change a call's timeout on flight.

This doesn't seem to work:

Request original = chain.request(); if (original.headers().get(TIMEOUT_HEADER) != null) { original = chain .withReadTimeout(Integer.valueOf(original.headers().get(TIMEOUT_HEADER)), TimeUnit.MILLISECONDS) .request(); }

Any suggestion?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/square/okhttp/issues/3259#issuecomment-357827135, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEERK4UiOFZrSbRRdCqDqo8P4YfWWmks5tK_rVgaJpZM4MwLeK .

swankjesse commented 6 years ago

Call proceed() on the returned chain.

yoavgray commented 6 years ago

I'll try that. Thanks for the prompt response.

On Mon, Jan 15, 2018, 5:34 PM Jesse Wilson notifications@github.com wrote:

Call proceed() on the returned chain.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/square/okhttp/issues/3259#issuecomment-357828175, or mute the thread https://github.com/notifications/unsubscribe-auth/AEW58M9SISkczh9Dkt9xLquAyTgxX7Lqks5tK_y7gaJpZM4MwLeK .

yoavgray commented 6 years ago

Yeah, you guys were right. I wasn't using the new chain. Thanks so much for your help!