Friday, 13 July 2018

generalize way to handle response and error for all webservice calls

I am trying to create generlize approach to handle following

  1. response from webservice
  2. to handle HTTP error and custom errors

for that i have written following code and crated callbacks to handle response ResponseCallback and errors ErrorCallback.

now to fetch data related to nutrition or any other objects i can use the same callbacks.

    Request request = new Request();
    request.setAccess_token("d80fa6bd6f78cc704104d61146c599bc94b82ca225349ee68762fc6c70d2dcf0");
    request.setStart_date("2018-06-01T00:00:00");
    Flowable<Response> fitnessFlowable = new WebRequest()
            .getRemoteClient()
            .create(FitnessApi.class)
            .getFitnessData("5b238abb4d3590001d9b94a8",request.toMap());

      fitnessFlowable.subscribeOn(Schedulers.io())
            .takeUntil(response->response.getSummary().getNext()!=null)
            .doOnNext(new ResponseCallback())
            .doOnError(new ErrorCallback<Throwable>())
            .subscribe();

Response callback

public  class ResponseCallback<T extends Response> implements Consumer<Response> {


    @Override
    public void accept(Response response) throws Exception {
        if(response ==null || response.getFitness() == null || response.getFitness().isEmpty()) {


            new Exception("NUll response");

        }

        RxBus.getInstance().send(response.getFitness());
       Log.e("Data","accept");
        try(Realm r = Realm.getDefaultInstance()) {
            r.executeTransaction((realm) -> {
                realm.copyToRealmOrUpdate(response.getFitness());
            });
        }
    }
}

Error callback

public class ErrorCallback <T extends Throwable> implements Consumer<Throwable> {

    public static final String TAG = "ErrorCallback";
    @Override
    public void accept(Throwable e) throws Exception {

        if (e instanceof HttpException) {
            ResponseBody body = ((HttpException) e).response().errorBody();
            try {
                Response response=  LoganSquare.parse(body.byteStream(),Response.class);

                if(response.getErrors() !=null)
                    if(response.getErrors().size() > 0)
                      Log.e(TAG , response.getErrors().get(0).getErrors() );

            } catch (IOException t) {
                t.printStackTrace();
            }

        } else if (e instanceof SocketTimeoutException) {

            Log.e(TAG , e.getMessage() );

        }else if (e instanceof IOException) {

            Log.e(TAG , e.getMessage() );
        }else{
            Log.e(TAG , e.getMessage() );
        }
    }
}

Two problems here i am trying to address

  1. To handle response and error i need to make two separate callbacks. is there any other way where i can handle response and error in callback rather than creating two separate one

  2. When should i dispose the subscribe in above code because you can see in my code that my subscribe is hagging.

i have refereed this link to design generalize approach.



from generalize way to handle response and error for all webservice calls

No comments:

Post a Comment