Created
January 18, 2023 14:13
-
-
Save alibahaaa/d34ed435cf9e274bf190cfb47b043ac1 to your computer and use it in GitHub Desktop.
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
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