Last active
September 6, 2017 17:43
-
-
Save caseykulm/22cb766ee8d783d92b461432d1c75d92 to your computer and use it in GitHub Desktop.
Abstract Retrofit from your API
This file contains hidden or 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 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