How to make a non-rx network call, which depends on the rx network call

I have a network call that returns Observable, and I have another network call that is not rx, which depends on the first Observable, and I need to somehow convert it all with Rx.

Observable<Response> responseObservable = apiclient.executeRequest(request);

After execution, I need to make another http call that does not return Observable:

responseObservable.map(response - > execute the no rx network call using the response.id) 

noRxClient.getInformation(response.id, new Action1<Information>() {
    @Override
    public void call(Information information) {
        //Need to return information with page response
    }
});

After that I need to call this method to display the answer

renderResponse(response, information);

How can I connect a non-rx call using rx and then trigger the rendering response all using RxJava?

+4
source share
1 answer

rx Observable Observable.fromEmitter (RxJava1) Observable.create (RxJava2) Observable.fromCallable ( ):

private Observable<Information> wrapGetInformation(String responseId) {
    return Observable.create(emitter -> {
        noRxClient.getInformation(responseId, new Action1<Information>() {
            @Override
            public void call(Information information) {
                emitter.onNext(information);
                emitter.onComplete();
                //also wrap exceptions into emitter.onError(Throwable)
            }
        });
    });
}

private Observalbe<RenderedResponse> wrapRenderResponse(Response response, Information information) {
    return Observable.fromCallable(() -> {
        return renderResponse(response, information);
        //exceptions automatically wrapped
    });
}

flatMap:

apiclient.executeRequest(request)
    .flatMap(response -> wrapGetInformation(response.id), 
            (response, information) -> wrapRenderResponse(response, information))
    )
    //apply Schedulers
    .subscribe(...)
+2

Source: https://habr.com/ru/post/1669755/


All Articles