Skip to content

Commit

Permalink
Change structure up a bit to be more of a builder rather than the con…
Browse files Browse the repository at this point in the history
…structor building
  • Loading branch information
Jawnnypoo committed Aug 18, 2016
1 parent dd5f605 commit ceca6da
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# EasyCallback
Easier Retrofit callbacks
Easier Retrofit and OkHttp callbacks

[![Build Status](https://travis-ci.org/Commit451/EasyCallback.svg?branch=master)](https://travis-ci.org/Commit451/EasyCallback)
[![](https://jitpack.io/v/Commit451/EasyCallback.svg)](https://jitpack.io/#Commit451/EasyCallback)

Many times when using Retrofit, you would probably want your `!isSuccessful()` responses to just fall through into your failure case. That is what this do. It is very reminiscent of Retrofit 1.X callback days
Many times when using Retrofit or OkHttp, you would probably want your `!isSuccessful()` responses to just fall through into your failure case. That is what this does for you. It is very reminiscent of Retrofit 1.X callback days.

# Usage
Usage is simple:
Usage is simple and similar to regular Retrofit Callbacks:
```java
api.getUsers().enqueue(new EasyCallback<ResponseBody>() {
@Override
Expand Down Expand Up @@ -41,10 +41,12 @@ public void failure(Throwable t) {
}
}
```
There is also `EasyOkCallback` which is an OkHttp specific flavor of the Callback which also checks `isSuccessful()` and will also by default post the result on the main thread for simplicity
You can also still retrieve information about the call with things like `getCall()` or `getResponse()` if needed.

`EasyOkCallback` is an OkHttp specific flavor of the Callback which also checks `isSuccessful()` and will also by default post the result on the main thread for simplicity.

# Note
If your API happens to return a `200` code, but contains an empty body, this will fall through to the `failure` block due to the fact that we check for `null` in the `onResponse` and redirect to `failure` if the response is null.
If your API happens to return a `200` code, but contains an empty body, this will fall through to the `failure` block due to the fact that we check for `null` in the `onResponse` and redirect to `failure` if the response is null. You can work around this by calling `allowNullBodies(true)` on the callback.

License
--------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public void success(@NonNull Response response) {
public void failure(Throwable t) {
Toast.makeText(MainActivity.this, "OkHttp error!", Toast.LENGTH_SHORT).show();
}
});
//Just to show you that you can do this if you really need to
}.allowNullBodies(true));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public abstract class EasyCallback<T> implements Callback<T> {
private Response<T> mResponse;
private Call<T> mCall;

private boolean mAllowNullBodies;

public EasyCallback() {
mAllowNullBodies = false;
}

/**
* Called on success. You can still get the original {@link Response} via {@link #getResponse()}
* @param response the response
Expand All @@ -32,6 +38,16 @@ public abstract class EasyCallback<T> implements Callback<T> {
*/
public abstract void failure(Throwable t);

/**
* Allows specification of if you want the callback to consider null bodies as a {@link NullBodyException}. Default is false
* @param allowNullBodies true if you want to allow null bodies, false if you want exceptions throw on null bodies
* @return this
*/
public EasyCallback<T> allowNullBodies(boolean allowNullBodies) {
mAllowNullBodies = allowNullBodies;
return this;
}

@Override
public void onResponse(Call<T> call, Response<T> response) {
mCall = call;
Expand All @@ -40,7 +56,7 @@ public void onResponse(Call<T> call, Response<T> response) {
failure(new HttpException(response.code(), response.errorBody()));
return;
}
if (response.body() == null) {
if (response.body() == null && !mAllowNullBodies) {
failure(new NullBodyException());
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ private static Handler getMainHandler() {

private Call mCall;
private boolean mCallbackOnMainThread;
private boolean mAllowNullBodies;

public EasyOkCallback() {
this(true);
}

public EasyOkCallback(boolean callbackOnMainThread) {
mCallbackOnMainThread = callbackOnMainThread;
mCallbackOnMainThread = true;
mAllowNullBodies = false;
}

/**
Expand All @@ -50,14 +48,34 @@ public EasyOkCallback(boolean callbackOnMainThread) {
*/
public abstract void failure(Throwable t);

/**
* Allows specification of if you want the callback on the main thread. Default is true
* @param callbackOnMainThread true if you want it on the main thread, false if you want it on the background thread
* @return this
*/
public EasyOkCallback callbackOnMainThread(boolean callbackOnMainThread) {
mCallbackOnMainThread = callbackOnMainThread;
return this;
}

/**
* Allows specification of if you want the callback to consider null bodies as a {@link NullBodyException}. Default is false
* @param allowNullBodies true if you want to allow null bodies, false if you want exceptions throw on null bodies
* @return this
*/
public EasyOkCallback allowNullBodies(boolean allowNullBodies) {
mAllowNullBodies = allowNullBodies;
return this;
}

@Override
public void onResponse(Call call, Response response) throws IOException {
mCall = call;
if (!response.isSuccessful()) {
callFailure(new HttpException(response.code(), response.body()));
return;
}
if (response.body() == null) {
if (response.body() == null && !mAllowNullBodies) {
callFailure(new NullBodyException());
return;
}
Expand Down

0 comments on commit ceca6da

Please sign in to comment.