title: Design pattern for retrofit interface
body: <p>I have trouble with the design of my Retrofit interface creator. I want to be able to instanciate the API interface in a generic way and update the corresponding instance whenever a token is passed. Currently, when I update the token, I have to call createService() method again to get the new instance that used the token in the generation of the Interface...</p>
  
  <p>Somebody asked for a similar question but never got an answer <a href="https://stackoverflow.com/questions/35498787/design-pattern-for-building-an-interface-to-a-rest-api">here</a></p>
  
  <pre><code>public class RetrofitCreator {
  
      private static String TAG = "RetrofitCreator";
      private static String WSSE = null;
      private static String AmzToken = null;
      static HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
      private static AmazonAPI amazonAPI = null;
      private static VanishAPI cobaltAPI = null;
  
      //static OkHttpClient client = new OkHttpClient.Builder().build();
      static OkHttpClient.Builder httpClient = new OkHttpClient.Builder().addInterceptor(interceptor.setLevel(HttpLoggingInterceptor.Level.BODY));
  
      private static Retrofit.Builder builder =
              new Retrofit.Builder();
  
      public static &lt;S&gt; S createService(Class&lt;S&gt; serviceClass) {
  
          S mAPI = null;
              if(serviceClass.getSimpleName().equals("VanishAPI")){
                  if(VanishAPI==null){
                      VanishAPI = (VanishAPI) createVanishAPI(serviceClass);
                  }
                  mAPI = (S) VanishAPI;
              }else if(serviceClass.getSimpleName().equals("AmazonAPI")){
                  if(amazonAPI==null){
                      amazonAPI = (AmazonAPI) createAmazonAPI(serviceClass);
                  }
                  mAPI = (S) amazonAPI;
              }
          return mAPI;
      }
  
  
      public static void setWSSE(String WSSE) {
          RetrofitCreator.WSSE = WSSE;
          vanishAPI = createVanishAPI(VanishAPI.class);
      }
  
      public static void setAmzToken(String token) {
          RetrofitCreator.AmzToken = token;
          amazonAPI = createAmazonAPI(AmazonAPI.class);
      }
  
  private static &lt;S&gt; S createAmazonAPI(Class&lt;S&gt; serviceClass){
      httpClient = getUnsafeOkHttpClient();
      builder = new Retrofit.Builder()
              .baseUrl(Constants.URL_AMAZON)
              .addConverterFactory(JacksonConverterFactory.create());
  
      if (AmzToken != null) {
          Log.w(TAG, "WSSE not null!");
          Interceptor interceptorSecure = new Interceptor() {
              @Override
              public Response intercept(Interceptor.Chain chain) throws IOException {
                  Request original = chain.request();
  
                  // Request customization: add request headers
                  Request.Builder requestBuilder = original.newBuilder()
                          .header("Cache-Control", "no-cache")
                          .header("Accept", "application/json")
                          .header("Authorization", "Bearer " + AmzToken)
                          .method(original.method(), original.body());
  
                  Request request = requestBuilder.build();
                  return chain.proceed(request);
              }
          };
          httpClient.addInterceptor(interceptorSecure);
      }
  
      OkHttpClient client = httpClient.build();
      Retrofit retrofit = builder.client(client).build();
      return retrofit.create(serviceClass);
  }
  
  (...)
  }
  </code></pre>
  
  <p>To get it in each Activity I use : </p>
  
  <pre><code>amazonApi = RetrofitCreator.createService(AmazonAPI.class);
  </code></pre>