Reputation: 424
I have followed THIS tutorial for MVVM and Retrofit2 with Kodein structure/framework. I wanted to know, Using this same framework/structure how can i have multiple base URLs in a single App.
Below is the code of "MyApi" interface which have an interceptor class as parameter.
companion object {
operator fun invoke(
networkConnectionInterceptor: NetworkConnectionInterceptor
): MyApi {
val okkHttpclient = OkHttpClient.Builder()
.addInterceptor(networkConnectionInterceptor)
.readTimeout(20, TimeUnit.SECONDS)
.build()
return Retrofit.Builder()
.client(okkHttpclient)
.baseUrl("http://my-base-url")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(MyApi::class.java)
}
}
And here is how Iam initializing MyApi interface in Application class:
bind() from singleton { PreferenceProvider(instance()) }
bind() from singleton { NetworkConnectionInterceptor(instance(), instance()) }
bind() from singleton { MyApi(instance()) }
Here instance() in MyApi is obviously NetworkConnectionInterceptor.
I have seen a lot of examples on stackoverflow and medium but i didnt got any help.
Upvotes: 1
Views: 1869
Reputation: 424
I think i found a work around to achive this. There are two solutions...
First Solution:
Pros:
Cons:
development
and qa
server providing an option for testers and developers to switch between qa and dev servers at run time the you need to have 4 interfaces and 2 extra for production
that means 6 interfaces, It will become very had to manage it.Second Solution:
@URL
annotation provided by retrofit2. Now if you do this there will be no base_url, you have to pass the URL and microservice name in a common function which will return you a full URL based on what server users/testers are on (dev/qa or prod).Pros:
Cons:
@URL
annotation in each and every API call.Upvotes: 1