|
| 1 | +package com.mapbox.services.api.rx.distance.v1; |
| 2 | + |
| 3 | +import com.mapbox.services.api.ServicesException; |
| 4 | +import com.mapbox.services.api.distance.v1.MapboxDistance; |
| 5 | +import com.mapbox.services.api.distance.v1.models.DistanceResponse; |
| 6 | + |
| 7 | +import retrofit2.Retrofit; |
| 8 | +import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; |
| 9 | +import retrofit2.converter.gson.GsonConverterFactory; |
| 10 | +import rx.Observable; |
| 11 | + |
| 12 | +/** |
| 13 | + * Note this API is still in preview. |
| 14 | + * <p> |
| 15 | + * The Mapbox Distance API returns all travel times between many points. For example, given 3 |
| 16 | + * locations A, B, C, the Distance API will return a matrix of travel times in seconds between each |
| 17 | + * location. This API allows you to build tools that efficiently check the reachability of |
| 18 | + * coordinates from each other, filter points by travel time, or run algorithms for solving |
| 19 | + * optimization problems. |
| 20 | + * </p> |
| 21 | + * <p> |
| 22 | + * Limits placed on this API include a maximum 100 coordinate pairs per request and a maximum 60 |
| 23 | + * requests per minute. |
| 24 | + * </p> |
| 25 | + * This class has support for Rx Observables. |
| 26 | + * |
| 27 | + * @see <a href="https://www.mapbox.com/api-documentation/?language=Java#distance">Mapbox Distance API documentation</a> |
| 28 | + * @since 2.0.0 |
| 29 | + */ |
| 30 | +public class MapboxDistanceRx extends MapboxDistance { |
| 31 | + |
| 32 | + private DistanceServiceRx serviceRx = null; |
| 33 | + private Observable<DistanceResponse> observable = null; |
| 34 | + |
| 35 | + public MapboxDistanceRx(Builder builder) { |
| 36 | + super(builder); |
| 37 | + } |
| 38 | + |
| 39 | + private DistanceServiceRx getServiceRx() { |
| 40 | + // No need to recreate it |
| 41 | + if (serviceRx != null) { |
| 42 | + return serviceRx; |
| 43 | + } |
| 44 | + |
| 45 | + // Retrofit instance |
| 46 | + Retrofit retrofit = new Retrofit.Builder() |
| 47 | + .client(getOkHttpClient()) |
| 48 | + .baseUrl(builder.getBaseUrl()) |
| 49 | + .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) |
| 50 | + .addConverterFactory(GsonConverterFactory.create(getGson())) |
| 51 | + .build(); |
| 52 | + |
| 53 | + // Distance service |
| 54 | + serviceRx = retrofit.create(DistanceServiceRx.class); |
| 55 | + return serviceRx; |
| 56 | + } |
| 57 | + |
| 58 | + public Observable<DistanceResponse> getObservable() { |
| 59 | + // No need to recreate it |
| 60 | + if (observable != null) { |
| 61 | + return observable; |
| 62 | + } |
| 63 | + |
| 64 | + observable = getServiceRx().getObservable( |
| 65 | + getHeaderUserAgent(builder.getClientAppName()), |
| 66 | + builder.getUser(), |
| 67 | + builder.getProfile(), |
| 68 | + builder.getAccessToken(), |
| 69 | + builder.getCoordinates() |
| 70 | + ); |
| 71 | + |
| 72 | + // Done |
| 73 | + return observable; |
| 74 | + } |
| 75 | + |
| 76 | + public static class Builder extends MapboxDistance.Builder<Builder> { |
| 77 | + @Override |
| 78 | + public MapboxDistanceRx build() throws ServicesException { |
| 79 | + super.build(); |
| 80 | + return new MapboxDistanceRx(this); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments