1313import com .mapbox .api .directions .v5 .DirectionsCriteria .OverviewCriteria ;
1414import com .mapbox .api .directions .v5 .DirectionsCriteria .ProfileCriteria ;
1515import com .mapbox .api .directions .v5 .DirectionsCriteria .VoiceUnitCriteria ;
16- import com .mapbox .api .directions .v5 .models .DirectionsError ;
1716import com .mapbox .api .directions .v5 .models .DirectionsResponse ;
18- import com .mapbox .api .directions .v5 .models .DirectionsRoute ;
1917import com .mapbox .api .directions .v5 .models .RouteLeg ;
20- import com .mapbox .api .directions .v5 .models .RouteOptions ;
2118import com .mapbox .core .MapboxService ;
2219import com .mapbox .core .constants .Constants ;
2320import com .mapbox .core .exceptions .ServicesException ;
2724import com .mapbox .geojson .Point ;
2825
2926import java .io .IOException ;
30- import java .lang .annotation .Annotation ;
3127import java .util .ArrayList ;
3228import java .util .List ;
3329import java .util .Locale ;
34- import java .util .logging .Level ;
35- import java .util .logging .Logger ;
3630
3731import okhttp3 .EventListener ;
3832import okhttp3 .Interceptor ;
3933import okhttp3 .OkHttpClient ;
40- import okhttp3 .ResponseBody ;
4134import okhttp3 .logging .HttpLoggingInterceptor ;
4235import retrofit2 .Call ;
4336import retrofit2 .Callback ;
44- import retrofit2 .Converter ;
4537import retrofit2 .Response ;
4638
4739/**
@@ -114,26 +106,8 @@ protected GsonBuilder getGsonBuilder() {
114106 @ Override
115107 public Response <DirectionsResponse > executeCall () throws IOException {
116108 Response <DirectionsResponse > response = super .executeCall ();
117- if (response .isSuccessful ()) {
118- if (response .body () != null && !response .body ().routes ().isEmpty ()) {
119- return Response .success (
120- response .body ()
121- .toBuilder ()
122- .routes (generateRouteOptions (response ))
123- .build (),
124- new okhttp3 .Response .Builder ()
125- .code (200 )
126- .message ("OK" )
127- .protocol (response .raw ().protocol ())
128- .headers (response .headers ())
129- .request (response .raw ().request ())
130- .build ());
131- }
132- } else {
133- errorDidOccur (null , response );
134- }
135-
136- return response ;
109+ DirectionsResponseFactory factory = new DirectionsResponseFactory (this );
110+ return factory .generate (response );
137111 }
138112
139113 /**
@@ -149,33 +123,9 @@ public void enqueueCall(final Callback<DirectionsResponse> callback) {
149123 getCall ().enqueue (new Callback <DirectionsResponse >() {
150124 @ Override
151125 public void onResponse (Call <DirectionsResponse > call , Response <DirectionsResponse > response ) {
152- if (!response .isSuccessful ()) {
153- errorDidOccur (callback , response );
154-
155- } else if (callback != null ) {
156- if (response .body () == null || response .body ().routes ().isEmpty ()) {
157- // If null just pass the original object back since there's nothing to modify.
158- callback .onResponse (call , response );
159-
160- } else {
161- Response <DirectionsResponse > newResponse =
162- Response .success (
163- response
164- .body ()
165- .toBuilder ()
166- .routes (generateRouteOptions (response ))
167- .build (),
168- new okhttp3 .Response .Builder ()
169- .code (200 )
170- .message ("OK" )
171- .protocol (response .raw ().protocol ())
172- .headers (response .headers ())
173- .request (response .raw ().request ())
174- .build ());
175-
176- callback .onResponse (call , newResponse );
177- }
178- }
126+ DirectionsResponseFactory factory = new DirectionsResponseFactory (MapboxDirections .this );
127+ Response <DirectionsResponse > generatedResponse = factory .generate (response );
128+ callback .onResponse (call , generatedResponse );
179129 }
180130
181131 @ Override
@@ -208,61 +158,6 @@ protected synchronized OkHttpClient getOkHttpClient() {
208158 return okHttpClient ;
209159 }
210160
211- private void errorDidOccur (@ Nullable Callback <DirectionsResponse > callback ,
212- @ NonNull Response <DirectionsResponse > response ) {
213- // Response gave an error, we try to LOGGER any messages into the LOGGER here.
214- Converter <ResponseBody , DirectionsError > errorConverter =
215- getRetrofit ().responseBodyConverter (DirectionsError .class , new Annotation [0 ]);
216- if (callback == null ) {
217- Logger .getLogger (MapboxDirections .class .getName ()).log (
218- Level .WARNING , "Failed to complete your request and callback is null" );
219- } else {
220- try {
221- callback .onFailure (getCall (),
222- new Throwable (errorConverter .convert (response .errorBody ()).message ()));
223- } catch (IOException ioException ) {
224- Logger .getLogger (MapboxDirections .class .getName ()).log (
225- Level .WARNING , "Failed to complete your request. " , ioException );
226- }
227- }
228- }
229-
230- private List <DirectionsRoute > generateRouteOptions (Response <DirectionsResponse > response ) {
231- List <DirectionsRoute > routes = response .body ().routes ();
232- List <DirectionsRoute > modifiedRoutes = new ArrayList <>();
233- for (DirectionsRoute route : routes ) {
234- modifiedRoutes .add (route .toBuilder ().routeOptions (
235- RouteOptions .builder ()
236- .profile (profile ())
237- .coordinates (coordinates ())
238- .waypointIndices (waypointIndices ())
239- .waypointNames (waypointNames ())
240- .waypointTargets (waypointTargets ())
241- .continueStraight (continueStraight ())
242- .annotations (annotation ())
243- .approaches (approaches ())
244- .bearings (bearing ())
245- .alternatives (alternatives ())
246- .language (language ())
247- .radiuses (radius ())
248- .user (user ())
249- .voiceInstructions (voiceInstructions ())
250- .bannerInstructions (bannerInstructions ())
251- .roundaboutExits (roundaboutExits ())
252- .geometries (geometries ())
253- .overview (overview ())
254- .steps (steps ())
255- .exclude (exclude ())
256- .voiceUnits (voiceUnits ())
257- .accessToken (accessToken ())
258- .requestUuid (response .body ().uuid ())
259- .baseUrl (baseUrl ())
260- .build ()
261- ).build ());
262- }
263- return modifiedRoutes ;
264- }
265-
266161 private static String formatCoordinates (List <Point > coordinates ) {
267162 String [] coordinatesFormatted = new String [coordinates .size ()];
268163 int index = 0 ;
0 commit comments