Skip to content

Instantly share code, notes, and snippets.

@cdmunoz
Created September 3, 2020 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdmunoz/58de305960fd3423044eff2840fe710f to your computer and use it in GitHub Desktop.
Save cdmunoz/58de305960fd3423044eff2840fe710f to your computer and use it in GitHub Desktop.
Koin's network module
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