diff --git a/services-route-tiles/src/main/java/com/mapbox/api/routetiles/v1/MapboxRouteTiles.java b/services-route-tiles/src/main/java/com/mapbox/api/routetiles/v1/MapboxRouteTiles.java index 3a75dbac5..d77e36482 100644 --- a/services-route-tiles/src/main/java/com/mapbox/api/routetiles/v1/MapboxRouteTiles.java +++ b/services-route-tiles/src/main/java/com/mapbox/api/routetiles/v1/MapboxRouteTiles.java @@ -13,7 +13,11 @@ import com.mapbox.geojson.BoundingBox; import java.util.Locale; + +import okhttp3.Interceptor; +import okhttp3.OkHttpClient; import okhttp3.ResponseBody; +import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Call; /** @@ -39,14 +43,27 @@ protected Call initializeCall() { ); } - private String formatBoundingBox(BoundingBox boundingBox) { - return String.format(Locale.US, - "%f,%f;%f,%f", - boundingBox.west(), - boundingBox.south(), - boundingBox.east(), - boundingBox.north() - ); + @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); + } + Interceptor networkInterceptor = networkInterceptor(); + if (networkInterceptor != null) { + httpClient.addNetworkInterceptor(networkInterceptor); + } + + okHttpClient = httpClient.build(); + } + return okHttpClient; } @Nullable @@ -61,6 +78,12 @@ private String formatBoundingBox(BoundingBox boundingBox) { @NonNull abstract String accessToken(); + @Nullable + abstract Interceptor interceptor(); + + @Nullable + abstract Interceptor networkInterceptor(); + @Override protected abstract String baseUrl(); @@ -142,6 +165,22 @@ public abstract static class Builder { */ public abstract Builder clientAppName(@NonNull String clientAppName); + /** + * 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 network interceptor to set in the OkHttp client. + * + * @param interceptor to set for OkHttp + * @return this builder for chaining options together + */ + public abstract Builder networkInterceptor(Interceptor interceptor); + abstract MapboxRouteTiles autoBuild(); /** @@ -162,4 +201,14 @@ public MapboxRouteTiles build() { return mapboxRouteTiles; } } + + private String formatBoundingBox(BoundingBox boundingBox) { + return String.format(Locale.US, + "%f,%f;%f,%f", + boundingBox.west(), + boundingBox.south(), + boundingBox.east(), + boundingBox.north() + ); + } }