Created
November 9, 2015 18:15
-
-
Save jakewilson801/a9b0def03cca8a1fd54c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static <T,U> Observable<Response<T>> createRequest(U data, Func1<U, Call<T>> endpoint){ | |
| return Observable.create(new Observable.OnSubscribe<Response<T>>() { | |
| @Override | |
| public void call(Subscriber<? super Response<T>> subscriber) { | |
| if (isThereInternetConnection()) { | |
| Call<T> request = endpoint.call(data); | |
| try { | |
| Response<T> response = request.execute(); | |
| int code = response.code(); | |
| if (response.isSuccess()) { | |
| subscriber.onNext(response); | |
| subscriber.onCompleted(); | |
| } else if (code >= 400 && code < 500) { | |
| subscriber.onError(new BadRequestException(response.errorBody() | |
| .string())); | |
| } else { | |
| subscriber.onError(new NetworkConnectionException()); | |
| } | |
| } catch (Exception e) { | |
| subscriber.onError(new NetworkConnectionException()); | |
| } | |
| } else { | |
| subscriber.onError(new NetworkConnectionException()); | |
| } | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment