From ceca6da5c02d3ee19f142a8dc89f2810258d1879 Mon Sep 17 00:00:00 2001 From: Jawnnypoo Date: Thu, 18 Aug 2016 11:25:43 -0500 Subject: [PATCH] Change structure up a bit to be more of a builder rather than the constructor building --- README.md | 12 ++++---- .../easycallback/sample/MainActivity.java | 3 +- .../commit451/easycallback/EasyCallback.java | 18 ++++++++++- .../easycallback/EasyOkCallback.java | 30 +++++++++++++++---- 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index ec77d1b..45cad4a 100644 --- a/README.md +++ b/README.md @@ -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() { @Override @@ -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 -------- diff --git a/app/src/main/java/com/commit451/easycallback/sample/MainActivity.java b/app/src/main/java/com/commit451/easycallback/sample/MainActivity.java index 1e2ba87..9efcc8b 100644 --- a/app/src/main/java/com/commit451/easycallback/sample/MainActivity.java +++ b/app/src/main/java/com/commit451/easycallback/sample/MainActivity.java @@ -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)); } }); } diff --git a/easycallback/src/main/java/com/commit451/easycallback/EasyCallback.java b/easycallback/src/main/java/com/commit451/easycallback/EasyCallback.java index 5244efd..969fd9b 100644 --- a/easycallback/src/main/java/com/commit451/easycallback/EasyCallback.java +++ b/easycallback/src/main/java/com/commit451/easycallback/EasyCallback.java @@ -20,6 +20,12 @@ public abstract class EasyCallback implements Callback { private Response mResponse; private Call 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 @@ -32,6 +38,16 @@ public abstract class EasyCallback implements Callback { */ 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 allowNullBodies(boolean allowNullBodies) { + mAllowNullBodies = allowNullBodies; + return this; + } + @Override public void onResponse(Call call, Response response) { mCall = call; @@ -40,7 +56,7 @@ public void onResponse(Call call, Response response) { failure(new HttpException(response.code(), response.errorBody())); return; } - if (response.body() == null) { + if (response.body() == null && !mAllowNullBodies) { failure(new NullBodyException()); return; } diff --git a/easycallback/src/main/java/com/commit451/easycallback/EasyOkCallback.java b/easycallback/src/main/java/com/commit451/easycallback/EasyOkCallback.java index de44e74..ed5a6bc 100644 --- a/easycallback/src/main/java/com/commit451/easycallback/EasyOkCallback.java +++ b/easycallback/src/main/java/com/commit451/easycallback/EasyOkCallback.java @@ -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; } /** @@ -50,6 +48,26 @@ 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; @@ -57,7 +75,7 @@ public void onResponse(Call call, Response response) throws IOException { callFailure(new HttpException(response.code(), response.body())); return; } - if (response.body() == null) { + if (response.body() == null && !mAllowNullBodies) { callFailure(new NullBodyException()); return; }