Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide top level methods to set custom http handlers #446

Merged
merged 1 commit into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/checkstyle/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
<suppress checks="ParameterNumberCheck" files="src/main/java/com/mapzen/android/graphics/MapReadyInitializer.java"/>
<suppress checks="ParameterNumberCheck" files="src/main/java/com/mapzen/android/graphics/model/MarkerManager.java"/>
<suppress checks="ParameterNumberCheck" files="src/main/java/com/mapzen/android/graphics/model/BitmapMarkerManager.java"/>
<suppress checks="[a-zA-Z0-9]*" files="src/main/java/com/mapzen/android/routing/MapzenRouterHttpHandler"/>
</suppressions>
59 changes: 59 additions & 0 deletions core/src/main/java/com/mapzen/android/core/GenericHttpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,65 @@ public interface GenericHttpHandler {
String HEADER_USER_AGENT = "User-Agent";
String USER_AGENT = "android-sdk;" + MapzenManager.getSdkVersion() + ";" + Build.VERSION.RELEASE;

/**
* Log levels for http requests.
*/
enum LogLevel {
/** No logs. */
NONE,
/**
* Logs request and response lines.
*
* <p>Example:
* <pre>{@code
* --> POST /greeting http/1.1 (3-byte body)
*
* <-- 200 OK (22ms, 6-byte body)
* }</pre>
*/
BASIC,
/**
* Logs request and response lines and their respective headers.
*
* <p>Example:
* <pre>{@code
* --> POST /greeting http/1.1
* Host: example.com
* Content-Type: plain/text
* Content-Length: 3
* --> END POST
*
* <-- 200 OK (22ms)
* Content-Type: plain/text
* Content-Length: 6
* <-- END HTTP
* }</pre>
*/
HEADERS,
/**
* Logs request and response lines and their respective headers and bodies (if present).
*
* <p>Example:
* <pre>{@code
* --> POST /greeting http/1.1
* Host: example.com
* Content-Type: plain/text
* Content-Length: 3
*
* Hi?
* --> END POST
*
* <-- 200 OK (22ms)
* Content-Type: plain/text
* Content-Length: 6
*
* Hello!
* <-- END HTTP
* }</pre>
*/
BODY
}

/**
* Return query parameters to be appended to every request.
* @return
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/com/mapzen/android/graphics/MapzenMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,14 @@ public void setOverlaysEnabled(boolean transitOverlayEnabled, boolean bikeOverla
mapController.updateSceneAsync(updates);
}

/**
* Sets the object used to add query parameters and headers to each request.
* @param handler
*/
public void setHttpHandler(MapzenMapHttpHandler handler) {
mapController.setHttpHandler(handler.httpHandler());
}

/**
* Restores all aspects of the map EXCEPT the style, this is restored in the
* {@link MapInitializer}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ public void setLocationProvider(PeliasLocationProvider locationProvider) {
internalSearch.setLocationProvider(locationProvider);
}

/**
* Sets the router's http handler for adding custom headers and parameters to
* requests.
* @param handler
*/
public void setHttpHandler(MapzenSearchHttpHandler handler) {
internalSearch.setRequestHandler(handler.searchHandler());
}

/**
* Return the underlying {@link Pelias} object.
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.mapzen.android.graphics.model.Polygon;
import com.mapzen.android.graphics.model.Polyline;
import com.mapzen.android.graphics.model.WalkaboutStyle;
import com.mapzen.tangram.HttpHandler;
import com.mapzen.tangram.LabelPickResult;
import com.mapzen.tangram.LngLat;
import com.mapzen.tangram.MapController;
Expand Down Expand Up @@ -709,6 +710,14 @@ public void applySceneUpdates_shouldClearQueuedUpdates() throws Exception {
verify(bitmapMarkerManager).restoreMarkers();
}

@Test public void setHttpHandler_shouldCallMapController() throws Exception {
MapzenMapHttpHandler mapzenMapHandler = mock(MapzenMapHttpHandler.class);
HttpHandler handler = mock(HttpHandler.class);
when(mapzenMapHandler.httpHandler()).thenReturn(handler);
map.setHttpHandler(mapzenMapHandler);
verify(mapController).setHttpHandler(handler);
}

public class TestRotateResponder implements TouchInput.RotateResponder {

boolean rotated = false;
Expand Down
10 changes: 10 additions & 0 deletions core/src/test/java/com/mapzen/android/search/MapzenSearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
Expand Down Expand Up @@ -85,6 +86,15 @@ public class MapzenSearchTest {
verify(search.getPelias()).setLocationProvider(provider);
}

@Test public void setHttpHandler_shouldCallInternalSearch() throws Exception {
MapzenSearchHttpHandler mapzenSearchHandler = mock(MapzenSearchHttpHandler.class);
MapzenSearchHttpHandler.SearchRequestHandler handler = mock(
MapzenSearchHttpHandler.SearchRequestHandler.class);
when(mapzenSearchHandler.searchHandler()).thenReturn(handler);
search.setHttpHandler(mapzenSearchHandler);
verify(search.getPelias()).setRequestHandler(handler);
}

private class TestCallback implements Callback<Result> {
@Override public void onResponse(Call<Result> call, Response<Result> response) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ public MapzenRouter clearLocations() {
return this;
}

/**
* Sets the router's http handler for adding custom headers and parameters to
* requests.
* @param handler
*/
public void setHttpHandler(MapzenRouterHttpHandler handler) {
internalRouter.setHttpHandler(handler.turnByTurnHandler());
}

public Router getRouter() {
return internalRouter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import java.util.HashMap;
import java.util.Map;

import static com.mapzen.android.core.GenericHttpHandler.LogLevel.BASIC;
import static com.mapzen.android.core.GenericHttpHandler.LogLevel.BODY;
import static com.mapzen.android.core.GenericHttpHandler.LogLevel.HEADERS;
import static com.mapzen.android.core.GenericHttpHandler.LogLevel.NONE;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
Expand All @@ -18,14 +22,23 @@
*/
public abstract class MapzenRouterHttpHandler implements GenericHttpHandler {

public static final String DEFAULT_URL = "https://valhalla.mapzen.com/";
public static final LogLevel DEFAULT_LOG_LEVEL = MapzenRouterHttpHandler.getDefaultLogLevel();
private TurnByTurnHttpHandler handler;
ChainProceder chainProceder = new ChainProceder();

/**
* Construct handler with default url and log levels.
*/
public MapzenRouterHttpHandler() {
handler = new TurnByTurnHttpHandler(HttpLoggingInterceptor.Level.BODY);
handler = new TurnByTurnHttpHandler(DEFAULT_URL, DEFAULT_LOG_LEVEL);
}

/**
* Construct handler with custom url and log levels.
*/
public MapzenRouterHttpHandler(String url, LogLevel logLevel) {
handler = new TurnByTurnHttpHandler(url, logLevel);
}

/**
Expand All @@ -36,6 +49,10 @@ TurnByTurnHttpHandler turnByTurnHandler() {
return handler;
}

private static LogLevel getDefaultLogLevel() {
return BASIC;
}

/**
* Handles appending api keys for all turn-by-turn requests.
*/
Expand All @@ -45,32 +62,20 @@ class TurnByTurnHttpHandler extends HttpHandler {

private String apiKey;

/**
* Construct handler with default url and log levels.
*/
public TurnByTurnHttpHandler() {
configure(DEFAULT_URL, DEFAULT_LOG_LEVEL);
}

/**
* Construct handler with url and default log levels.
*/
public TurnByTurnHttpHandler(String endpoint) {
configure(endpoint, DEFAULT_LOG_LEVEL);
}

/**
* Construct handler with log levels and default url.
*/
public TurnByTurnHttpHandler(HttpLoggingInterceptor.Level logLevel) {
configure(DEFAULT_URL, logLevel);
}
private final Map<LogLevel, HttpLoggingInterceptor.Level> TO_INTERNAL_LEVEL = new HashMap() {
{
put(NONE, HttpLoggingInterceptor.Level.NONE);
put(BASIC, HttpLoggingInterceptor.Level.BASIC);
put(HEADERS, HttpLoggingInterceptor.Level.HEADERS);
put(BODY, HttpLoggingInterceptor.Level.BODY);
}
};

/**
* Construct handler with url and log levels.
*/
public TurnByTurnHttpHandler(String endpoint, HttpLoggingInterceptor.Level logLevel) {
configure(endpoint, logLevel);
public TurnByTurnHttpHandler(String endpoint, LogLevel logLevel) {
configure(endpoint, TO_INTERNAL_LEVEL.get(logLevel));
}

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

import com.mapzen.android.core.GenericHttpHandler;
import com.mapzen.valhalla.TestHttpHandlerHelper;

import org.junit.Test;

import java.util.HashMap;
Expand All @@ -9,6 +12,8 @@
import static com.mapzen.android.core.GenericHttpHandler.USER_AGENT;
import static com.mapzen.android.routing.MapzenRouterHttpHandler.TurnByTurnHttpHandler.NAME_API_KEY;
import okhttp3.Interceptor;
import okhttp3.logging.HttpLoggingInterceptor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

Expand Down Expand Up @@ -64,4 +69,22 @@ public class MapzenRouterHttpHandlerTest {

verify(proceder).proceed(chain, params, headers);
}

@Test public void initWithCustomUrlAndLogLevelShouldCallConstructor() throws Exception {
MapzenRouterHttpHandler handler = new MapzenRouterHttpHandler("http://test.com",
GenericHttpHandler.LogLevel.BODY) {
@Override public Map<String, String> queryParamsForRequest() {
return null;
}

@Override public Map<String, String> headersForRequest() {
return null;
}
};
String endpoint = TestHttpHandlerHelper.getEndpoint(handler.turnByTurnHandler());
assertThat(endpoint).isEqualTo("http://test.com");
HttpLoggingInterceptor.Level level = TestHttpHandlerHelper.getLogLevel(
handler.turnByTurnHandler());
assertThat(level).isEqualTo(HttpLoggingInterceptor.Level.BODY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class MapzenRouterTest {

Expand Down Expand Up @@ -96,6 +97,15 @@ public class MapzenRouterTest {
assertThat(MapzenRouter.DistanceUnits.KILOMETERS.toString()).isEqualTo("kilometers");
}

@Test public void setHttpHandler_shouldCallInternalRouter() throws Exception {
MapzenRouterHttpHandler mapzenRouterHandler = mock(MapzenRouterHttpHandler.class);
MapzenRouterHttpHandler.TurnByTurnHttpHandler handler = mock(
MapzenRouterHttpHandler.TurnByTurnHttpHandler.class);
when(mapzenRouterHandler.turnByTurnHandler()).thenReturn(handler);
router.setHttpHandler(mapzenRouterHandler);
verify(router.getRouter()).setHttpHandler(handler);
}

class TestRouteCallback implements RouteCallback {

@Override public void success(Route route) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mapzen.valhalla;


import okhttp3.logging.HttpLoggingInterceptor;

/**
* Used to get access to internal variables for
* {@link com.mapzen.android.routing.MapzenRouterHttpHandlerTest}.
*/
public class TestHttpHandlerHelper {

public static String getEndpoint(HttpHandler httpHandler) {
return httpHandler.endpoint;
}

public static HttpLoggingInterceptor.Level getLogLevel(HttpHandler httpHandler) {
return httpHandler.logLevel;
}
}