Skip to content

Instantly share code, notes, and snippets.

@caseykulm
Last active September 6, 2017 17:43
Show Gist options
  • Save caseykulm/22cb766ee8d783d92b461432d1c75d92 to your computer and use it in GitHub Desktop.
Save caseykulm/22cb766ee8d783d92b461432d1c75d92 to your computer and use it in GitHub Desktop.
Abstract Retrofit from your API
interface RetroDogApi {
@GET("/dogs/{doggoName}")
Flowable<Dog> getDoggo(@Path("doggoName") String doggoName);
}
interface DogApi {
Flowable<Dog> getDoggo(String doggoName);
}
class DogClient(val urlA: String, val urlB: String): DogApi {
private val retroDogApiA: RetroDogApi
private val retroDogApiB: RetroDogApi
init {
val retrofitA = Retrofit.Builder()
.baseUrl(urlA)
.build()
retroDogApiA = retrofit.create(RetroDogApi::class.java)
val retrofitB = Retrofit.Builder()
.baseUrl(urlB)
.build()
retroDogApiB = retrofit.create(RetroDogApi::class.java)
}
Flowable<Dog> getDoggo(String doggoName) {
if (shouldUseApiA()) {
return retroDogApiA.getDoggo(doggoName)
} else {
return retroDogApiB.getDoggo(doggoName);
}
}
fun shouldUseApiA(): boolean {
// logic here
}
}
class UseIt {
fun use() {
val dogApi = DogClient("https://doggo.dogs/servicea", "https://doggo.dogs/serviceb")
dogApi.getDoggo("fido")
.subscribeOn(...)
.observeOn(...)
.subscribe(...)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment