Retrofit: I want to retrieve method annotations on interceptor.

Created on 23 Jul 2018  路  2Comments  路  Source: square/retrofit

I cannot retrieve method and parameter annotations on OkHttp interceptor, I'd like to use
annotations to modify request url, header or other configurations.
Or can author supply a new feature like interceptor but can obtain the method annotations
and add a method like addXxxFactory.

Most helpful comment

This is not possible, unfortunately. OkHttp does not know about Retrofit to know that a request is being executed by a method declaration and not normal OkHttp interaction. From the other side, Retrofit doesn't actually know that OkHttp is the HTTP client that is executing the request. It only uses OkHttp's model for Request/Response and doesn't actually know about OkHttpClient or interceptors.

The easiest way to pass information to an interceptor is with a header that you then strip. So your method would have something like @Headers("Custom-Thing: value") which you would then read in the interceptor and then remove the header.

All 2 comments

This is not possible, unfortunately. OkHttp does not know about Retrofit to know that a request is being executed by a method declaration and not normal OkHttp interaction. From the other side, Retrofit doesn't actually know that OkHttp is the HTTP client that is executing the request. It only uses OkHttp's model for Request/Response and doesn't actually know about OkHttpClient or interceptors.

The easiest way to pass information to an interceptor is with a header that you then strip. So your method would have something like @Headers("Custom-Thing: value") which you would then read in the interceptor and then remove the header.

Another option: we could add metadata to retrofit2.Call: the java.lang.reflect.Method and its parameters. And then we could use deluxe tags to tell the okhttp3.Request about its retrofit2.Call.

Was this page helpful?
0 / 5 - 0 ratings