Skip to content

Commit f2e5cc5

Browse files
author
Łukasz Paczos
committed
removed access token from RouteOptions
Avoids problems of deserialization and usability of route objects. Each entity that does need to make a resources request using a route reference (like annotations refresh or reroute) will most likely get a token from a different source, the route object shouldn't need to transport it.
1 parent 692c855 commit f2e5cc5

12 files changed

Lines changed: 133 additions & 195 deletions

File tree

samples/src/main/java/com/mapbox/samples/BasicDirections.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ private static void simpleMapboxDirectionsRequest() throws IOException {
4040
coordinates.add(Point.fromLngLat(-95.6332, 29.7890));
4141
coordinates.add(Point.fromLngLat(-95.3591, 29.7576));
4242
RouteOptions routeOptions = RouteOptions.builder()
43-
.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN)
4443
.coordinatesList(coordinates)
4544
.build();
4645
builder.routeOptions(routeOptions);
46+
builder.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN);
4747

4848
// 2. That's it! Now execute the command and get the response.
4949
Response<DirectionsResponse> response = builder.build().executeCall();
@@ -66,14 +66,14 @@ private static void simpleMapboxDirectionsWalkingRequest() throws IOException {
6666
coordinates.add(Point.fromLngLat(-95.6332, 29.7890));
6767
coordinates.add(Point.fromLngLat(-95.3591, 29.7576));
6868
RouteOptions routeOptions = RouteOptions.builder()
69-
.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN)
7069
.coordinatesList(coordinates)
7170
.profile("walking")
7271
.walkingSpeed(1.0)
7372
.walkwayBias(0.6)
7473
.alleyBias(0.7)
7574
.build();
7675
builder.routeOptions(routeOptions);
76+
builder.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN);
7777

7878
// 2. That's it! Now execute the command and get the response.
7979
Response<DirectionsResponse> response = builder.build().executeCall();
@@ -96,11 +96,11 @@ private static void simpleMapboxDirectionsPostRequest() throws IOException {
9696
coordinates.add(Point.fromLngLat(-95.6332, 29.7890));
9797
coordinates.add(Point.fromLngLat(-95.3591, 29.7576));
9898
RouteOptions routeOptions = RouteOptions.builder()
99-
.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN)
10099
.coordinatesList(coordinates)
101100
.build();
102101
builder.routeOptions(routeOptions);
103102
builder.usePostMethod(true);
103+
builder.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN);
104104

105105
Response<DirectionsResponse> response = builder.build().executeCall();
106106

@@ -120,12 +120,12 @@ private static void asyncMapboxDirectionsRequest() {
120120
coordinates.add(Point.fromLngLat(-95.6332, 29.7890));
121121
coordinates.add(Point.fromLngLat(-95.3591, 29.7576));
122122
RouteOptions routeOptions = RouteOptions.builder()
123-
.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN)
124123
.coordinatesList(coordinates)
125124
.profile(DirectionsCriteria.PROFILE_CYCLING)
126125
.steps(true)
127126
.build();
128127
builder.routeOptions(routeOptions);
128+
builder.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN);
129129

130130
// 2. Now request the route using a async call
131131
builder.build().enqueueCall(new Callback<DirectionsResponse>() {

samples/src/main/java/com/mapbox/samples/BasicDirectionsRefresh.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ private static MapboxDirections mapboxDirections(Boolean addWaypoint) {
4949
}
5050
coordinates.add(Point.fromLngLat(-95.3591, 29.7576));
5151
RouteOptions routeOptions = RouteOptions.builder()
52-
.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN)
5352
.coordinatesList(coordinates)
5453
.enableRefresh(true)
5554
.overview(OVERVIEW_FULL)
5655
.profile(PROFILE_DRIVING_TRAFFIC)
5756
.annotationsList(Arrays.asList(ANNOTATION_CONGESTION, ANNOTATION_MAXSPEED))
5857
.build();
5958
builder.routeOptions(routeOptions);
59+
builder.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN);
6060

6161
return builder.build();
6262
}

services-directions-models/src/main/java/com/mapbox/api/directions/v5/models/DirectionsResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* chained together to make up a similar structure to the original APIs JSON response.
1818
*
1919
* @see <a href="https://www.mapbox.com/api-documentation/navigation/#directions-response-object">Direction
20-
* Response Object</a>
20+
* Response Object</a>
2121
* @since 1.0.0
2222
*/
2323
@AutoValue
@@ -146,7 +146,7 @@ public static DirectionsResponse fromJson(@NonNull String json) {
146146
* @return a new instance of this class defined by the values passed inside this static factory
147147
* method
148148
* @see RouteOptions#fromUrl(java.net.URL)
149-
* @see RouteOptions#fromJson(String, String)
149+
* @see RouteOptions#fromJson(String)
150150
*/
151151
public static DirectionsResponse fromJson(
152152
@NonNull String json, @Nullable RouteOptions routeOptions, @Nullable String requestUuid) {

services-directions-models/src/main/java/com/mapbox/api/directions/v5/models/DirectionsRoute.java

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,6 @@ public static TypeAdapter<DirectionsRoute> typeAdapter(Gson gson) {
167167
/**
168168
* Create a new instance of this class by passing in a formatted valid JSON String.
169169
* <p>
170-
* Use this method if the provided serialized route was not obtained by this library.
171-
* Alternatively, use {@link #fromJson(String, String)}.
172-
* <p>
173170
* If you're using the provided route with the Mapbox Navigation SDK, also see
174171
* {@link #fromJson(String, RouteOptions, String)}.
175172
*
@@ -180,41 +177,6 @@ public static TypeAdapter<DirectionsRoute> typeAdapter(Gson gson) {
180177
public static DirectionsRoute fromJson(@NonNull String json) {
181178
GsonBuilder gson = new GsonBuilder();
182179
JsonObject jsonObject = gson.create().fromJson(json, JsonObject.class);
183-
if (jsonObject.has("routeOptions")) {
184-
throw new IllegalArgumentException(
185-
"Provided serialized route contains RouteOptions. "
186-
+ "Use DirectionsRoute#fromJson(json, accessToken) instead."
187-
);
188-
}
189-
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
190-
gson.registerTypeAdapter(Point.class, new PointAsCoordinatesTypeAdapter());
191-
return gson.create().fromJson(jsonObject, DirectionsRoute.class);
192-
}
193-
194-
/**
195-
* Create a new instance of this class by passing in a formatted valid JSON String.
196-
* <p>
197-
* Use this method if the provided serialized route was obtained by this library.
198-
* This means that it includes {@link RouteOptions} and you need to supply a Mapbox Access Token.
199-
* Alternatively, use {@link #fromJson(String)}.
200-
*
201-
* @param json a formatted valid JSON string defining a GeoJson Directions Route
202-
* @param accessToken a Mapbox Access Token
203-
* @return a new instance of this class defined by the values passed inside this static factory
204-
* method
205-
*/
206-
public static DirectionsRoute fromJson(@NonNull String json, @NonNull String accessToken) {
207-
GsonBuilder gson = new GsonBuilder();
208-
JsonObject jsonObject = gson.create().fromJson(json, JsonObject.class);
209-
if (jsonObject.has("routeOptions")) {
210-
JsonObject routeOptions = jsonObject.getAsJsonObject("routeOptions");
211-
routeOptions.addProperty("access_token", accessToken);
212-
} else {
213-
throw new IllegalArgumentException(
214-
"Provided serialized route does not contain RouteOptions. "
215-
+ "Use DirectionsRoute#fromJson(json) instead."
216-
);
217-
}
218180
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
219181
gson.registerTypeAdapter(Point.class, new PointAsCoordinatesTypeAdapter());
220182
return gson.create().fromJson(jsonObject, DirectionsRoute.class);
@@ -234,7 +196,7 @@ public static DirectionsRoute fromJson(@NonNull String json, @NonNull String acc
234196
* @return a new instance of this class defined by the values passed inside this static factory
235197
* method
236198
* @see RouteOptions#fromUrl(java.net.URL)
237-
* @see RouteOptions#fromJson(String, String)
199+
* @see RouteOptions#fromJson(String)
238200
*/
239201
public static DirectionsRoute fromJson(
240202
@NonNull String json, @Nullable RouteOptions routeOptions, @Nullable String requestUuid

services-directions-models/src/main/java/com/mapbox/api/directions/v5/models/RouteOptions.java

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.google.auto.value.AutoValue;
77
import com.google.gson.Gson;
88
import com.google.gson.GsonBuilder;
9-
import com.google.gson.JsonElement;
109
import com.google.gson.JsonObject;
1110
import com.google.gson.TypeAdapter;
1211
import com.google.gson.annotations.SerializedName;
@@ -15,7 +14,6 @@
1514
import com.mapbox.api.directions.v5.utils.FormatUtils;
1615
import com.mapbox.api.directions.v5.utils.ParseUtils;
1716
import com.mapbox.geojson.Point;
18-
import com.ryanharter.auto.value.gson.Ignore;
1917
import java.io.UnsupportedEncodingException;
2018
import java.net.URL;
2119
import java.net.URLDecoder;
@@ -366,16 +364,6 @@ public List<String> annotationsList() {
366364
@DirectionsCriteria.VoiceUnitCriteria
367365
public abstract String voiceUnits();
368366

369-
/**
370-
* A valid Mapbox access token used to making the request.
371-
*
372-
* @return a string representing the Mapbox access token
373-
*/
374-
@SerializedName("access_token")
375-
@Ignore(Ignore.Type.SERIALIZATION)
376-
@NonNull
377-
public abstract String accessToken();
378-
379367
/**
380368
* A semicolon-separated list indicating from which side of the road
381369
* to approach a waypoint.
@@ -675,20 +663,14 @@ public static TypeAdapter<RouteOptions> typeAdapter(Gson gson) {
675663
* Create a new instance of this class by passing in a formatted valid JSON String
676664
* with a Mapbox Access Token.
677665
*
678-
* @param json a formatted valid JSON string defining a RouteOptions
679-
* @param accessToken a Mapbox Access Token since {@link #toJson()} does not serialize the token
666+
* @param json a formatted valid JSON string defining a RouteOptions
680667
* @return a new instance of this class defined by the values passed inside this static factory
681668
* method
682669
* @see #fromUrl(URL)
683670
*/
684671
@NonNull
685-
public static RouteOptions fromJson(@NonNull String json, @NonNull String accessToken) {
686-
GsonBuilder gson = new GsonBuilder();
687-
688-
JsonObject jsonObject = gson.create().fromJson(json, JsonObject.class);
689-
jsonObject.addProperty("access_token", accessToken);
690-
691-
return fromJsonElement(jsonObject);
672+
public static RouteOptions fromJson(@NonNull String json) {
673+
return fromJsonString(json);
692674
}
693675

694676
/**
@@ -710,7 +692,7 @@ public String toJson() {
710692
* @param url request URL
711693
* @return a new instance of this class defined by the values passed inside this static factory
712694
* method
713-
* @see #fromJson(String, String)
695+
* @see #fromJson(String)
714696
*/
715697
@NonNull
716698
public static RouteOptions fromUrl(@NonNull URL url) {
@@ -746,13 +728,6 @@ private static RouteOptions fromJsonString(@NonNull String json) {
746728
return gson.create().fromJson(json, RouteOptions.class);
747729
}
748730

749-
@NonNull
750-
private static RouteOptions fromJsonElement(@NonNull JsonElement jsonElement) {
751-
GsonBuilder gson = new GsonBuilder();
752-
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
753-
return gson.create().fromJson(jsonElement, RouteOptions.class);
754-
}
755-
756731
/**
757732
* Convert the current {@link RouteOptions} to its builder holding the currently assigned
758733
* values. This allows you to modify a single property and then rebuild the object resulting in
@@ -1109,17 +1084,6 @@ public Builder annotationsList(@Nullable List<String> annotations) {
11091084
@NonNull
11101085
public abstract Builder voiceUnits(@Nullable String voiceUnits);
11111086

1112-
/**
1113-
* A valid Mapbox access token used to making the request.
1114-
*
1115-
* @param accessToken a string containing a valid Mapbox access token.
1116-
* Avoiding to provide a token will most-likely result in a failure, however,
1117-
* it's annotated as nullable to prevent serialization of tokens.
1118-
* @return this builder for chaining options together
1119-
*/
1120-
@NonNull
1121-
public abstract Builder accessToken(@Nullable String accessToken);
1122-
11231087
/**
11241088
* Exclude certain road types from routing. The default is to not exclude anything from the
11251089
* profile selected. The following exclude flags are available for each profile:

services-directions-models/src/test/java/com/mapbox/api/directions/v5/models/DirectionsResponseTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public void fromJson_correctlyBuildsFromJsonWithOptionsAndUuid() throws Exceptio
5555
add(Point.fromLngLat(1.0, 1.0));
5656
add(Point.fromLngLat(2.0, 2.0));
5757
}})
58-
.accessToken("token")
5958
.build();
6059
String uuid = "123";
6160
DirectionsResponse response = DirectionsResponse.fromJson(json, options, uuid);

services-directions-models/src/test/java/com/mapbox/api/directions/v5/models/DirectionsRouteTest.java

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public void directionsRoute_doesContainOptionsAndUuid() throws Exception {
6969
add(Point.fromLngLat(1.0, 1.0));
7070
add(Point.fromLngLat(2.0, 2.0));
7171
}})
72-
.accessToken("token")
7372
.build();
7473
String uuid = "123";
7574
DirectionsRoute route = DirectionsRoute.fromJson(json, options, uuid);
@@ -87,52 +86,14 @@ public void directionsRoute_json_withOptionsAndUUID_roundTripping() throws Excep
8786
add(Point.fromLngLat(1.0, 1.0));
8887
add(Point.fromLngLat(2.0, 2.0));
8988
}})
90-
.accessToken("token")
9189
.build();
9290
String uuid = "123";
9391
DirectionsRoute route = DirectionsRoute.fromJson(json, options, uuid);
9492

9593
String newRouteJson = route.toJson();
9694

97-
DirectionsRoute newRoute = DirectionsRoute.fromJson(newRouteJson, "token");
95+
DirectionsRoute newRoute = DirectionsRoute.fromJson(newRouteJson);
9896

9997
assertEquals(route, newRoute);
10098
}
101-
102-
@Test
103-
public void directionsRoute_json_invalid_with_options() throws Exception {
104-
thrown.expect(RuntimeException.class);
105-
thrown.expectMessage(
106-
"Provided serialized route contains RouteOptions. "
107-
+ "Use DirectionsRoute#fromJson(json, accessToken) instead.");
108-
String json = loadJsonFixture("directions_v5-with-closure_precision_6.json");
109-
RouteOptions options = RouteOptions.builder()
110-
.profile(DirectionsCriteria.PROFILE_DRIVING_TRAFFIC)
111-
.coordinatesList(new ArrayList<Point>() {{
112-
add(Point.fromLngLat(1.0, 1.0));
113-
add(Point.fromLngLat(2.0, 2.0));
114-
}})
115-
.accessToken("token")
116-
.build();
117-
String uuid = "123";
118-
DirectionsRoute route = DirectionsRoute.fromJson(json, options, uuid);
119-
120-
String newRouteJson = route.toJson();
121-
122-
DirectionsRoute.fromJson(newRouteJson);
123-
}
124-
125-
@Test
126-
public void directionsRoute_json_invalid_without_options() throws Exception {
127-
thrown.expect(RuntimeException.class);
128-
thrown.expectMessage(
129-
"Provided serialized route does not contain RouteOptions. "
130-
+ "Use DirectionsRoute#fromJson(json) instead.");
131-
String json = loadJsonFixture("directions_v5-with-closure_precision_6.json");
132-
DirectionsRoute route = DirectionsRoute.fromJson(json);
133-
134-
String newRouteJson = route.toJson();
135-
136-
DirectionsRoute.fromJson(newRouteJson, "token");
137-
}
13899
}

0 commit comments

Comments
 (0)