Skip to content

Commit

Permalink
Added wrappers for route tile APIs (#913)
Browse files Browse the repository at this point in the history
  • Loading branch information
Devota Aabel authored Nov 6, 2018
1 parent 3da1ad5 commit d67f1e9
Show file tree
Hide file tree
Showing 16 changed files with 649 additions and 1 deletion.
21 changes: 21 additions & 0 deletions services-route-tiles/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apply plugin: 'java-library'

dependencies {
api project(":services-core")
api project(":services-geojson")

// Annotations
compileOnly dependenciesList.supportAnnotation

// AutoValue
compileOnly dependenciesList.autoValue
compileOnly dependenciesList.autoValueGson

// Test Dependencies
testImplementation dependenciesList.okhttp3Mockwebserver
testImplementation project(path: ':services-core', configuration: 'testOutput')
compile project(path: ':services-geojson')
}

apply from: "${rootDir}/gradle/checkstyle.gradle"
apply from: "${rootDir}/gradle/jacoco.gradle"
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package com.mapbox.api.routetiles.v1;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.auto.value.AutoValue;
import com.mapbox.api.routetiles.v1.versions.MapboxRouteTileVersions;
import com.mapbox.core.MapboxService;
import com.mapbox.core.constants.Constants;
import com.mapbox.core.exceptions.ServicesException;
import com.mapbox.core.utils.ApiCallHelper;
import com.mapbox.core.utils.MapboxUtils;
import com.mapbox.geojson.BoundingBox;

import okhttp3.ResponseBody;
import retrofit2.Call;

/**
* The Route Tiles API allows the download of route tiles for the purpose of offline routing. To
* get a list of the versions, use the {@link MapboxRouteTileVersions} API.
*
* @since 4.1.0
*/
@AutoValue
public abstract class MapboxRouteTiles extends MapboxService<ResponseBody, RouteTilesService> {

protected MapboxRouteTiles() {
super(RouteTilesService.class);
}

@Override
protected Call<ResponseBody> initializeCall() {
return getService().getCall(
ApiCallHelper.getHeaderUserAgent(clientAppName()),
formatBoundingBox(boundingBox()),
version(),
accessToken()
);
}

private String formatBoundingBox(BoundingBox boundingBox) {
return String.format("%f,%f;%f,%f",
boundingBox.west(), boundingBox.south(), boundingBox.east(), boundingBox.north());
}

@Nullable
abstract String clientAppName();

@NonNull
abstract BoundingBox boundingBox();

@NonNull
abstract String version();

@NonNull
abstract String accessToken();

@Override
protected abstract String baseUrl();

/**
* Build a new {@link MapboxRouteTiles} object.
*
* @return a {@link Builder} object for creating this object
* @since 4.1.0
*/
public static Builder builder() {
return new AutoValue_MapboxRouteTiles.Builder()
.baseUrl(Constants.BASE_API_URL);
}

/**
* Returns the builder which created this instance of {@link MapboxRouteTiles} and allows for
* modification and building a new route tiles request with new information.
*
* @return {@link Builder} with the same variables set as this route tiles object
* @since 4.1.0
*/
public abstract Builder toBuilder();

/**
* This builder is used to create a new request to the Mapbox Route Tiles API. At a bare minimum,
* your request must include an access token, a {@link BoundingBox}, and a version.
*
* @since 4.1.0
*/
@AutoValue.Builder
public abstract static class Builder {

/**
* The bounding box of which to download map route tiles.
*
* @param boundingBox of which to download map route tiles
* @return this builder for chaining options together
* @since 4.1.0
*/
public abstract Builder boundingBox(@NonNull BoundingBox boundingBox);

/**
* The version of map tiles being requested. To get a list of the versions, use the
* {@link MapboxRouteTileVersions} API.
*
* @param version of which to download
* @return this builder for chaining options together
* @since 4.1.0
*/
public abstract Builder version(@NonNull String version);


/**
* Required to call when this is being built. If no access token provided,
* {@link ServicesException} will be thrown.
*
* @param accessToken Mapbox access token, You must have a Mapbox account inorder to use
* the Route Tiles API
* @return this builder for chaining options together
* @since 4.1.0
*/
public abstract Builder accessToken(@NonNull String accessToken);

/**
* Optionally change the APIs base URL to something other then the default Mapbox one.
*
* @param baseUrl base url used as end point
* @return this builder for chaining options together
* @since 4.1.0
*/
public abstract Builder baseUrl(@NonNull String baseUrl);

/**
* Base package name or other simple string identifier. Used inside the calls user agent header.
*
* @param clientAppName base package name or other simple string identifier
* @return this builder for chaining options together
* @since 4.1.0
*/
public abstract Builder clientAppName(@NonNull String clientAppName);

abstract MapboxRouteTiles autoBuild();

/**
* This uses the provided parameters set using the {@link Builder} and first checks that all
* values are valid, and creates a new {@link MapboxRouteTiles} object with the values provided.
*
* @return a new instance of Mapbox Route Tiles
* @throws ServicesException when a provided parameter is detected to be incorrect
* @since 4.1.0
*/
public MapboxRouteTiles build() {
MapboxRouteTiles mapboxRouteTiles = autoBuild();

if (!MapboxUtils.isAccessTokenValid(mapboxRouteTiles.accessToken())) {
throw new ServicesException("Using Mapbox Services requires setting a valid access token.");
}

return mapboxRouteTiles;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.mapbox.api.routetiles.v1;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Path;
import retrofit2.http.Query;

/**
* Interface that defines the Route Tiles Service (v1).
*
* @since 4.1.0
*/
public interface RouteTilesService {

/**
* Constructs the html call using the informmation passed in through the
* {@link MapboxRouteTiles.Builder}.
*
* @param userAgent the user agent
* @param coordinates a string value of the min and max longitude and latitude
* @param version version which was previously fetched through
* {@link com.mapbox.api.routetiles.v1.versions.MapboxRouteTileVersions}
* @param accessToken Mapbox access token
* @return the ResponseBody containing the data stream wrapped in a Call wrapper
* @since 4.1.0
*/
@GET("route-tiles/v1/{coordinates}")
Call<ResponseBody> getCall(
@Header("User-Agent") String userAgent,
@Path("coordinates") String coordinates,
@Query("version") String version,
@Query("access_token") String accessToken
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Contains classes for accessing the Mapbox route tiles API.
*
* @since 4.1.0
*/
package com.mapbox.api.routetiles.v1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package com.mapbox.api.routetiles.v1.versions;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.auto.value.AutoValue;
import com.google.gson.GsonBuilder;
import com.mapbox.api.routetiles.v1.MapboxRouteTiles;
import com.mapbox.api.routetiles.v1.versions.models.RouteTileVersionsAdapterFactory;
import com.mapbox.api.routetiles.v1.versions.models.RouteTileVersionsResponse;
import com.mapbox.core.MapboxService;
import com.mapbox.core.constants.Constants;
import com.mapbox.core.exceptions.ServicesException;
import com.mapbox.core.utils.ApiCallHelper;
import com.mapbox.core.utils.MapboxUtils;

import retrofit2.Call;

/**
* The Route Tile Versions API allows the fetching of all available versions of route tiles
* currently available. It is used in conjunction with the {@link MapboxRouteTiles} API.
*
* @since 4.1.0
*/
@AutoValue
public abstract class MapboxRouteTileVersions extends MapboxService<RouteTileVersionsResponse,
RouteTileVersionsService> {

protected MapboxRouteTileVersions() {
super(RouteTileVersionsService.class);
}

@Override
protected GsonBuilder getGsonBuilder() {
return new GsonBuilder()
.registerTypeAdapterFactory(RouteTileVersionsAdapterFactory.create());
}

@Override
protected Call<RouteTileVersionsResponse> initializeCall() {
return getService().getCall(
ApiCallHelper.getHeaderUserAgent(clientAppName()),
accessToken()
);
}

@Nullable
abstract String clientAppName();

@NonNull
abstract String accessToken();

@Override
protected abstract String baseUrl();

/**
* Build a new {@link MapboxRouteTileVersions} object.
*
* @return a {@link Builder} object for creating this object
* @since 4.1.0
*/
public static Builder builder() {
return new AutoValue_MapboxRouteTileVersions.Builder()
.baseUrl(Constants.BASE_API_URL);
}

/**
* Returns the builder which created this instance of {@link MapboxRouteTileVersions} and
* allows for modification and building a new route tile versions request with new information.
*
* @return {@link Builder} with the same variables set as this route tile versions object
* @since 4.1.0
*/
public abstract Builder toBuilder();

/**
* This builder is used to create a new request to the Mapbox Route Tiles API. At a bare minimum,
* your request must include an access token.
*
* @since 4.1.0
*/
@AutoValue.Builder
public abstract static class Builder {

/**
* Required to call when this is being built. If no access token provided,
* {@link ServicesException} will be thrown.
*
* @param accessToken Mapbox access token, You must have a Mapbox account inorder to use
* the Route Tiles API
* @return this builder for chaining options together
* @since 4.1.0
*/
public abstract Builder accessToken(@NonNull String accessToken);

/**
* Optionally change the APIs base URL to something other then the default Mapbox one.
*
* @param baseUrl base url used as end point
* @return this builder for chaining options together
* @since 4.1.0
*/
public abstract Builder baseUrl(@NonNull String baseUrl);

/**
* Base package name or other simple string identifier. Used inside the calls user agent header.
*
* @param clientAppName base package name or other simple string identifier
* @return this builder for chaining options together
* @since 4.1.0
*/
public abstract Builder clientAppName(@NonNull String clientAppName);

abstract MapboxRouteTileVersions autoBuild();

/**
* This uses the provided parameters set using the {@link Builder} and first checks that all
* values are valid, and creates a new {@link MapboxRouteTileVersions} object with the values
* provided.
*
* @return a new instance of Mapbox Route Tiles Version
* @throws ServicesException when a provided parameter is detected to be incorrect
* @since 4.1.0
*/
public MapboxRouteTileVersions build() {
MapboxRouteTileVersions mapboxRouteTileVersions = autoBuild();

if (!MapboxUtils.isAccessTokenValid(mapboxRouteTileVersions.accessToken())) {
throw new ServicesException("Using Mapbox Services requires setting a valid access token.");
}

return mapboxRouteTileVersions;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.mapbox.api.routetiles.v1.versions;

import com.mapbox.api.routetiles.v1.versions.models.RouteTileVersionsResponse;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Query;

/**
* Interface that defines the Route Tile Versions Service (v1).
*
* @since 4.1.0
*/
public interface RouteTileVersionsService {
/**
*
* @param userAgent the user agent
* @param accessToken Mapbox access token
* @return the ResponseBody containing the data stream wrapped in a Call wrapper
* @since 4.1.0
*/
@GET("route-tiles/v1/versions?")
Call<RouteTileVersionsResponse> getCall(
@Header("User-Agent") String userAgent,
@Query("access_token") String accessToken
);
}
Loading

0 comments on commit d67f1e9

Please sign in to comment.