Skip to content

Instantly share code, notes, and snippets.

@varundwarkani
Created May 7, 2021 18:50
Show Gist options
  • Save varundwarkani/1f19ea1202b332132c7ee51ae7102d02 to your computer and use it in GitHub Desktop.
Save varundwarkani/1f19ea1202b332132c7ee51ae7102d02 to your computer and use it in GitHub Desktop.
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@JvmStatic
@Provides
@Singleton
fun provideApiInterface(): ApiService {
val retrofit = getRetrofit(okHttpClient)
return retrofit.create(ApiService::class.java)
}
@JvmStatic
@Provides
@Singleton
fun getRetrofit(okHttpClient: OkHttpClient?): Retrofit {
return Retrofit.Builder()
.baseUrl(APIConstants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient!!)
.build()
}
@JvmStatic
@get:Singleton
@get:Provides
val okHttpClient: OkHttpClient
get() {
val okHttpClientBuilder = 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(RequestInterceptor())
okHttpClientBuilder.addInterceptor(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