Skip to content

Instantly share code, notes, and snippets.

@varundwarkani
Created April 26, 2021 08:00
Show Gist options
  • Save varundwarkani/8a9613fdb20ce21b521102e032a0fb42 to your computer and use it in GitHub Desktop.
Save varundwarkani/8a9613fdb20ce21b521102e032a0fb42 to your computer and use it in GitHub Desktop.
@Module
@InstallIn(SingletonComponent.class)
public class NetworkModule {
@Provides
@Singleton
public static ApiService provideApiInterface() {
Retrofit retrofit = getRetrofit(getOkHttpClient());
return retrofit.create(ApiService.class);
}
@Provides
@Singleton
static Retrofit getRetrofit(OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.baseUrl(APIConstants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build();
}
@Provides
@Singleton
static OkHttpClient getOkHttpClient() {
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
okHttpClientBuilder.connectTimeout(APIConstants.CONNECT_TIMEOUT, TimeUnit.MILLISECONDS);
okHttpClientBuilder.readTimeout(APIConstants.READ_TIMEOUT, TimeUnit.MILLISECONDS);
okHttpClientBuilder.writeTimeout(APIConstants.WRITE_TIMEOUT, TimeUnit.MILLISECONDS);
okHttpClientBuilder.addInterceptor(new RequestInterceptor());
okHttpClientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
return okHttpClientBuilder.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment