Last active
January 13, 2021 10:30
-
-
Save harmittaa/3c8c882795e55208082e4e995b7e1a3b to your computer and use it in GitHub Desktop.
Koin 2.0 Retrofit example
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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi. I think it will be right to use
WeatherApi
instead ofWeatherForecastApi
because in your medium post you are createdWeatherApi
interface.