java - Retrofit 2 - Elegant way of adding headers in the api level -


my retrofit 2 (2.0.2 currently) client needs add custom headers requests.

i'm using interceptor add these headers requests:

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(); 

some headers want add, headers need add based on requirements of specific endpoint, example whether user needs authenticated or not.

i'd have ability control @ api level, example using annotation, like:

public interface myapi {     @no_auth     @post("register")     call<registerresponse> register(@body registerrequest data);      @get("user/{userid}")     call<getuserresponse> getuser(@path("userid") string userid); } 

when sending request register there's no need add authentication token, requests lack @no_auth annotation have token header.

from understand retrofit 2 doesn't support custom annotations, , while found workaround custom annotations retrofit 2, it's seems bit much.

i'd avoid need pass these headers per request, like:

public interface myapi {     @post("register")     call<registerresponse> register(@body registerrequest data);      @get("user/{userid}")     call<getuserresponse> getuser(@header("authtoken") string token, @path("userid") string userid); } 

it feels redundant every time call method instead of doing in interceptor (since have access header values statically).
somehow need know in interceptor.intercept implementation whether or not specific request should have specific header(s).

any idea how can make work?
prefer generic solution , not auth token case, specific solution welcome well. thanks

i came simple , elegant (in opinion) solution problem, , other scenarios.

i use headers annotation pass custom annotations, , since okhttp requires follow name: value format, decided format be: @: annotation_name.

so basically:

public interface myapi {     @post("register")     @headers("@: noauth")     call<registerresponse> register(@body registerrequest data);      @get("user/{userid}")     call<getuserresponse> getuser(@path("userid") string userid); } 

then can intercept request, check whether have annotation name @. if so, value , remove header request.
works if want have more 1 "custom annotation":

@headers({     "@: noauth",     "@: logresponsecode" }) 

here's how extract of these "custom annotations" , remove them request:

new okhttpclient.builder().addnetworkinterceptor(new interceptor() {     @override     public okhttp3.response intercept(chain chain) throws ioexception {         request request = chain.request();          list<string> customannotations = request.headers().values("@");          // "custom annotations"          request = request.newbuilder().removeheader("@").build();         return chain.proceed(request);     } }); 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -