Created
September 3, 2020 23:34
-
-
Save cdmunoz/58de305960fd3423044eff2840fe710f to your computer and use it in GitHub Desktop.
Koin's network module
This file contains hidden or 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
val networkModule = module { | |
single { provideOkHttpClient() } | |
single { provideRetrofit(get()) } | |
single { provideApiService(get(), ApiService::class.java) } | |
} | |
fun provideOkHttpClient(): OkHttpClient = OkHttpClient.Builder().build() | |
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit = | |
Retrofit.Builder().baseUrl(BuildConfig.BASE_URL) | |
.addConverterFactory(GsonConverterFactory.create()).client(okHttpClient).build() | |
fun provideApiService(retrofit: Retrofit, apiService: Class<ApiService>) = | |
createService(retrofit, apiService) | |
fun <T> createService(retrofit: Retrofit, serviceClass: Class<T>): T = retrofit.create(serviceClass) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment