Example usage for com.squareup.okhttp OkHttpClient interceptors

List of usage examples for com.squareup.okhttp OkHttpClient interceptors

Introduction

In this page you can find the example usage for com.squareup.okhttp OkHttpClient interceptors.

Prototype

List interceptors

To view the source code for com.squareup.okhttp OkHttpClient interceptors.

Click Source Link

Usage

From source file:alberto.avengers.model.rest.RestDataSource.java

License:Mozilla Public License

@Inject
public RestDataSource() {
    OkHttpClient client = new OkHttpClient();

    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);

    MarvelSigningIterceptor signingIterceptor = new MarvelSigningIterceptor(BuildConfig.MARVEL_PUBLIC_KEY,
            BuildConfig.MARVEL_PRIVATE_KEY);

    client.interceptors().add(signingIterceptor);
    client.interceptors().add(loggingInterceptor);

    Gson customGsonInstance = new GsonBuilder().registerTypeAdapter(new TypeToken<List<Character>>() {
    }.getType(), new MarvelResultsDeserializer<Character>())

            .registerTypeAdapter(new TypeToken<List<CollectionItem>>() {
            }.getType(), new MarvelResultsDeserializer<CollectionItem>())

            .create();/*w  ww  .ja  v a2s  .  c  o  m*/

    Retrofit marvelApiAdapter = new Retrofit.Builder().baseUrl(MarvelApi.END_POINT)
            .addConverterFactory(GsonConverterFactory.create(customGsonInstance))
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create()).client(client).build();

    mMarvelApi = marvelApiAdapter.create(MarvelApi.class);
}

From source file:bitrefill.retrofit.Generator.java

static void configAuth(OkHttpClient client, Config config) {
    final String auth = getAuth(config);
    if (auth == null) {
        return;/*from   w  ww .  j ava2  s . co m*/
    }
    client.interceptors().clear();
    client.interceptors().add(new Interceptor() {
        @Override
        public Response intercept(Interceptor.Chain chain) throws IOException {
            Request original = chain.request();

            Request.Builder requestBuilder = original.newBuilder().header("Authorization", "Basic " + auth)
                    .header("Accept", "applicaton/json").method(original.method(), original.body());

            Request request = requestBuilder.build();
            return chain.proceed(request);
        }
    });

}

From source file:bitrefill.retrofit.Generator.java

static void configLogger(OkHttpClient client, Config config) {
    HttpLoggingInterceptor.Logger logger = config.getLogger();
    if (logger == null) {
        return;/* w  ww. jav a  2  s  .c o  m*/
    }
    HttpLoggingInterceptor.Level loggerLevel = config.getLoggerLevel();
    if (loggerLevel == null) {
        loggerLevel = HttpLoggingInterceptor.Level.BODY;
    }
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(logger);
    interceptor.setLevel(loggerLevel);
    client.interceptors().add(interceptor);
}

From source file:com.auth0.android.authentication.AuthenticationAPIClientTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test/*from   w ww  . ja v  a2 s. c  o  m*/
public void shouldEnableHttpLogging() throws Exception {
    Auth0 account = mock(Auth0.class);
    when(account.isLoggingEnabled()).thenReturn(true);
    RequestFactory factory = mock(RequestFactory.class);
    OkHttpClient okClient = mock(OkHttpClient.class);
    List list = mock(List.class);
    when(okClient.interceptors()).thenReturn(list);

    ArgumentCaptor<Interceptor> interceptorCaptor = ArgumentCaptor.forClass(Interceptor.class);
    new AuthenticationAPIClient(account, factory, okClient);

    verify(okClient).interceptors();
    verify(list).add(interceptorCaptor.capture());

    assertThat(interceptorCaptor.getValue(), is(notNullValue()));
    assertThat(interceptorCaptor.getValue(), is(instanceOf(HttpLoggingInterceptor.class)));
    assertThat(((HttpLoggingInterceptor) interceptorCaptor.getValue()).getLevel(),
            is(HttpLoggingInterceptor.Level.BODY));
}

From source file:com.auth0.android.authentication.AuthenticationAPIClientTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test// ww w .  j av  a  2  s  .c o m
public void shouldDisableHttpLogging() throws Exception {
    Auth0 account = mock(Auth0.class);
    when(account.isLoggingEnabled()).thenReturn(false);
    RequestFactory factory = mock(RequestFactory.class);
    OkHttpClient okClient = mock(OkHttpClient.class);
    List list = mock(List.class);
    when(okClient.interceptors()).thenReturn(list);

    new AuthenticationAPIClient(account, factory, okClient);

    verify(okClient, never()).interceptors();
    verify(list, never()).add(any(Interceptor.class));
}

From source file:com.auth0.android.authentication.AuthenticationAPIClientTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test/*www.  j  a v  a 2  s . co  m*/
public void shouldHaveHttpLoggingDisabledByDefault() throws Exception {
    Auth0 account = mock(Auth0.class);
    RequestFactory factory = mock(RequestFactory.class);
    OkHttpClient okClient = mock(OkHttpClient.class);
    List list = mock(List.class);
    when(okClient.interceptors()).thenReturn(list);

    new AuthenticationAPIClient(account, factory, okClient);

    verify(okClient, never()).interceptors();
    verify(list, never()).add(any(Interceptor.class));
}

From source file:com.auth0.android.management.UsersAPIClientTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test//from   w  w  w  .j  a  v a  2  s . com
public void shouldEnableHttpLogging() throws Exception {
    Auth0 account = mock(Auth0.class);
    when(account.isLoggingEnabled()).thenReturn(true);
    RequestFactory factory = mock(RequestFactory.class);
    OkHttpClient okClient = mock(OkHttpClient.class);
    List list = mock(List.class);
    when(okClient.interceptors()).thenReturn(list);

    ArgumentCaptor<Interceptor> interceptorCaptor = ArgumentCaptor.forClass(Interceptor.class);
    new UsersAPIClient(account, factory, okClient);

    verify(okClient).interceptors();
    verify(list).add(interceptorCaptor.capture());

    assertThat(interceptorCaptor.getValue(), is(notNullValue()));
    assertThat(interceptorCaptor.getValue(), is(instanceOf(HttpLoggingInterceptor.class)));
    assertThat(((HttpLoggingInterceptor) interceptorCaptor.getValue()).getLevel(),
            is(HttpLoggingInterceptor.Level.BODY));
}

From source file:com.auth0.android.management.UsersAPIClientTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test//from   w ww  .j a v a2 s.c  om
public void shouldDisableHttpLogging() throws Exception {
    Auth0 account = mock(Auth0.class);
    when(account.isLoggingEnabled()).thenReturn(false);
    RequestFactory factory = mock(RequestFactory.class);
    OkHttpClient okClient = mock(OkHttpClient.class);
    List list = mock(List.class);
    when(okClient.interceptors()).thenReturn(list);

    new UsersAPIClient(account, factory, okClient);

    verify(okClient, never()).interceptors();
    verify(list, never()).add(any(Interceptor.class));
}

From source file:com.auth0.android.management.UsersAPIClientTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test//from w ww.  jav a  2  s .  c om
public void shouldHaveHttpLoggingDisabledByDefault() throws Exception {
    Auth0 account = mock(Auth0.class);
    RequestFactory factory = mock(RequestFactory.class);
    OkHttpClient okClient = mock(OkHttpClient.class);
    List list = mock(List.class);
    when(okClient.interceptors()).thenReturn(list);

    new UsersAPIClient(account, factory, okClient);

    verify(okClient, never()).interceptors();
    verify(list, never()).add(any(Interceptor.class));
}

From source file:com.belatrixsf.allstars.utils.di.modules.RetrofitModule.java

License:Open Source License

@Singleton
@Provides//w w w.j  av  a  2  s . co  m
public Retrofit provideRetrofit() {
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient();
    client.interceptors().add(loggingInterceptor);
    client.interceptors().add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            String token = PreferencesManager.get().getToken();
            if (token != null) {
                Request request = chain.request().newBuilder().addHeader("Authorization", "Token " + token)
                        .build();
                return chain.proceed(request);
            }
            return chain.proceed(chain.request());
        }
    });
    return new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
            .baseUrl(BuildConfig.BASE_URL).client(client).build();
}