Nested requests example multiple requests combine results
suggest changeSuppose we have an API which allows us to get object metadata in a single request (getAllPets), and another request which has the full data for a single resource (getSinglePet). How can we query all of them in a single chain? petApi is not created on this page.
public class PetsFetcher {
static class PetRepository {
List<Integer> ids;
}
static class Pet {
int id;
String name;
int weight;
int height;
}
interface PetApi {
@GET("pets") Observable<PetRepository> getAllPets();
@GET("pet/{id}") Observable<Pet> getSinglePet(@Path("id") int id);
}
PetApi petApi;
Disposable petsDisposable;
public void requestAllPets() {
petApi.getAllPets()
.doOnSubscribe(new Consumer<Disposable>() {
@Override public void accept(Disposable disposable) throws Exception {
petsDisposable = disposable;
}
})
.flatMap(new Function<PetRepository, ObservableSource<Integer>>() {
@Override
public ObservableSource<Integer> apply(PetRepository petRepository) throws Exception {
List<Integer> petIds = petRepository.ids;
return Observable.fromIterable(petIds);
}
})
.flatMap(new Function<Integer, ObservableSource<Pet>>() {
@Override public ObservableSource<Pet> apply(Integer id) throws Exception {
return petApi.getSinglePet(id);
}
})
.toList()
.toObservable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<List<Pet>>() {
@Override public void accept(List<Pet> pets) throws Exception {
//use your pets here
}
}, new Consumer<Throwable>() {
@Override public void accept(Throwable throwable) throws Exception {
//show the user that something went wrong
}
});
}
void cancelRequests(){
if (petsDisposable!=null){
petsDisposable.dispose();
petsDisposable = null;
}
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents