خرید بک لینک

Vote count: 0

I'm retrieving film information from two API.

  • The first API (Api1 class) receives an id and returns a JSON (Api1Dto class):

{ id : "123456", title : "Top Secret", api2Id : "7787" }

  • The second API (Api2 class) receives an api2id and returns a JSON (Api2Dto class):

{api2Id : "58582", "director" : "Peter Jackson" }

I want, from an id, compose a structure with all those fields (Film class):

{ id: "123456", title : "Muahaha", api2Id : "5552", director : "Uncle Joe" }

I have this code:

 public Observable<Film> getById(final String id) {
        return api1.getById(id).map(new Func1<Api1Dto, Film>() {
            @Override
            public Film call(Api1Dto dto1) {
                Film film = new Film();
                film.id = dto1.id;
                film.title = dto1.title;
                film.api2Id = dto1.api2Id;
                return film;
            }
        }).flatMap(new Func1<Film, Observable<Film>>() {
            @Override
            public Observable<Film> call(final Film film) {
                return api2.getById(film.api2Id).map(new Func1<Api2Dto, Film>() {
                    @Override
                    public Film call(Api2Dto dto2) {
                        film.director = dto2.director;
                        return film;
                    }
                });
            }
        });
    }

It works, but I wonder if this is the correct way to do it or exists a better/more elegant solution using functions that RxJava provides.

asked 21 secs ago

برچسب: نویسنده: استخدام کار تاريخ: سه شنبه 19 مرداد 1395 ساعت: 17:29

صفحه بندی