-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Polling and Converter.Factory customization support (#10)
* Allow customization of converter factories in RetrofitServices * Polling mechanisms * Remove unnecesary Manifest stuff * Remove unnecesary string * Add comment as to why Timer is fine * Update Wolmo version to v1.3.1
- Loading branch information
1 parent
fd4efaf
commit 59cc759
Showing
6 changed files
with
137 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="ar.com.wolox.wolmo.networking"> | ||
<manifest | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="ar.com.wolox.wolmo.networking"> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:label="@string/app_name" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"/> | ||
android:supportsRtl="true"/> | ||
|
||
</manifest> |
17 changes: 17 additions & 0 deletions
17
...ing/src/main/java/ar/com/wolox/wolmo/networking/exception/PollRunOutOfTriesException.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package ar.com.wolox.wolmo.networking.exception; | ||
|
||
import android.support.annotation.NonNull; | ||
|
||
import retrofit2.Call; | ||
|
||
/** | ||
* {@link Exception} raised when polling an endpoint, for a maximum amount of tries, consumes all | ||
* of its tries available (ie: reaches 0). | ||
*/ | ||
public final class PollRunOutOfTriesException extends RuntimeException { | ||
|
||
public PollRunOutOfTriesException(@NonNull Call call) { | ||
super("Polling to " + call.request().url().toString() + " failed after running out of tries"); | ||
} | ||
|
||
} |
This file contains 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
81 changes: 81 additions & 0 deletions
81
networking/src/main/java/ar/com/wolox/wolmo/networking/utils/CallUtils.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package ar.com.wolox.wolmo.networking.utils; | ||
|
||
import android.support.annotation.IntRange; | ||
import android.support.annotation.NonNull; | ||
|
||
import com.android.internal.util.Predicate; | ||
|
||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import ar.com.wolox.wolmo.networking.exception.PollRunOutOfTriesException; | ||
import retrofit2.Call; | ||
import retrofit2.Callback; | ||
import retrofit2.Response; | ||
|
||
/** | ||
* Common uses of Retrofit's {@link Call}, {@link Callback} and their interactions. | ||
*/ | ||
public class CallUtils { | ||
|
||
private CallUtils() {} | ||
|
||
/** | ||
* Polls with a delay in-between calls. | ||
* | ||
* @param tries amount of tries | ||
* @param call to poll | ||
* @param pollingCondition that dictates whether to keep polling | ||
* @param callback to be notified when polling ends | ||
* @param delay to apply in-between calls | ||
* @param timeoutUnit to convert delay | ||
* @return a {@link Timer} which is controlling the polling | ||
*/ | ||
public static <T> Timer pollWithDelay(@IntRange(from = 1) final int tries, | ||
@NonNull final Call<T> call, | ||
@NonNull final Predicate<Response<T>> pollingCondition, | ||
@NonNull final Callback<T> callback, | ||
@IntRange(from = 0) long delay, | ||
@NonNull TimeUnit timeoutUnit) { | ||
// Timer can be used safely since Retrofit callback is called on UiThread | ||
Timer pollingTimer = new Timer(); | ||
pollWithDelay(tries, call, pollingCondition, callback, | ||
timeoutUnit.toMillis(delay), pollingTimer); | ||
return pollingTimer; | ||
} | ||
|
||
private static <T> void pollWithDelay(@IntRange(from = 1) final int triesRemaining, | ||
@NonNull final Call<T> call, | ||
@NonNull final Predicate<Response<T>> pollingCondition, | ||
@NonNull final Callback<T> callback, | ||
@IntRange(from = 0) long delayInMillis, | ||
@NonNull final Timer pollingTimer) { | ||
if (triesRemaining <= 0) callback.onFailure(call, new PollRunOutOfTriesException(call)); | ||
|
||
call.enqueue(new Callback<T>() { | ||
@Override | ||
public void onResponse(Call<T> call, Response<T> response) { | ||
if (!pollingCondition.apply(response)) { | ||
callback.onResponse(call, response); | ||
return; | ||
} | ||
|
||
pollingTimer.schedule(new TimerTask() { | ||
@Override | ||
public void run() { | ||
pollWithDelay(triesRemaining - 1, call.clone(), | ||
pollingCondition, callback, delayInMillis, pollingTimer); | ||
} | ||
}, delayInMillis); | ||
} | ||
|
||
@Override | ||
public void onFailure(Call<T> call, Throwable t) { | ||
callback.onFailure(call, t); | ||
} | ||
}); | ||
} | ||
|
||
} |
This file contains 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