Skip to content

Commit f7a798d

Browse files
author
Devota Aabel
authored
Directions refresh (#955)
1 parent 30f299c commit f7a798d

19 files changed

Lines changed: 631 additions & 19 deletions

File tree

build.gradle

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,19 @@ subprojects {
6565
}
6666

6767
def TESTABLE_MODULES = ["services",
68-
"services-core",
69-
"services-directions",
70-
"services-geocoding",
71-
"services-geojson",
72-
"services-matching",
73-
"services-matrix",
74-
"services-optimization",
75-
"services-route-tiles",
76-
"services-speech",
77-
"services-staticmap",
78-
"services-tilequery",
79-
"services-turf"]
68+
"services-core",
69+
"services-directions",
70+
"services-geocoding",
71+
"services-geojson",
72+
"services-matching",
73+
"services-matrix",
74+
"services-optimization",
75+
"services-route-tiles",
76+
"services-speech",
77+
"services-staticmap",
78+
"services-tilequery",
79+
"services-turf",
80+
"services-directions-refresh"]
8081

8182
def RELEASE_MODULES = ["services",
8283
"services-core",
@@ -85,9 +86,9 @@ def RELEASE_MODULES = ["services",
8586

8687
subprojects { subproject ->
8788
if (TESTABLE_MODULES.contains(subproject.name)) {
88-
subproject.apply plugin: "com.vanniktech.android.junit.jacoco"
89-
subproject.apply from: "${rootDir}/gradle/jacoco.gradle"
90-
subproject.apply from: "${rootDir}/gradle/checkstyle.gradle"
89+
subproject.apply plugin: "com.vanniktech.android.junit.jacoco"
90+
subproject.apply from: "${rootDir}/gradle/jacoco.gradle"
91+
subproject.apply from: "${rootDir}/gradle/checkstyle.gradle"
9192
}
9293

9394
if (RELEASE_MODULES.contains(subproject.name)) {

samples/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ dependencies {
2424
implementation project(":services-staticmap")
2525
implementation project(":services-speech")
2626
implementation project(":services-tilequery")
27+
implementation project(":services-directions-refresh")
2728
}
2829

2930
buildConfig {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.mapbox.samples;
2+
3+
import com.mapbox.api.directions.v5.MapboxDirections;
4+
import com.mapbox.api.directions.v5.models.DirectionsResponse;
5+
import com.mapbox.api.directionsrefresh.v1.MapboxDirectionsRefresh;
6+
import com.mapbox.api.directionsrefresh.v1.models.DirectionsRefreshResponse;
7+
import com.mapbox.geojson.Point;
8+
import com.mapbox.sample.BuildConfig;
9+
10+
import java.io.IOException;
11+
12+
import retrofit2.Call;
13+
import retrofit2.Callback;
14+
import retrofit2.Response;
15+
16+
import static com.mapbox.api.directions.v5.DirectionsCriteria.ANNOTATION_CONGESTION;
17+
import static com.mapbox.api.directions.v5.DirectionsCriteria.OVERVIEW_FULL;
18+
import static com.mapbox.api.directions.v5.DirectionsCriteria.PROFILE_DRIVING_TRAFFIC;
19+
20+
public class BasicDirectionsRefresh {
21+
public static void main(String[] args) throws IOException {
22+
String requestId = simpleMapboxDirectionsRequest();
23+
simpleMapboxDirectionsRefreshRequest(requestId);
24+
}
25+
26+
private static String simpleMapboxDirectionsRequest() throws IOException {
27+
MapboxDirections directions = MapboxDirections.builder()
28+
.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN)
29+
.enableRefresh(true)
30+
.origin(Point.fromLngLat(-95.6332, 29.7890))
31+
.destination(Point.fromLngLat(-95.3591, 29.7576))
32+
.overview(OVERVIEW_FULL)
33+
.profile(PROFILE_DRIVING_TRAFFIC)
34+
.annotations(ANNOTATION_CONGESTION).build();
35+
36+
Response<DirectionsResponse> response = directions.executeCall();
37+
System.out.println("Directions response: " + response);
38+
String requestId = response.body().routes().get(0).routeOptions().requestUuid();
39+
40+
return requestId;
41+
}
42+
43+
private static void simpleMapboxDirectionsRefreshRequest(String requestId) {
44+
MapboxDirectionsRefresh refresh =
45+
MapboxDirectionsRefresh.builder()
46+
.accessToken(BuildConfig.MAPBOX_ACCESS_TOKEN)
47+
.requestId(requestId)
48+
.routeIndex(0)
49+
.legIndex(0)
50+
.build();
51+
52+
refresh.enqueueCall(new Callback<DirectionsRefreshResponse>() {
53+
@Override
54+
public void onResponse(Call<DirectionsRefreshResponse> call, Response<DirectionsRefreshResponse> response) {
55+
System.out.println("Refresh response: " + response);
56+
}
57+
58+
@Override
59+
public void onFailure(Call<DirectionsRefreshResponse> call, Throwable throwable) {
60+
System.out.println("" + call.request().url());
61+
System.out.printf("Failure: " + throwable);
62+
}
63+
});
64+
}
65+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apply plugin: 'java-library'
2+
3+
dependencies {
4+
implementation fileTree(dir: 'libs', include: ['*.jar'])
5+
api project(":services-core")
6+
7+
// Annotations
8+
compileOnly dependenciesList.supportAnnotation
9+
10+
// AutoValue
11+
compileOnly dependenciesList.autoValue
12+
compileOnly dependenciesList.autoValueGson
13+
implementation project(":services")
14+
15+
// Test Dependencies
16+
testImplementation dependenciesList.okhttp3Mockwebserver
17+
testImplementation project(path: ':services-core', configuration: 'testOutput')
18+
}
19+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.mapbox.api.directionsrefresh.v1;
2+
3+
import com.mapbox.api.directionsrefresh.v1.models.DirectionsRefreshResponse;
4+
5+
import retrofit2.Call;
6+
import retrofit2.http.GET;
7+
import retrofit2.http.Header;
8+
import retrofit2.http.Path;
9+
import retrofit2.http.Query;
10+
11+
/**
12+
* Interface that defines the directions refresh service. This corresponds to v1 of the
13+
* directions API, specifically driving directions.
14+
*
15+
* @since 4.4.0
16+
*/
17+
public interface DirectionsRefreshService {
18+
19+
/**
20+
* Constructs the html call using the information passed in through the
21+
* {@link MapboxDirectionsRefresh.Builder}.
22+
*
23+
* @param userAgent the user agent
24+
* @param requestId a uuid specifying the request containing the route being refreshed
25+
* @param routeIndex the index of the specified route
26+
* @param legIndex the index of the leg to start the refresh response (inclusive)
27+
* @param accessToken Mapbox access token
28+
* @return the {@link DirectionsRefreshResponse} in a Call wrapper
29+
* @since 4.4.0
30+
*/
31+
@GET("directions-refresh/v1/mapbox/driving-traffic/{request_id}/{route_index}/{leg_index}")
32+
Call<DirectionsRefreshResponse> getCall(
33+
@Header("User-Agent") String userAgent,
34+
@Path("request_id") String requestId,
35+
@Path("route_index") int routeIndex,
36+
@Path("leg_index") int legIndex,
37+
@Query("access_token") String accessToken
38+
);
39+
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package com.mapbox.api.directionsrefresh.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.google.gson.GsonBuilder;
8+
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
9+
import com.mapbox.api.directionsrefresh.v1.models.DirectionsRefreshAdapterFactory;
10+
import com.mapbox.api.directionsrefresh.v1.models.DirectionsRefreshResponse;
11+
import com.mapbox.core.MapboxService;
12+
import com.mapbox.core.constants.Constants;
13+
import com.mapbox.core.utils.ApiCallHelper;
14+
15+
import retrofit2.Call;
16+
17+
/**
18+
* The directions refresh API allows a route to be refreshed via it's annotations. The
19+
* refreshEnabled parameter would have had to have been specified as true in the original
20+
* directions request for a refresh to be possible.
21+
*
22+
* @since 4.4.0
23+
*/
24+
@AutoValue
25+
public abstract class MapboxDirectionsRefresh extends MapboxService<DirectionsRefreshResponse,
26+
DirectionsRefreshService> {
27+
28+
private static final int ZERO = 0;
29+
30+
protected MapboxDirectionsRefresh() {
31+
super(DirectionsRefreshService.class);
32+
}
33+
34+
@Override
35+
protected Call<DirectionsRefreshResponse> initializeCall() {
36+
return getService().getCall(
37+
ApiCallHelper.getHeaderUserAgent(clientAppName()),
38+
requestId(),
39+
routeIndex(),
40+
legIndex(),
41+
accessToken()
42+
);
43+
}
44+
45+
abstract String requestId();
46+
47+
abstract int routeIndex();
48+
49+
abstract int legIndex();
50+
51+
abstract String accessToken();
52+
53+
@Nullable
54+
abstract String clientAppName();
55+
56+
@NonNull
57+
@Override
58+
protected abstract String baseUrl();
59+
60+
@Override
61+
protected GsonBuilder getGsonBuilder() {
62+
return super.getGsonBuilder()
63+
.registerTypeAdapterFactory(DirectionsRefreshAdapterFactory.create())
64+
.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
65+
}
66+
67+
/**
68+
* Convert the current {@link MapboxDirectionsRefresh} to its builder holding the currently
69+
* assigned values. This allows you to modify a single property and then rebuild the object
70+
* resulting in an updated and modified {@link MapboxDirectionsRefresh}.
71+
*
72+
* @return a {@link MapboxDirectionsRefresh.Builder} with the same values
73+
* @since 4.4.0
74+
*/
75+
public abstract Builder toBuilder();
76+
77+
/**
78+
* Build a new {@link MapboxDirectionsRefresh} builder with default initial values.
79+
*
80+
* @return a {@link Builder} for creating a default {@link MapboxDirectionsRefresh}
81+
* @since 4.4.0
82+
*/
83+
public static Builder builder() {
84+
return new AutoValue_MapboxDirectionsRefresh.Builder()
85+
.baseUrl(Constants.BASE_API_URL)
86+
.routeIndex(ZERO)
87+
.legIndex(ZERO);
88+
}
89+
90+
/**
91+
* This builder is used to build a new request to the Mapbox Directions Refresh API. A request
92+
* requires an access token and a request id.
93+
*
94+
* @since 4.4.0
95+
*/
96+
@AutoValue.Builder
97+
public abstract static class Builder {
98+
99+
/**
100+
* Specified here is the uuid of the initial directions request. The original request must
101+
* have specified enableRefresh.
102+
*
103+
* @param requestId id of the original directions request. This is found in the
104+
* {@link com.mapbox.api.directions.v5.models.RouteOptions} object.
105+
* @return this builder
106+
* @since 4.4.0
107+
*/
108+
public abstract Builder requestId(String requestId);
109+
110+
/**
111+
* Index of original route in response.
112+
*
113+
* @param routeIndex index of route in response
114+
* @return this builder
115+
* @since 4.4.0
116+
*/
117+
public abstract Builder routeIndex(@NonNull int routeIndex);
118+
119+
/**
120+
* Index of leg of which to start. The response will include the annotations of the specified
121+
* leg through the end of the list of legs.
122+
*
123+
* @param legIndex index of leg
124+
* @return this builder
125+
* @since 4.4.0
126+
*/
127+
public abstract Builder legIndex(@NonNull int legIndex);
128+
129+
/**
130+
* Required to call when this is being built. If no access token provided,
131+
* {@link com.mapbox.core.exceptions.ServicesException} will be thrown.
132+
*
133+
* @param accessToken Mapbox access token
134+
* @since 4.4.0
135+
*/
136+
public abstract Builder accessToken(@NonNull String accessToken);
137+
138+
/**
139+
* Base package name or other simple string identifier. Used inside the calls user agent header.
140+
*
141+
* @param clientAppName base package name or other simple string identifier
142+
* @return this builder for chaining options together
143+
* @since 4.4.0
144+
*/
145+
public abstract Builder clientAppName(@NonNull String clientAppName);
146+
147+
/**
148+
* Optionally change the APIs base URL to something other then the default Mapbox one.
149+
*
150+
* @param baseUrl base url used as endpoint
151+
* @return this builder
152+
* @since 4.4.0
153+
*/
154+
public abstract Builder baseUrl(String baseUrl);
155+
156+
/**
157+
* Returns an instance of {@link MapboxDirectionsRefresh} for interacting with the endpoint
158+
* with the specified values.
159+
*
160+
* @return instance of {@link MapboxDirectionsRefresh} with specified attributes
161+
* @since 4.4.0
162+
*/
163+
public abstract MapboxDirectionsRefresh build();
164+
}
165+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.mapbox.api.directionsrefresh.v1.models;
2+
3+
import com.google.gson.TypeAdapterFactory;
4+
import com.ryanharter.auto.value.gson.GsonTypeAdapterFactory;
5+
6+
/**
7+
* Required so that AutoValue can generate specific type adapters when needed inside the
8+
* directions refresh package.
9+
*
10+
* @since 4.4.0
11+
*/
12+
@GsonTypeAdapterFactory
13+
public abstract class DirectionsRefreshAdapterFactory implements TypeAdapterFactory {
14+
15+
/**
16+
* Creates a TypeAdapter that AutoValue uses to generate specific type adapters.
17+
*
18+
* @return a {@link TypeAdapterFactory} to deserialize {@link DirectionsRefreshResponse}
19+
* @since 4.4.0
20+
*/
21+
public static TypeAdapterFactory create() {
22+
return new AutoValueGson_DirectionsRefreshAdapterFactory();
23+
}
24+
}

0 commit comments

Comments
 (0)