Skip to content

Commit

Permalink
allow request cancellation (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahsnow1 authored Nov 29, 2017
1 parent eea8ccd commit 61773e3
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 18 deletions.
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ dependencies {
implementation project(':refill')
implementation project(':walkabout')
api "com.mapzen.tangram:tangram:$tangram_version"
api 'com.mapzen.android:pelias-android-sdk:1.2.0'
api 'com.mapzen.android:pelias-android-sdk:1.2.1'
api 'com.mapzen.android:lost:3.0.3'

api 'com.google.dagger:dagger:2.0'
Expand Down
27 changes: 27 additions & 0 deletions core/src/main/java/com/mapzen/android/core/http/CallRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mapzen.android.core.http;

import retrofit2.Call;

/**
* Default {@link Request} implementation.
*/
public class CallRequest implements Request {

private Call call;

/**
* Public constructor.
* @param call
*/
public CallRequest(Call call) {
this.call = call;
}

@Override public boolean isCanceled() {
return call.isCanceled();
}

@Override public void cancel() {
call.cancel();
}
}
15 changes: 15 additions & 0 deletions core/src/main/java/com/mapzen/android/core/http/Request.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mapzen.android.core.http;

/**
* Generic interface for objects abstracting http requests.
*/
public interface Request {
/**
* Returns whether or not the request has been canceled.
*/
boolean isCanceled();
/**
* Cancels the request.
*/
void cancel();
}
30 changes: 16 additions & 14 deletions core/src/main/java/com/mapzen/android/search/MapzenSearch.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mapzen.android.search;

import com.mapzen.android.core.CoreDI;
import com.mapzen.android.core.http.CallRequest;
import com.mapzen.android.core.http.Request;
import com.mapzen.pelias.BoundingBox;
import com.mapzen.pelias.Pelias;
import com.mapzen.pelias.PeliasLocationProvider;
Expand Down Expand Up @@ -52,8 +54,8 @@ private void initDI(Context context) {
* @param query
* @param callback
*/
public void suggest(String query, Callback<Result> callback) {
internalSearch.suggest(query, callback);
public Request suggest(String query, Callback<Result> callback) {
return new CallRequest(internalSearch.suggest(query, callback));
}

/**
Expand All @@ -62,8 +64,8 @@ public void suggest(String query, Callback<Result> callback) {
* @param query
* @param callback
*/
public void suggest(String query, double lat, double lon, Callback<Result> callback) {
internalSearch.suggest(query, lat, lon, callback);
public Request suggest(String query, double lat, double lon, Callback<Result> callback) {
return new CallRequest(internalSearch.suggest(query, lat, lon, callback));
}

/**
Expand All @@ -73,8 +75,8 @@ public void suggest(String query, double lat, double lon, Callback<Result> callb
* @param query
* @param callback
*/
public void search(String query, Callback<Result> callback) {
internalSearch.search(query, callback);
public Request search(String query, Callback<Result> callback) {
return new CallRequest(internalSearch.search(query, callback));
}

/**
Expand All @@ -84,8 +86,8 @@ public void search(String query, Callback<Result> callback) {
* @param query
* @param callback
*/
public void search(String query, BoundingBox box, Callback<Result> callback) {
internalSearch.search(query, box, callback);
public Request search(String query, BoundingBox box, Callback<Result> callback) {
return new CallRequest(internalSearch.search(query, box, callback));
}

/**
Expand All @@ -95,8 +97,8 @@ public void search(String query, BoundingBox box, Callback<Result> callback) {
* @param query
* @param callback
*/
public void search(String query, double lat, double lon, Callback<Result> callback) {
internalSearch.search(query, lat, lon, callback);
public Request search(String query, double lat, double lon, Callback<Result> callback) {
return new CallRequest(internalSearch.search(query, lat, lon, callback));
}

/**
Expand All @@ -106,8 +108,8 @@ public void search(String query, double lat, double lon, Callback<Result> callba
* @param lon
* @param callback
*/
public void reverse(double lat, double lon, Callback<Result> callback) {
internalSearch.reverse(lat, lon, callback);
public Request reverse(double lat, double lon, Callback<Result> callback) {
return new CallRequest(internalSearch.reverse(lat, lon, callback));
}

/**
Expand All @@ -116,8 +118,8 @@ public void reverse(double lat, double lon, Callback<Result> callback) {
* @param gid
* @param callback
*/
public void place(String gid, Callback<Result> callback) {
internalSearch.place(gid, callback);
public Request place(String gid, Callback<Result> callback) {
return new CallRequest(internalSearch.place(gid, callback));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mapzen.android.core.http;

import org.junit.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import retrofit2.Call;

public class CallRequestTest {

Call call = mock(Call.class);
CallRequest request = new CallRequest(call);

@Test
public void cancel_shouldCancelCall() throws Exception {
request.cancel();
verify(call).cancel();
}

@Test
public void isCanceled_shouldCallCall() throws Exception {
request.isCanceled();
verify(call).isCanceled();
}
}
2 changes: 1 addition & 1 deletion mapzen-android-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ task checkstyle(type: Checkstyle) {

dependencies {
api project(':core')
api ('com.mapzen:on-the-road:1.3.0') {
api ('com.mapzen:on-the-road:1.3.1') {
exclude group: 'com.squareup.retrofit2', module: 'converter-gson'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mapzen.android.routing;

import com.mapzen.android.core.DI;
import com.mapzen.android.core.http.CallRequest;
import com.mapzen.android.core.http.Request;
import com.mapzen.valhalla.RouteCallback;
import com.mapzen.valhalla.Router;
import com.mapzen.valhalla.ValhallaRouter;
Expand Down Expand Up @@ -37,8 +39,8 @@ private void initDI(Context context) {
/**
* Fetch a route for the given configuration.
*/
public void fetch() {
internalRouter.fetch();
public Request fetch() {
return new CallRequest(internalRouter.fetch());
}

/**
Expand Down

0 comments on commit 61773e3

Please sign in to comment.