Skip to content

[RFC] Easier creation of new calls - #3309

Closed
christophehenry wants to merge 1 commit into
lysine-dev:masterfrom
christophehenry:easier-call-creation
Closed

[RFC] Easier creation of new calls#3309
christophehenry wants to merge 1 commit into
lysine-dev:masterfrom
christophehenry:easier-call-creation

Conversation

@christophehenry

Copy link
Copy Markdown

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
Copy Markdown
Collaborator

Can you explain your use case more?

@christophehenry

christophehenry commented Feb 21, 2020

Copy link
Copy Markdown
Author

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
Copy Markdown
Collaborator

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
Copy Markdown
Author

@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 February 23, 2020 10:41
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.

3 participants