Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated map matching to use a POST request if the url for a GET request is larger than the maximum allowed size #978

Merged
merged 1 commit into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @since 1.0.0
*/
public abstract class MapboxService<T, S> {

protected static final int MAX_URL_SIZE = 1024 * 8;
private final Class<S> serviceType;
private boolean enableDebug;
protected OkHttpClient okHttpClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
@AutoValue
public abstract class MapboxDirections extends
MapboxService<DirectionsResponse, DirectionsService> {
private static final int MAX_URL_SIZE = 1024 * 8;

protected MapboxDirections() {
super(DirectionsService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,26 @@ protected GsonBuilder getGsonBuilder() {

@Override
protected Call<MapMatchingResponse> initializeCall() {
if (usePostMethod() == null) {
return callForUrlLength();
}

if (usePostMethod()) {
return getService().postCall(
ApiCallHelper.getHeaderUserAgent(clientAppName()),
user(),
profile(),
coordinates(),
accessToken(),
geometries(),
radiuses(),
steps(),
overview(),
timestamps(),
annotations(),
language(),
tidy(),
roundaboutExits(),
bannerInstructions(),
voiceInstructions(),
voiceUnits(),
waypointIndices(),
waypointNames(),
approaches());
return post();
}

return get();
}

private Call<MapMatchingResponse> callForUrlLength() {
Call<MapMatchingResponse> get = get();
if (get.request().url().toString().length() < MAX_URL_SIZE) {
return get;
}
return post();
}

private Call<MapMatchingResponse> get() {
return getService().getCall(
ApiCallHelper.getHeaderUserAgent(clientAppName()),
user(),
Expand All @@ -106,6 +103,30 @@ protected Call<MapMatchingResponse> initializeCall() {
approaches());
}

private Call<MapMatchingResponse> post() {
return getService().postCall(
ApiCallHelper.getHeaderUserAgent(clientAppName()),
user(),
profile(),
coordinates(),
accessToken(),
geometries(),
radiuses(),
steps(),
overview(),
timestamps(),
annotations(),
language(),
tidy(),
roundaboutExits(),
bannerInstructions(),
voiceInstructions(),
voiceUnits(),
waypointIndices(),
waypointNames(),
approaches());
}

/**
* Wrapper method for Retrofits {@link Call#execute()} call returning a response specific to the
* Map Matching API.
Expand Down Expand Up @@ -149,7 +170,7 @@ public void onFailure(Call<MapMatchingResponse> call, Throwable throwable) {
});
}

@NonNull
@Nullable
abstract Boolean usePostMethod();

@Nullable
Expand Down Expand Up @@ -228,8 +249,7 @@ public static Builder builder() {
.baseUrl(Constants.BASE_API_URL)
.profile(DirectionsCriteria.PROFILE_DRIVING)
.geometries(DirectionsCriteria.GEOMETRY_POLYLINE6)
.user(DirectionsCriteria.PROFILE_DEFAULT_USER)
.usePostMethod(false);
.user(DirectionsCriteria.PROFILE_DEFAULT_USER);
}

/**
Expand Down Expand Up @@ -666,7 +686,7 @@ public MapboxMapMatching build() {
"Waypoints must be a list of at least two indexes separated by ';'");
}
if (waypointIndices[0] != 0
|| waypointIndices[waypointIndices.length - 1] != coordinates.size() - 1) {
|| waypointIndices[waypointIndices.length - 1] != coordinates.size() - 1) {
throw new ServicesException(
"Waypoints must contain indices of the first and last coordinates"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import okhttp3.HttpUrl;
import okhttp3.mockwebserver.MockResponse;
Expand Down Expand Up @@ -674,4 +675,94 @@ public void testUsePostMethod() throws Exception {
assertNotNull(response.body().matchings());
assertEquals(1, response.body().matchings().size());
}

@Test
public void testCallForUrlLength_longUrl() {
MapboxMapMatching.Builder builder = MapboxMapMatching.builder()
.profile(PROFILE_CYCLING)
.steps(true)
.coordinate(Point.fromLngLat(-122.42,37.78))
.coordinate(Point.fromLngLat(-77.03,38.91))
.voiceInstructions(true)
.voiceUnits(DirectionsCriteria.IMPERIAL)
.accessToken(ACCESS_TOKEN)
.baseUrl(mockUrl.toString());
addWaypoints(builder, 400);

retrofit2.Call<MapMatchingResponse> call = builder.build().initializeCall();

assertEquals("POST", call.request().method());
}

@Test
public void testCallForUrlLength_shortUrl() {
MapboxMapMatching.Builder builder = MapboxMapMatching.builder()
.profile(PROFILE_CYCLING)
.steps(true)
.coordinate(Point.fromLngLat(-122.42,37.78))
.coordinate(Point.fromLngLat(-77.03,38.91))
.voiceInstructions(true)
.voiceUnits(DirectionsCriteria.IMPERIAL)
.accessToken(ACCESS_TOKEN)
.baseUrl(mockUrl.toString());
addWaypoints(builder, 10);

retrofit2.Call<MapMatchingResponse> call = builder.build().initializeCall();

assertEquals("GET", call.request().method());
}

@Test
public void testPostIsUsed() {
MapboxMapMatching.Builder builder = MapboxMapMatching.builder()
.profile(PROFILE_CYCLING)
.steps(true)
.coordinate(Point.fromLngLat(-122.42,37.78))
.coordinate(Point.fromLngLat(-77.03,38.91))
.voiceInstructions(true)
.voiceUnits(DirectionsCriteria.IMPERIAL)
.accessToken(ACCESS_TOKEN)
.baseUrl(mockUrl.toString())
.post();

retrofit2.Call<MapMatchingResponse> call = builder.build().initializeCall();

assertEquals("POST", call.request().method());
}

@Test
public void testGetIsUsed() {
MapboxMapMatching.Builder builder = MapboxMapMatching.builder()
.profile(PROFILE_CYCLING)
.steps(true)
.coordinate(Point.fromLngLat(-122.42,37.78))
.coordinate(Point.fromLngLat(-77.03,38.91))
.voiceInstructions(true)
.voiceUnits(DirectionsCriteria.IMPERIAL)
.accessToken(ACCESS_TOKEN)
.baseUrl(mockUrl.toString())
.get();

retrofit2.Call<MapMatchingResponse> call = builder.build().initializeCall();

assertEquals("GET", call.request().method());
}

private void addWaypoints(MapboxMapMatching.Builder builder, int number) {
for (int i = 0; i < number; i++) {
builder.coordinate(Point.fromLngLat(getRandomLng(), getRandomLat()));
}
}

private double getRandomLng() {
Random random = new Random();
double lng = random.nextDouble() % 360;
return lng - 180;
}

private double getRandomLat() {
Random random = new Random();
double lat = random.nextDouble() % 180;
return lat - 90;
}
}