From 23847e1965d7511c8c3cbac03e148838b62a8394 Mon Sep 17 00:00:00 2001
From: danesfeder
Date: Thu, 7 Feb 2019 14:40:32 -0500
Subject: [PATCH] Add Interceptor and EventListener to MapboxDirections
---
.../api/directions/v5/MapboxDirections.java | 66 ++++++++++++++++---
.../directions/v5/MapboxDirectionsTest.java | 53 ++++++++++++++-
2 files changed, 109 insertions(+), 10 deletions(-)
diff --git a/services-directions/src/main/java/com/mapbox/api/directions/v5/MapboxDirections.java b/services-directions/src/main/java/com/mapbox/api/directions/v5/MapboxDirections.java
index 26b65ac67..56cede36b 100644
--- a/services-directions/src/main/java/com/mapbox/api/directions/v5/MapboxDirections.java
+++ b/services-directions/src/main/java/com/mapbox/api/directions/v5/MapboxDirections.java
@@ -33,7 +33,11 @@
import java.util.logging.Level;
import java.util.logging.Logger;
+import okhttp3.EventListener;
+import okhttp3.Interceptor;
+import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
+import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Converter;
@@ -51,9 +55,9 @@
*
*
* @see Android
- * Directions documentation
+ * Directions documentation
* @see Directions API
- * documentation
+ * documentation
* @since 1.0.0
*/
@AutoValue
@@ -179,6 +183,29 @@ public void onFailure(Call call, Throwable throwable) {
});
}
+ @Override
+ protected synchronized OkHttpClient getOkHttpClient() {
+ if (okHttpClient == null) {
+ OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
+ if (isEnableDebug()) {
+ HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
+ logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
+ httpClient.addInterceptor(logging);
+ }
+ Interceptor interceptor = interceptor();
+ if (interceptor != null) {
+ httpClient.addInterceptor(interceptor);
+ }
+ EventListener eventListener = eventListener();
+ if (eventListener != null) {
+ httpClient.eventListener(eventListener);
+ }
+
+ okHttpClient = httpClient.build();
+ }
+ return okHttpClient;
+ }
+
private void errorDidOccur(@Nullable Callback callback,
@NonNull Response response) {
// Response gave an error, we try to LOGGER any messages into the LOGGER here.
@@ -261,8 +288,8 @@ private static String formatWaypointTargets(Point[] waypointTargets) {
coordinatesFormatted[index++] = "";
} else {
coordinatesFormatted[index++] = String.format(Locale.US, "%s,%s",
- TextUtils.formatCoordinate(target.longitude()),
- TextUtils.formatCoordinate(target.latitude()));
+ TextUtils.formatCoordinate(target.longitude()),
+ TextUtils.formatCoordinate(target.latitude()));
}
}
return TextUtils.join(";", coordinatesFormatted);
@@ -338,6 +365,12 @@ private static String formatWaypointTargets(Point[] waypointTargets) {
@Nullable
abstract String waypointTargets();
+ @Nullable
+ abstract Interceptor interceptor();
+
+ @Nullable
+ abstract EventListener eventListener();
+
/**
* Build a new {@link MapboxDirections} object with the initial values set for
* {@link #baseUrl()}, {@link #profile()}, {@link #user()}, and {@link #geometries()}.
@@ -533,7 +566,7 @@ public Builder addWaypoint(@NonNull Point waypoint) {
* written in when returned
* @return this builder for chaining options together
* @see Supported
- * Languages
+ * Languages
* @since 2.2.0
*/
public Builder language(@Nullable Locale language) {
@@ -568,7 +601,7 @@ public Builder language(@Nullable Locale language) {
* or null which will result in no annotations being used
* @return this builder for chaining options together
* @see RouteLeg object
- * documentation
+ * documentation
* @since 2.1.0
*/
public Builder annotations(@Nullable @AnnotationCriteria String... annotations) {
@@ -714,8 +747,23 @@ public Builder radiuses(@FloatRange(from = 0) double... radiuses) {
*/
public abstract Builder baseUrl(String baseUrl);
- abstract Builder coordinates(@NonNull List coordinates);
+ /**
+ * Adds an optional interceptor to set in the OkHttp client.
+ *
+ * @param interceptor to set for OkHttp
+ * @return this builder for chaining options together
+ */
+ public abstract Builder interceptor(Interceptor interceptor);
+ /**
+ * Adds an optional event listener to set in the OkHttp client.
+ *
+ * @param eventListener to set for OkHttp
+ * @return this builder for chaining options together
+ */
+ public abstract Builder eventListener(EventListener eventListener);
+
+ abstract Builder coordinates(@NonNull List coordinates);
/**
* Indicates from which side of the road to approach a waypoint.
@@ -744,6 +792,7 @@ public Builder addApproaches(String... approaches) {
* each separated by ; . Values can be any string and total number of all characters cannot
* exceed 500. If provided, the list of waypointNames must be the same length as the list of
* coordinates, but you can skip a coordinate and show its position with the ; separator.
+ *
* @param waypointNames Custom names for waypoints used for the arrival instruction.
* @return this builder for chaining options together
* @since 3.3.0
@@ -761,6 +810,7 @@ public Builder addWaypointNames(@Nullable String... waypointNames) {
* The number of waypoint targets must be the same as the number of coordinates,
* but you can skip a coordinate with a null value.
* Must be used with steps=true.
+ *
* @param waypointTargets list of coordinate points for drop-off locations
* @return this builder for chaining options together
* @since 4.3.0
@@ -807,7 +857,7 @@ public MapboxDirections build() {
if (waypointTargets != null) {
if (waypointTargets.length != coordinates.size()) {
throw new ServicesException("Number of waypoint targets must match "
- + " the number of waypoints provided.");
+ + " the number of waypoints provided.");
}
waypointTargets(formatWaypointTargets(waypointTargets));
diff --git a/services-directions/src/test/java/com/mapbox/api/directions/v5/MapboxDirectionsTest.java b/services-directions/src/test/java/com/mapbox/api/directions/v5/MapboxDirectionsTest.java
index 835c24bfd..e7eba9e39 100644
--- a/services-directions/src/test/java/com/mapbox/api/directions/v5/MapboxDirectionsTest.java
+++ b/services-directions/src/test/java/com/mapbox/api/directions/v5/MapboxDirectionsTest.java
@@ -8,7 +8,6 @@
import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.api.directions.v5.models.LegAnnotation;
import com.mapbox.api.directions.v5.models.RouteOptions;
-import com.mapbox.api.directions.v5.models.StepManeuver;
import com.mapbox.core.TestUtils;
import com.mapbox.core.exceptions.ServicesException;
import com.mapbox.geojson.Point;
@@ -22,12 +21,14 @@
import org.junit.rules.ExpectedException;
import java.io.IOException;
-import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
+import okhttp3.Call;
+import okhttp3.EventListener;
import okhttp3.HttpUrl;
+import okhttp3.Interceptor;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
@@ -730,4 +731,52 @@ public void testWithWaypointTargets() throws Exception {
}
+ @Test
+ public void testWithInterceptor() throws Exception {
+ Interceptor interceptor = new Interceptor() {
+ @Override
+ public okhttp3.Response intercept(Chain chain) throws IOException {
+ return null;
+ }
+ };
+ MapboxDirections mapboxDirections = MapboxDirections.builder()
+ .profile(PROFILE_CYCLING)
+ .origin(Point.fromLngLat(-122.42,37.78))
+ .destination(Point.fromLngLat(-77.03,38.91))
+ .steps(true)
+ .voiceInstructions(true)
+ .voiceUnits(DirectionsCriteria.IMPERIAL)
+ .addWaypointNames("Home", "Work")
+ .accessToken(ACCESS_TOKEN)
+ .baseUrl(mockUrl.toString())
+ .interceptor(interceptor)
+ .build();
+
+ assertEquals(interceptor, mapboxDirections.interceptor());
+ }
+
+ @Test
+ public void testWithEventListener() throws Exception {
+ EventListener eventListener = new EventListener() {
+ @Override
+ public void callStart(Call call) {
+ super.callStart(call);
+ }
+ };
+ MapboxDirections mapboxDirections = MapboxDirections.builder()
+ .profile(PROFILE_CYCLING)
+ .origin(Point.fromLngLat(-122.42,37.78))
+ .destination(Point.fromLngLat(-77.03,38.91))
+ .steps(true)
+ .voiceInstructions(true)
+ .voiceUnits(DirectionsCriteria.IMPERIAL)
+ .addWaypointNames("Home", "Work")
+ .accessToken(ACCESS_TOKEN)
+ .baseUrl(mockUrl.toString())
+ .eventListener(eventListener)
+ .build();
+
+
+ assertEquals(eventListener, mapboxDirections.eventListener());
+ }
}