-
Notifications
You must be signed in to change notification settings - Fork 206
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
Bump Retrofit version to 2.0.0 #137
Bump Retrofit version to 2.0.0 #137
Conversation
@DmitriyZaitsev: Thanks for your contribution, but it'd better to make it pass all the tests before create a PR. On the other hand, we have an issue #111 about |
|
||
/** | ||
* Such implementation allows us easily change base url in the integration and functional tests! | ||
*/ | ||
public class ChangeableBaseUrl implements BaseUrl { | ||
public class ChangeableBaseUrl { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Retrofit 2.0.0 drops the mutable base URL, so this class doesn't make sense any more and it'll break the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, there is an issue for that: #111.
For now you can do it via OkHttp interceptor/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing me out. I'll dig in this direction.
@lenguyenthanh thanks for your comment. Of course, before making this PR I made sure that local test have passed successfully. UPD: Will check again |
Indeed. My fault. :( Attempt №3 has brought me closer... |
1a7db71
to
91b5e43
Compare
if (host != null) { | ||
final HttpUrl newUrl = request.url() | ||
.newBuilder() | ||
.host(host) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems I should parse baseUrl
and build newUrl
also with port()
and scheme()
here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, probably
889c8ad
to
e4a82b7
Compare
@artem-zinnatullin |
e4a82b7
to
e7d7e16
Compare
Current coverage is
|
final String baseUrl = this.baseUrl; | ||
|
||
if (baseUrl != null) { | ||
request = modifyRequest(request, baseUrl); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about the problem that we redirect all requests (not only api ones) to the mock webserver?
I see 2 solutions:
- Do not rely on interceptor and recreate retrofit instance (clean solution)
- Interceptor should know about real base url and redirect only requests that would hit real base url
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, we do not redirect all requests to the mock webserver.
By default HostSelectionInterceptor
is dummy. It doesn't affect the current logic at all until baseUrl
is set.
In fact, we added the interceptor that does nothing, though it has the property of dynamic change of the base url if there is a desire.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do redirect all requests that goes through that instance of OkHttpClient
as soon as baseUrl
is set in the interceptor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right. I mean that dynamic url is used for testing purposes only and now only test code mutates the interceptor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing prevents us from hitting not only API in future and functional tests will cover that and we may have problems, that's why I'm trying to say
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. Recreate OkHttpClient
. :) Network module will provide two different OkHttpClient
instances. One for Retrofit
only (with HostSelectionInterceptor
) and the second one - for other dudes e.g. Picasso
(without that interceptor).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait wait wait then :)
Please check square/retrofit#1652 and #111
The idea is that Retrofit
has immutable baseUrl
so we can recreate Retrofit
and switch it to new url.
Recreating OkHttpClient
is not that great, we use it for many things and using one instance for everything is great because it reuses same connection pool and we don't allocate memory and resources for using multiple clients
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to recreate Retrofit
than OkHttp
, because baseUrl
is something depends on Retrofit
not OkHttp
. If we change baseUrl
in an Intercepter
, we add magic to our code. It's not really good.
Actually, what I'm doing in my project is: Using some kind of TestModule so that we can inject mock or fake url for testing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made some investigations and don't see beautiful solution. The problem is object graph is created before MockWebServer starts. If we want to instatiate Retrofit with baseUrl taken from mockWebServer, we should set that url into ApiModule. Using Provider<> in this case looks like a smelling hack for me.
When I was looking for solution, I found that Jake Wharton in his u2020 app uses the same approach I suggest: he uses separate OkHttpClient for API calls @Named("Api")
. And I believe it's great because makes code cleaner and gives two extra benefits:
• we can use dynamic url only for api in tests
• api client won't be busy and shouldn't wait while Picasso loads images.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyway, I think such refactoring is out of this scope.
I suggest next solution: use two OkHttpClients for now and next step - if you insist, make refactoring in other PR.
e7d7e16
to
ef3620f
Compare
I have a question that deals with OkHttp interceptors so I'm going to ask it here. I only want my interceptor action to be performed on certain requests, e.g. signing OAuth headers only for certain requests. What is the best way to "enable" and "disable" the functionality of my interceptor? I was thinking of having a boolean |
Better to have no state at all, can you pass this flag as request header On Tue, 10 May 2016, 14:57 Plastix, [email protected] wrote:
|
@artem-zinnatullin Thanks. I just figured this out myself before you responded. I'm going to be using a request header. |
Closing for now, will be part of 2.0 |
No description provided.