Search in sources :

Example 1 with AuthInterceptor

use of com.mxt.anitrend.model.api.interceptor.AuthInterceptor in project anitrend-app by AniTrend.

the class WebFactory method createService.

/**
 * Generates retrofit service classes in a background thread
 * and handles creation of API tokens or renewal of them
 * <br/>
 *
 * @param serviceClass The interface class to use such as
 *
 * @param context A valid application, fragment or activity context but must be application context
 */
public static <S> S createService(@NonNull Class<S> serviceClass, Context context) {
    WebTokenRequest.getToken(context);
    if (mRetrofit == null) {
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder().readTimeout(35, TimeUnit.SECONDS).connectTimeout(35, TimeUnit.SECONDS).addInterceptor(new AuthInterceptor(context));
        if (BuildConfig.DEBUG) {
            HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);
            httpClient.addInterceptor(httpLoggingInterceptor);
        }
        mRetrofit = anitrendBuilder.client(httpClient.build()).addConverterFactory(GraphConverter.create(context)).build();
    }
    return mRetrofit.create(serviceClass);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) AuthInterceptor(com.mxt.anitrend.model.api.interceptor.AuthInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Aggregations

AuthInterceptor (com.mxt.anitrend.model.api.interceptor.AuthInterceptor)1 OkHttpClient (okhttp3.OkHttpClient)1 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)1