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

[RFC] Easier creation of new calls #3309

Closed
wants to merge 1 commit into from
Closed

[RFC] Easier creation of new calls #3309

wants to merge 1 commit into from

Conversation

christophehenry
Copy link

@christophehenry christophehenry commented Feb 20, 2020

Creating new Call<T> is far too difficult. Currently, it requires creating a new class implementing Call. Vut this is not handy in the case you just want to modify the call at run-time according to custom annotations using adapters.

@JakeWharton
Copy link
Member

JakeWharton commented Feb 20, 2020

Can you explain your use case more?

@christophehenry
Copy link
Author

christophehenry commented Feb 21, 2020

Sure! So I have an HTTP API that mainly work with short-lived tokens for authentication. This token must be retrieved by calling an endpoint secured by a basic authentication. I wanted to use custom annotation to:

  • intercept the calls to the HTTP API and renew the token if necessary before performing the actual call
  • dynamically set the authentication header when calling the token endpoint.

Now I know that the second use case can be handled by using a @Header parameter. But since I also have the first use case, my service implementation class has the credentials in its properties. I also know That both these use case can be handled by an interceptor, as described here, but not every endpoint requires the same authentication. So I would have to add a lot of logic to determine which endpoint is being called and which pre-processing it requires.

I ended up trying to distinguish the endpoints authenticated by a token from the token endpoint authenticated by a basic authentication using custom annotations and, unless I'm wrong, OkHttpClient's Interceptor won't have acces to annotations.

@swankjesse
Copy link
Member

swankjesse commented Feb 22, 2020

Does Invocation help?
https://square.github.io/retrofit/2.x/retrofit/retrofit2/Invocation.html


 class InvocationLogger implements Interceptor {
   @Override public Response intercept(Chain chain) throws IOException {
     Request request = chain.request();
     Invocation invocation = request.tag(Invocation.class);
     if (invocation != null) {
       System.out.printf("%s.%s %s%n",
           invocation.method().getDeclaringClass().getSimpleName(),
           invocation.method().getName(), invocation.arguments());
     }
     return chain.proceed(request);
   }
 }

@christophehenry
Copy link
Author

christophehenry commented Feb 23, 2020

@swankjesse Oh nice! That works well! Maybe this use-case could be worth documenting?
Thank you both a lot for taking time to answer to me 👍

@christophehenry christophehenry deleted the easier-call-creation branch Feb 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants