Skip to content

Instantly share code, notes, and snippets.

@ch8n
Created September 22, 2020 04:38
Show Gist options
  • Save ch8n/b3ea4a213ae6af2a22287f719029d8ac to your computer and use it in GitHub Desktop.
Save ch8n/b3ea4a213ae6af2a22287f719029d8ac to your computer and use it in GitHub Desktop.
for medium article
object NetworkResolver {
// step 1, you first need to resolve Api Manager
fun provideApiManager() = ApiManager(??)
// step 2 Api manager requires retrofit Instance, provide it
// from parameter dependency
fun provideApiManager(retrofit: Retrofit) = ApiManager(retrofit)
// step 3 We haven't created a resolving function that provides
// a retrofit instance, hence
fun provideRetrofitClient() : Retrofit{
return Retrofit.Builder()
.baseUrl(??)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(??)
.build()
}
// step 4 dependencies for retrofit can be passed from parameter
fun provideRetrofitClient(baseURl:String, okHttpClient : OkHttpClient) : Retrofit{
return Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build()
}
// step 5, similarly add okhttp and baseUrl
fun provideBaseURl():String = "http://www.someURL.com"
fun provideOkHttpClient() : OkHttpClient = OkHttpClient
.Builder()
.build()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment