Skip to content

Instantly share code, notes, and snippets.

@alibahaaa
Created January 18, 2023 14:13
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 alibahaaa/d34ed435cf9e274bf190cfb47b043ac1 to your computer and use it in GitHub Desktop.
Save alibahaaa/d34ed435cf9e274bf190cfb47b043ac1 to your computer and use it in GitHub Desktop.
interface RetrofitApi {
fun create(): Retrofit
}
class ProductionRetrofitApi : RetrofitApi {
override fun create(): Retrofit {
val retrofit = Retrofit.Builder()
.baseUrl("https://production-api.com")
.addConverterFactory(GsonConverterFactory.create())
.build()
return retrofit
}
}
class StagingRetrofitApi : RetrofitApi {
override fun create(): Retrofit {
val retrofit = Retrofit.Builder()
.baseUrl("https://staging-api.com")
.addConverterFactory(GsonConverterFactory.create())
.build()
return retrofit
}
}
class RetrofitApiFactory {
companion object {
fun createRetrofitApi(apiType: String): RetrofitApi? {
return when (apiType) {
"production" -> ProductionRetrofitApi()
"staging" -> StagingRetrofitApi()
else -> null
}
}
}
}
val productionApi = RetrofitApiFactory.createRetrofitApi("production")?.create()
val stagingApi = RetrofitApiFactory.createRetrofitApi("staging")?.create()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment