Retrofit 2 - Elegant way of adding headers in the api level
<p>My Retrofit 2 (<code>2.0.2</code> currently) client needs to add custom headers to requests.  </p>

<p>I'm using an <code>Interceptor</code> to add these headers to all requests:</p>

<pre><code>OkHttpClient httpClient = new OkHttpClient();
httpClient.networkInterceptors().add(new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
        final Request request = chain.request().newBuilder()
                .addHeader("CUSTOM_HEADER_NAME_1", "CUSTOM_HEADER_VALUE_1")
                .addHeader("CUSTOM_HEADER_NAME_2", "CUSTOM_HEADER_VALUE_2")
                ...
                .addHeader("CUSTOM_HEADER_NAME_N", "CUSTOM_HEADER_VALUE_N")
                .build();

        return chain.proceed(request);
    }
});


Retrofit retrofitClient = new Retrofit.Builder()
        .baseUrl(baseUrl)
        .client(httpClient)
        .build();
</code></pre>

<p>Some headers I always want to add, but some headers I only need to add based on requirements of that specific endpoint, for example whether the user needs to be authenticated or not.</p>

<p>I'd like to have the ability to control that at the api level, for example using an annotation, something like:</p>

<pre><code>public interface MyApi {
    @NO_AUTH
    @POST("register")
    Call&lt;RegisterResponse&gt; register(@Body RegisterRequest data);

    @GET("user/{userId}")
    Call&lt;GetUserResponse&gt; getUser(@Path("userId") String userId);
}
</code></pre>

<p>When sending a request to <code>register</code> there's no need to add the authentication token, but requests who lack the <code>@NO_AUTH</code> annotation will have the token header.</p>

<p>From what I understand Retrofit 2 doesn't support custom annotations, and while I found this workaround for <a href="https://medium.com/@andretietz/custom-annotations-with-retrofit-2-8701ca7ce102#.79dg4wb8v" rel="noreferrer">Custom Annotations with Retrofit 2</a>, it's seems a bit too much.</p>

<p>I'd like to avoid the need to pass these headers per request, like:</p>

<pre><code>public interface MyApi {
    @POST("register")
    Call&lt;RegisterResponse&gt; register(@Body RegisterRequest data);

    @GET("user/{userId}")
    Call&lt;GetUserResponse&gt; getUser(@Header("AuthToken") String token, @Path("userId") String userId);
}
</code></pre>

<p>It just feels redundant to do it every time I call the method instead of doing it in the interceptor (since I have access to the header values statically).<br>
I just somehow need to know in my <code>Interceptor.intercept</code> implementation whether or not this specific request should have a specific header(s).</p>

<p>Any idea how I can make this work?<br>
I prefer a generic solution and not just for the auth token case, but a specific solution is welcome as well. 
Thanks</p>