-
Notifications
You must be signed in to change notification settings - Fork 7.3k
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
Access to full Response object in Converter #2987
Comments
Yeah I think in the next major version I'll abandon the unified |
I can't think of a good workaround for now either. The only way to sneak data into the converter would be to put it inside the content type. So you could have an interceptor which, when receiving a response, grabbed the final URL and tucked it into the content type. Then your converter would parse out that URL from the content type and use it to resolve the links into absolute ones. Really not great, but it would work... |
I wasn't suggesting changing the Converter interface. It's more the factory class i was thinking. |
Yeah the factory is called once per endpoint and then reused for the life
of the instance.
…On Tue, Jan 22, 2019, 6:42 PM Matt Inger ***@***.*** wrote:
I wasn't suggesting changing the Converter interface. It's more the
factory class i was thinking.
Looking further at the HttpServiceMethod it looks like the body isn't
available at the time the responseBodyConverter is being created. So I
don't really see a good workaround either. Perhaps i can work around it at
a higher level, and post process the object after retrofit returns and
examine all the various annotations we use to determine what a url is, and
do it then.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#2987 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAEEESwmprVQHApw8u1xb8kgzwuBZoW3ks5vF6HqgaJpZM4ZSZ42>
.
|
Widening this to access to the full |
Can't find an existing issue for accessing headers in a converter, but here's my +1 for that use case |
Is the issue below solves?
@Test
public void getWithJavaUriUrl() {
class Example {
@GET
Call<ResponseBody> method(@Url URI url) {
return null;
}
}
Request request = buildRequest(Example.class, URI.create("foo/bar/"));
assertThat(request.method()).isEqualTo("GET");
assertThat(request.headers().size()).isZero();
assertThat(request.url().toString()).isEqualTo("http://example.com/foo/bar/");
assertThat(request.body()).isNull();
}
@Test
public void getWithStringUrlAbsolute() {
class Example {
@GET
Call<ResponseBody> method(@Url String url) {
return null;
}
}
Request request = buildRequest(Example.class, "https://example2.com/foo/bar/");
assertThat(request.method()).isEqualTo("GET");
assertThat(request.headers().size()).isZero();
assertThat(request.url().toString()).isEqualTo("https://example2.com/foo/bar/");
assertThat(request.body()).isNull();
} |
My team and I were waiting for a solution on how to read headers from the response into the result JSON DTO with Retrofit. Some backend teams keep telling us: "It's the protocol to return stuff like page count, page total, and nonce, in the headers of the response. We don't want to add it in the body just for you." I currently have to deal with the WooCommerce apis, and several apis return important headers ("X-WP-TotalPages", "X-WC-Store-API-Nonce", ...).
And having to read the response as is:
|
@JakeWharton any ETA to enable this? Will have to figure out another approach for my use-case mentioned here otherwise. |
Years. Maybe never. |
What kind of issue is this?
Question. This issue tracker is not the place for questions. If you want to ask how to do
something, or to understand why something isn't working the way you expect it to, use Stack
Overflow. https://stackoverflow.com/questions/tagged/retrofit
Bug report. If you’ve found a bug, spend the time to write a failing test. Bugs with tests
get fixed. Here’s an example: https://gist.github.com/swankjesse/6608b4713ad80988cdc9
[ X] Feature Request. Start by telling us what problem you’re trying to solve. Often a solution
already exists! Don’t send pull requests to implement new features without first getting our
support. Sometimes we leave features out on purpose to keep the project small.
Our application is consuming hypermedia responses which have some links:
We want to move to retrofit, and we can use the @url annotation for dynamic urls.
The issue i'm having is that if the href is absolute, which is a new requirement for us, we will follow it correctly. However the links we get on the resulting page, would still be relative, but would use the baseUrl of the retrofit client, rather than the base of the response where the links were loaded from.
So the first response might have the following link sequence:
https://my.api.com/call
link1 -> https://some.other.api.com/call
https://some.other.api.com/call
link2 -> /call2
The way we're dealing with this now is to turn all links into absolute links at parse time, since we have the Response object available to us to the get the url that loaded the response.
However, with retrofit, i'm not seeing a way of doing this within a Converter.Factory. It seems to be agnostic to where the response body actually came from. So link2 would still use https://my.api.com as the baseUrl, since that's what's on the retrofit client
I'd like to be able to access the request URL as part of the Converter.Factory instance so that i can use it in my parsing logic.
The text was updated successfully, but these errors were encountered: