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

Drop the mutable base URL. #1652

Merged
merged 1 commit into from Mar 7, 2016
Merged

Conversation

swankjesse
Copy link
Member

This was unsafe for switching between development and production
backends. This is because requests that are triggered on a UI thread
may be delayed in execution on a background thread. These could cause
requests created for a development environment to be executed against
a production environment, or vice-versa.

Retrofit is the wrong layer for switching backends for load balancing
or proximity. That can happen in an OkHttp interceptor instead.

This was unsafe for switching between development and production
backends. This is because requests that are triggered on a UI thread
may be delayed in execution on a background thread. These could cause
requests created for a development environment to be executed against
a production environment, or vice-versa.

Retrofit is the wrong layer for switching backends for load balancing
or proximity. That can happen in an OkHttp interceptor instead.
@swankjesse
Copy link
Member Author

https://gist.github.com/swankjesse/8571a8207a5815cca1fb

JakeWharton added a commit that referenced this pull request Mar 7, 2016
@JakeWharton JakeWharton merged commit c7f7d23 into master Mar 7, 2016
@JakeWharton JakeWharton deleted the jwilson_0306_no_mutable_base_url branch March 7, 2016 05:53
@artem-zinnatullin
Copy link
Contributor

Whoa… Well, you're right that it's not safe to change url in Retrofit, but OkHttp interceptor is not a great place for that even more because not only it has the same problem with delayed requests but also you can't understand that request is coming from Retrofit inside of the interceptor (analyzing stacktrace is not an option)… Imagine you also have non-REST requests to the api endpoint that you do through same OkHttp instance without Retrofit.

Would you suggest to rebuild the Retrofit instance with new url and use something like Provider<Retrofit> instead of direct reference to the Retrofit or even rebuild whole graph of network dependencies to change endpoint in the application?

@JakeWharton
Copy link
Member

That has always been the recommendation, yes. BaseUrl existed solely for client-side load balancing or geographical proximity specialization of the same semantic host. Since there's no longer an HTTP client abstraction, we no longer need the behavior to exist in Retrofit.

@artem-zinnatullin
Copy link
Contributor

ok, thanks for confirmation.

// Anyway, 👍 👍 for more immutability, though BaseUrl was so convenient for functional testing with mock webserver…

@swankjesse
Copy link
Member Author

Yep. This new interceptor is the new best way to test with a mock webserver.

@jbaginski
Copy link

in my case baseUrl contains a path segment. I don't see any other way than manually replace the old baseUrl with the new one...
@swankjesse do you know any other way to change the baseUrl (with path) using interceptor mechanism?

@swankjesse
Copy link
Member Author

@jbaginski you can rewrite the URL however you like. One option is to extract out the path & query in an interceptor, and add those on to the base URL.

@jbaginski
Copy link

thanks @swankjesse, I ended up manipulating a string directly. it's not the best solution but it works ;)

@JessYanCoding
Copy link

This solution will affect @get (full path) @post (full path) and @url so that they can not work,try my solution: https://github.com/JessYanCoding/RetrofitUrlManager

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants