Created
May 7, 2021 18:50
-
-
Save varundwarkani/1f19ea1202b332132c7ee51ae7102d02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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