Advertisement
Guest User

ApiModule

a guest
Jan 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. @Module
  2. public class ApiModule {
  3.  
  4.     @Provides
  5.     @Singleton
  6.     Retrofit provideRetrofit(OkHttpClient okHttpClient) {
  7.         return new Retrofit.Builder()
  8.                 .baseUrl(BASE_URL)
  9.                 .client(okHttpClient)
  10.                 .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
  11.                 .addConverterFactory(GsonConverterFactory.create())
  12.                 .build();
  13.     }
  14.  
  15.     @Provides
  16.     @Singleton
  17.     OkHttpClient provideHttpClient(Context context) {
  18.         HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
  19.         interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
  20.         OkHttpClient.Builder builder = new OkHttpClient.Builder()
  21.                 .addInterceptor(interceptor)
  22.                 .addInterceptor(chain -> {
  23.                     if (!Utils.isOnline(context)) {
  24.                         throw new NoInternetException();
  25.                     }
  26.                     return chain.proceed(chain.request());
  27.                 });
  28.         return builder.build();
  29.     }
  30.  
  31.     @Provides
  32.     @Singleton
  33.     FitAPI provideApi(Retrofit retrofit) {
  34.         return retrofit.create(FitAPI.class);
  35.     }
  36.  
  37.     @Provides
  38.     @Singleton
  39.     public ApiController provideApiController(FitAPI fitAPI) {
  40.         return new ApiController(fitAPI);
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement