Skip to content
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 @@ -56,40 +56,34 @@
@AutoValue
public abstract class MapboxDirections extends
MapboxService<DirectionsResponse, DirectionsService> {
private static final int MAX_URL_SIZE = 1024 * 8;

protected MapboxDirections() {
super(DirectionsService.class);
}

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

if (usePostMethod()) {
return getService().postCall(
ApiCallHelper.getHeaderUserAgent(clientAppName()),
user(),
profile(),
formatCoordinates(coordinates()),
accessToken(),
alternatives(),
geometries(),
overview(),
radius(),
steps(),
bearing(),
continueStraight(),
annotation(),
language(),
roundaboutExits(),
voiceInstructions(),
bannerInstructions(),
voiceUnits(),
exclude(),
approaches(),
waypointIndices(),
waypointNames(),
waypointTargets(),
enableRefresh());
return post();
}

return get();
}

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

private Call<DirectionsResponse> get() {
return getService().getCall(
ApiCallHelper.getHeaderUserAgent(clientAppName()),
user(),
Expand Down Expand Up @@ -117,6 +111,34 @@ protected Call<DirectionsResponse> initializeCall() {
enableRefresh());
}

private Call<DirectionsResponse> post() {
return getService().postCall(
ApiCallHelper.getHeaderUserAgent(clientAppName()),
user(),
profile(),
formatCoordinates(coordinates()),
accessToken(),
alternatives(),
geometries(),
overview(),
radius(),
steps(),
bearing(),
continueStraight(),
annotation(),
language(),
roundaboutExits(),
voiceInstructions(),
bannerInstructions(),
voiceUnits(),
exclude(),
approaches(),
waypointIndices(),
waypointNames(),
waypointTargets(),
enableRefresh());
}

@Override
protected GsonBuilder getGsonBuilder() {
return super.getGsonBuilder()
Expand Down Expand Up @@ -303,7 +325,7 @@ private static String formatWaypointTargets(Point[] waypointTargets) {
@Nullable
abstract EventListener eventListener();

@NonNull
@Nullable
abstract Boolean usePostMethod();

/**
Expand All @@ -318,8 +340,7 @@ public static Builder builder() {
.baseUrl(Constants.BASE_API_URL)
.profile(DirectionsCriteria.PROFILE_DRIVING)
.user(DirectionsCriteria.PROFILE_DEFAULT_USER)
.geometries(DirectionsCriteria.GEOMETRY_POLYLINE6)
.usePostMethod(false);
.geometries(DirectionsCriteria.GEOMETRY_POLYLINE6);
}

/**
Expand Down Expand Up @@ -812,6 +833,8 @@ public Builder get() {

abstract Builder usePostMethod(@NonNull Boolean usePost);

abstract Boolean usePostMethod();

abstract MapboxDirections autoBuild();

/**
Expand All @@ -838,18 +861,18 @@ public MapboxDirections build() {
if (waypointIndices != null) {
if (waypointIndices.length < 2) {
throw new ServicesException(
"Waypoints must be a list of at least two indexes separated by ';'");
"Waypoints must be a list of at least two indexes separated by ';'");
}
if (waypointIndices[0] != 0 || waypointIndices[waypointIndices.length - 1]
!= coordinates.size() - 1) {
!= coordinates.size() - 1) {
throw new ServicesException(
"Waypoints must contain indices of the first and last coordinates"
"Waypoints must contain indices of the first and last coordinates"
);
}
for (int i = 1; i < waypointIndices.length - 1; i++) {
if (waypointIndices[i] < 0 || waypointIndices[i] >= coordinates.size()) {
throw new ServicesException(
"Waypoints index too large (no corresponding coordinate)");
"Waypoints index too large (no corresponding coordinate)");
}
}
}
Expand Down Expand Up @@ -895,5 +918,4 @@ public MapboxDirections build() {
return directions;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import okhttp3.Call;
import okhttp3.EventListener;
Expand Down Expand Up @@ -843,11 +844,100 @@ public void testPost() throws IOException {
.build();

Response<DirectionsResponse> response = mapboxDirections.executeCall();
System.out.print(response.raw().request().url());
assertEquals(200, response.code());
assertEquals("Ok", response.body().code());

assertNotNull(response.body().routes());
assertEquals(1, response.body().routes().size());
}

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

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

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

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

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

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

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

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

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

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

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

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

private void addWaypoints(MapboxDirections.Builder builder, int number) {
for (int i = 0; i < number; i++) {
builder.addWaypoint(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;
}
}