Skip to content

Instantly share code, notes, and snippets.

@harmittaa
Last active January 13, 2021 10:30
Show Gist options
  • Save harmittaa/3c8c882795e55208082e4e995b7e1a3b to your computer and use it in GitHub Desktop.
Save harmittaa/3c8c882795e55208082e4e995b7e1a3b to your computer and use it in GitHub Desktop.
Koin 2.0 Retrofit example
import com.github.harmittaa.koinexample.BuildConfig
import okhttp3.OkHttpClient
import org.koin.dsl.module
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
val networkModule = module {
factory { AuthInterceptor() }
factory { provideOkHttpClient(get()) }
factory { provideForecastApi(get()) }
single { provideRetrofit(get()) }
}
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
return Retrofit.Builder().baseUrl(BuildConfig.API_URL).client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create()).build()
}
fun provideOkHttpClient(authInterceptor: AuthInterceptor): OkHttpClient {
return OkHttpClient().newBuilder().addInterceptor(authInterceptor).build()
}
fun provideForecastApi(retrofit: Retrofit): WeatherForecastApi = retrofit.create(WeatherForecastApi::class.java)
@rid-x
Copy link

rid-x commented Jan 13, 2021

Hi. I think it will be right to use WeatherApi instead of WeatherForecastApi because in your medium post you are created WeatherApi interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment