Advertisement
challme28

Observables

May 25th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. @Override
  2.     public Observable<ResponseHttp<User>> loginUser
  3.             (final request.Login loginRequest) {
  4.         String email = loginRequest.getEmail();
  5.         String password = loginRequest.getPassword();
  6.         final String pushBotsToken = loginRequest.getPushBotsToken();
  7.         return service.loginUser(email, password)
  8.                 .subscribeOn(Schedulers.newThread())
  9.                 .observeOn(AndroidSchedulers.mainThread())
  10.                 .flatMap(new Function<LoginUser, ObservableSource<ResponseHttp<User>>>() {
  11.                     @Override
  12.                     public ObservableSource<ResponseHttp<User>> apply(@NonNull final LoginUser loginUser)
  13.                             throws Exception {
  14.                         Log.d(TAG, "apply: loginUser "+ loginUser.getId());
  15.                         return service.associatePushBotsO(pushBotsToken)
  16.                                 .subscribeOn(Schedulers.newThread())
  17.                                 .observeOn(AndroidSchedulers.mainThread())
  18.                                 .map(new Function<ResponseBody, ResponseHttp<User>>() {
  19.                                     @Override
  20.                                     public ResponseHttp<User> apply(@NonNull ResponseBody responseBody) throws Exception {
  21.                                         Log.d(TAG, "associatePushBots apply: map ");
  22.                                         return new ResponseHttp<>(200, new Login(loginUser).getOriginalObject());
  23.                                     }
  24.                                 });
  25.                     }
  26.                 })
  27.                 .onErrorReturn(new Function<Throwable, ResponseHttp<User>>() {
  28.                     @Override
  29.                     public ResponseHttp<User> apply(@NonNull Throwable throwable) throws Exception {
  30.                         ResponseHttp<User> res;
  31.                         if (throwable instanceof HttpException) {
  32.                             HttpException exception = (HttpException) throwable;
  33.                             res = new ResponseHttp<>(exception.response().code());
  34.                             if (res.code / 100 == 5) {
  35.                                 throw new ServerError("Algo salió mal", res.code);
  36.                             }
  37.                         } else {
  38.                             throw new Exception(throwable);
  39.                         }
  40.                         return res;
  41.                     }
  42.                 });
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement