Skip to content

Commit d67f1e9

Browse files
author
Devota Aabel
authored
Added wrappers for route tile APIs (#913)
1 parent 3da1ad5 commit d67f1e9

16 files changed

Lines changed: 649 additions & 1 deletion

services-route-tiles/build.gradle

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apply plugin: 'java-library'
2+
3+
dependencies {
4+
api project(":services-core")
5+
api project(":services-geojson")
6+
7+
// Annotations
8+
compileOnly dependenciesList.supportAnnotation
9+
10+
// AutoValue
11+
compileOnly dependenciesList.autoValue
12+
compileOnly dependenciesList.autoValueGson
13+
14+
// Test Dependencies
15+
testImplementation dependenciesList.okhttp3Mockwebserver
16+
testImplementation project(path: ':services-core', configuration: 'testOutput')
17+
compile project(path: ':services-geojson')
18+
}
19+
20+
apply from: "${rootDir}/gradle/checkstyle.gradle"
21+
apply from: "${rootDir}/gradle/jacoco.gradle"
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package com.mapbox.api.routetiles.v1;
2+
3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
5+
6+
import com.google.auto.value.AutoValue;
7+
import com.mapbox.api.routetiles.v1.versions.MapboxRouteTileVersions;
8+
import com.mapbox.core.MapboxService;
9+
import com.mapbox.core.constants.Constants;
10+
import com.mapbox.core.exceptions.ServicesException;
11+
import com.mapbox.core.utils.ApiCallHelper;
12+
import com.mapbox.core.utils.MapboxUtils;
13+
import com.mapbox.geojson.BoundingBox;
14+
15+
import okhttp3.ResponseBody;
16+
import retrofit2.Call;
17+
18+
/**
19+
* The Route Tiles API allows the download of route tiles for the purpose of offline routing. To
20+
* get a list of the versions, use the {@link MapboxRouteTileVersions} API.
21+
*
22+
* @since 4.1.0
23+
*/
24+
@AutoValue
25+
public abstract class MapboxRouteTiles extends MapboxService<ResponseBody, RouteTilesService> {
26+
27+
protected MapboxRouteTiles() {
28+
super(RouteTilesService.class);
29+
}
30+
31+
@Override
32+
protected Call<ResponseBody> initializeCall() {
33+
return getService().getCall(
34+
ApiCallHelper.getHeaderUserAgent(clientAppName()),
35+
formatBoundingBox(boundingBox()),
36+
version(),
37+
accessToken()
38+
);
39+
}
40+
41+
private String formatBoundingBox(BoundingBox boundingBox) {
42+
return String.format("%f,%f;%f,%f",
43+
boundingBox.west(), boundingBox.south(), boundingBox.east(), boundingBox.north());
44+
}
45+
46+
@Nullable
47+
abstract String clientAppName();
48+
49+
@NonNull
50+
abstract BoundingBox boundingBox();
51+
52+
@NonNull
53+
abstract String version();
54+
55+
@NonNull
56+
abstract String accessToken();
57+
58+
@Override
59+
protected abstract String baseUrl();
60+
61+
/**
62+
* Build a new {@link MapboxRouteTiles} object.
63+
*
64+
* @return a {@link Builder} object for creating this object
65+
* @since 4.1.0
66+
*/
67+
public static Builder builder() {
68+
return new AutoValue_MapboxRouteTiles.Builder()
69+
.baseUrl(Constants.BASE_API_URL);
70+
}
71+
72+
/**
73+
* Returns the builder which created this instance of {@link MapboxRouteTiles} and allows for
74+
* modification and building a new route tiles request with new information.
75+
*
76+
* @return {@link Builder} with the same variables set as this route tiles object
77+
* @since 4.1.0
78+
*/
79+
public abstract Builder toBuilder();
80+
81+
/**
82+
* This builder is used to create a new request to the Mapbox Route Tiles API. At a bare minimum,
83+
* your request must include an access token, a {@link BoundingBox}, and a version.
84+
*
85+
* @since 4.1.0
86+
*/
87+
@AutoValue.Builder
88+
public abstract static class Builder {
89+
90+
/**
91+
* The bounding box of which to download map route tiles.
92+
*
93+
* @param boundingBox of which to download map route tiles
94+
* @return this builder for chaining options together
95+
* @since 4.1.0
96+
*/
97+
public abstract Builder boundingBox(@NonNull BoundingBox boundingBox);
98+
99+
/**
100+
* The version of map tiles being requested. To get a list of the versions, use the
101+
* {@link MapboxRouteTileVersions} API.
102+
*
103+
* @param version of which to download
104+
* @return this builder for chaining options together
105+
* @since 4.1.0
106+
*/
107+
public abstract Builder version(@NonNull String version);
108+
109+
110+
/**
111+
* Required to call when this is being built. If no access token provided,
112+
* {@link ServicesException} will be thrown.
113+
*
114+
* @param accessToken Mapbox access token, You must have a Mapbox account inorder to use
115+
* the Route Tiles API
116+
* @return this builder for chaining options together
117+
* @since 4.1.0
118+
*/
119+
public abstract Builder accessToken(@NonNull String accessToken);
120+
121+
/**
122+
* Optionally change the APIs base URL to something other then the default Mapbox one.
123+
*
124+
* @param baseUrl base url used as end point
125+
* @return this builder for chaining options together
126+
* @since 4.1.0
127+
*/
128+
public abstract Builder baseUrl(@NonNull String baseUrl);
129+
130+
/**
131+
* Base package name or other simple string identifier. Used inside the calls user agent header.
132+
*
133+
* @param clientAppName base package name or other simple string identifier
134+
* @return this builder for chaining options together
135+
* @since 4.1.0
136+
*/
137+
public abstract Builder clientAppName(@NonNull String clientAppName);
138+
139+
abstract MapboxRouteTiles autoBuild();
140+
141+
/**
142+
* This uses the provided parameters set using the {@link Builder} and first checks that all
143+
* values are valid, and creates a new {@link MapboxRouteTiles} object with the values provided.
144+
*
145+
* @return a new instance of Mapbox Route Tiles
146+
* @throws ServicesException when a provided parameter is detected to be incorrect
147+
* @since 4.1.0
148+
*/
149+
public MapboxRouteTiles build() {
150+
MapboxRouteTiles mapboxRouteTiles = autoBuild();
151+
152+
if (!MapboxUtils.isAccessTokenValid(mapboxRouteTiles.accessToken())) {
153+
throw new ServicesException("Using Mapbox Services requires setting a valid access token.");
154+
}
155+
156+
return mapboxRouteTiles;
157+
}
158+
}
159+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.mapbox.api.routetiles.v1;
2+
3+
import okhttp3.ResponseBody;
4+
import retrofit2.Call;
5+
import retrofit2.http.GET;
6+
import retrofit2.http.Header;
7+
import retrofit2.http.Path;
8+
import retrofit2.http.Query;
9+
10+
/**
11+
* Interface that defines the Route Tiles Service (v1).
12+
*
13+
* @since 4.1.0
14+
*/
15+
public interface RouteTilesService {
16+
17+
/**
18+
* Constructs the html call using the informmation passed in through the
19+
* {@link MapboxRouteTiles.Builder}.
20+
*
21+
* @param userAgent the user agent
22+
* @param coordinates a string value of the min and max longitude and latitude
23+
* @param version version which was previously fetched through
24+
* {@link com.mapbox.api.routetiles.v1.versions.MapboxRouteTileVersions}
25+
* @param accessToken Mapbox access token
26+
* @return the ResponseBody containing the data stream wrapped in a Call wrapper
27+
* @since 4.1.0
28+
*/
29+
@GET("route-tiles/v1/{coordinates}")
30+
Call<ResponseBody> getCall(
31+
@Header("User-Agent") String userAgent,
32+
@Path("coordinates") String coordinates,
33+
@Query("version") String version,
34+
@Query("access_token") String accessToken
35+
);
36+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Contains classes for accessing the Mapbox route tiles API.
3+
*
4+
* @since 4.1.0
5+
*/
6+
package com.mapbox.api.routetiles.v1;
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package com.mapbox.api.routetiles.v1.versions;
2+
3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
5+
6+
import com.google.auto.value.AutoValue;
7+
import com.google.gson.GsonBuilder;
8+
import com.mapbox.api.routetiles.v1.MapboxRouteTiles;
9+
import com.mapbox.api.routetiles.v1.versions.models.RouteTileVersionsAdapterFactory;
10+
import com.mapbox.api.routetiles.v1.versions.models.RouteTileVersionsResponse;
11+
import com.mapbox.core.MapboxService;
12+
import com.mapbox.core.constants.Constants;
13+
import com.mapbox.core.exceptions.ServicesException;
14+
import com.mapbox.core.utils.ApiCallHelper;
15+
import com.mapbox.core.utils.MapboxUtils;
16+
17+
import retrofit2.Call;
18+
19+
/**
20+
* The Route Tile Versions API allows the fetching of all available versions of route tiles
21+
* currently available. It is used in conjunction with the {@link MapboxRouteTiles} API.
22+
*
23+
* @since 4.1.0
24+
*/
25+
@AutoValue
26+
public abstract class MapboxRouteTileVersions extends MapboxService<RouteTileVersionsResponse,
27+
RouteTileVersionsService> {
28+
29+
protected MapboxRouteTileVersions() {
30+
super(RouteTileVersionsService.class);
31+
}
32+
33+
@Override
34+
protected GsonBuilder getGsonBuilder() {
35+
return new GsonBuilder()
36+
.registerTypeAdapterFactory(RouteTileVersionsAdapterFactory.create());
37+
}
38+
39+
@Override
40+
protected Call<RouteTileVersionsResponse> initializeCall() {
41+
return getService().getCall(
42+
ApiCallHelper.getHeaderUserAgent(clientAppName()),
43+
accessToken()
44+
);
45+
}
46+
47+
@Nullable
48+
abstract String clientAppName();
49+
50+
@NonNull
51+
abstract String accessToken();
52+
53+
@Override
54+
protected abstract String baseUrl();
55+
56+
/**
57+
* Build a new {@link MapboxRouteTileVersions} object.
58+
*
59+
* @return a {@link Builder} object for creating this object
60+
* @since 4.1.0
61+
*/
62+
public static Builder builder() {
63+
return new AutoValue_MapboxRouteTileVersions.Builder()
64+
.baseUrl(Constants.BASE_API_URL);
65+
}
66+
67+
/**
68+
* Returns the builder which created this instance of {@link MapboxRouteTileVersions} and
69+
* allows for modification and building a new route tile versions request with new information.
70+
*
71+
* @return {@link Builder} with the same variables set as this route tile versions object
72+
* @since 4.1.0
73+
*/
74+
public abstract Builder toBuilder();
75+
76+
/**
77+
* This builder is used to create a new request to the Mapbox Route Tiles API. At a bare minimum,
78+
* your request must include an access token.
79+
*
80+
* @since 4.1.0
81+
*/
82+
@AutoValue.Builder
83+
public abstract static class Builder {
84+
85+
/**
86+
* Required to call when this is being built. If no access token provided,
87+
* {@link ServicesException} will be thrown.
88+
*
89+
* @param accessToken Mapbox access token, You must have a Mapbox account inorder to use
90+
* the Route Tiles API
91+
* @return this builder for chaining options together
92+
* @since 4.1.0
93+
*/
94+
public abstract Builder accessToken(@NonNull String accessToken);
95+
96+
/**
97+
* Optionally change the APIs base URL to something other then the default Mapbox one.
98+
*
99+
* @param baseUrl base url used as end point
100+
* @return this builder for chaining options together
101+
* @since 4.1.0
102+
*/
103+
public abstract Builder baseUrl(@NonNull String baseUrl);
104+
105+
/**
106+
* Base package name or other simple string identifier. Used inside the calls user agent header.
107+
*
108+
* @param clientAppName base package name or other simple string identifier
109+
* @return this builder for chaining options together
110+
* @since 4.1.0
111+
*/
112+
public abstract Builder clientAppName(@NonNull String clientAppName);
113+
114+
abstract MapboxRouteTileVersions autoBuild();
115+
116+
/**
117+
* This uses the provided parameters set using the {@link Builder} and first checks that all
118+
* values are valid, and creates a new {@link MapboxRouteTileVersions} object with the values
119+
* provided.
120+
*
121+
* @return a new instance of Mapbox Route Tiles Version
122+
* @throws ServicesException when a provided parameter is detected to be incorrect
123+
* @since 4.1.0
124+
*/
125+
public MapboxRouteTileVersions build() {
126+
MapboxRouteTileVersions mapboxRouteTileVersions = autoBuild();
127+
128+
if (!MapboxUtils.isAccessTokenValid(mapboxRouteTileVersions.accessToken())) {
129+
throw new ServicesException("Using Mapbox Services requires setting a valid access token.");
130+
}
131+
132+
return mapboxRouteTileVersions;
133+
}
134+
}
135+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.mapbox.api.routetiles.v1.versions;
2+
3+
import com.mapbox.api.routetiles.v1.versions.models.RouteTileVersionsResponse;
4+
5+
import retrofit2.Call;
6+
import retrofit2.http.GET;
7+
import retrofit2.http.Header;
8+
import retrofit2.http.Query;
9+
10+
/**
11+
* Interface that defines the Route Tile Versions Service (v1).
12+
*
13+
* @since 4.1.0
14+
*/
15+
public interface RouteTileVersionsService {
16+
/**
17+
*
18+
* @param userAgent the user agent
19+
* @param accessToken Mapbox access token
20+
* @return the ResponseBody containing the data stream wrapped in a Call wrapper
21+
* @since 4.1.0
22+
*/
23+
@GET("route-tiles/v1/versions?")
24+
Call<RouteTileVersionsResponse> getCall(
25+
@Header("User-Agent") String userAgent,
26+
@Query("access_token") String accessToken
27+
);
28+
}

0 commit comments

Comments
 (0)