|
| 1 | +package com.mapbox.api.routetiles.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.mapbox.api.routetiles.v1.versions.MapboxRouteTileVersions; |
| 8 | +import com.mapbox.core.MapboxService; |
| 9 | +import com.mapbox.core.constants.Constants; |
| 10 | +import com.mapbox.core.exceptions.ServicesException; |
| 11 | +import com.mapbox.core.utils.ApiCallHelper; |
| 12 | +import com.mapbox.core.utils.MapboxUtils; |
| 13 | +import com.mapbox.geojson.BoundingBox; |
| 14 | + |
| 15 | +import okhttp3.ResponseBody; |
| 16 | +import retrofit2.Call; |
| 17 | + |
| 18 | +/** |
| 19 | + * The Route Tiles API allows the download of route tiles for the purpose of offline routing. To |
| 20 | + * get a list of the versions, use the {@link MapboxRouteTileVersions} API. |
| 21 | + * |
| 22 | + * @since 4.1.0 |
| 23 | + */ |
| 24 | +@AutoValue |
| 25 | +public abstract class MapboxRouteTiles extends MapboxService<ResponseBody, RouteTilesService> { |
| 26 | + |
| 27 | + protected MapboxRouteTiles() { |
| 28 | + super(RouteTilesService.class); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + protected Call<ResponseBody> initializeCall() { |
| 33 | + return getService().getCall( |
| 34 | + ApiCallHelper.getHeaderUserAgent(clientAppName()), |
| 35 | + formatBoundingBox(boundingBox()), |
| 36 | + version(), |
| 37 | + accessToken() |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + private String formatBoundingBox(BoundingBox boundingBox) { |
| 42 | + return String.format("%f,%f;%f,%f", |
| 43 | + boundingBox.west(), boundingBox.south(), boundingBox.east(), boundingBox.north()); |
| 44 | + } |
| 45 | + |
| 46 | + @Nullable |
| 47 | + abstract String clientAppName(); |
| 48 | + |
| 49 | + @NonNull |
| 50 | + abstract BoundingBox boundingBox(); |
| 51 | + |
| 52 | + @NonNull |
| 53 | + abstract String version(); |
| 54 | + |
| 55 | + @NonNull |
| 56 | + abstract String accessToken(); |
| 57 | + |
| 58 | + @Override |
| 59 | + protected abstract String baseUrl(); |
| 60 | + |
| 61 | + /** |
| 62 | + * Build a new {@link MapboxRouteTiles} object. |
| 63 | + * |
| 64 | + * @return a {@link Builder} object for creating this object |
| 65 | + * @since 4.1.0 |
| 66 | + */ |
| 67 | + public static Builder builder() { |
| 68 | + return new AutoValue_MapboxRouteTiles.Builder() |
| 69 | + .baseUrl(Constants.BASE_API_URL); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns the builder which created this instance of {@link MapboxRouteTiles} and allows for |
| 74 | + * modification and building a new route tiles request with new information. |
| 75 | + * |
| 76 | + * @return {@link Builder} with the same variables set as this route tiles object |
| 77 | + * @since 4.1.0 |
| 78 | + */ |
| 79 | + public abstract Builder toBuilder(); |
| 80 | + |
| 81 | + /** |
| 82 | + * This builder is used to create a new request to the Mapbox Route Tiles API. At a bare minimum, |
| 83 | + * your request must include an access token, a {@link BoundingBox}, and a version. |
| 84 | + * |
| 85 | + * @since 4.1.0 |
| 86 | + */ |
| 87 | + @AutoValue.Builder |
| 88 | + public abstract static class Builder { |
| 89 | + |
| 90 | + /** |
| 91 | + * The bounding box of which to download map route tiles. |
| 92 | + * |
| 93 | + * @param boundingBox of which to download map route tiles |
| 94 | + * @return this builder for chaining options together |
| 95 | + * @since 4.1.0 |
| 96 | + */ |
| 97 | + public abstract Builder boundingBox(@NonNull BoundingBox boundingBox); |
| 98 | + |
| 99 | + /** |
| 100 | + * The version of map tiles being requested. To get a list of the versions, use the |
| 101 | + * {@link MapboxRouteTileVersions} API. |
| 102 | + * |
| 103 | + * @param version of which to download |
| 104 | + * @return this builder for chaining options together |
| 105 | + * @since 4.1.0 |
| 106 | + */ |
| 107 | + public abstract Builder version(@NonNull String version); |
| 108 | + |
| 109 | + |
| 110 | + /** |
| 111 | + * Required to call when this is being built. If no access token provided, |
| 112 | + * {@link ServicesException} will be thrown. |
| 113 | + * |
| 114 | + * @param accessToken Mapbox access token, You must have a Mapbox account inorder to use |
| 115 | + * the Route Tiles API |
| 116 | + * @return this builder for chaining options together |
| 117 | + * @since 4.1.0 |
| 118 | + */ |
| 119 | + public abstract Builder accessToken(@NonNull String accessToken); |
| 120 | + |
| 121 | + /** |
| 122 | + * Optionally change the APIs base URL to something other then the default Mapbox one. |
| 123 | + * |
| 124 | + * @param baseUrl base url used as end point |
| 125 | + * @return this builder for chaining options together |
| 126 | + * @since 4.1.0 |
| 127 | + */ |
| 128 | + public abstract Builder baseUrl(@NonNull String baseUrl); |
| 129 | + |
| 130 | + /** |
| 131 | + * Base package name or other simple string identifier. Used inside the calls user agent header. |
| 132 | + * |
| 133 | + * @param clientAppName base package name or other simple string identifier |
| 134 | + * @return this builder for chaining options together |
| 135 | + * @since 4.1.0 |
| 136 | + */ |
| 137 | + public abstract Builder clientAppName(@NonNull String clientAppName); |
| 138 | + |
| 139 | + abstract MapboxRouteTiles autoBuild(); |
| 140 | + |
| 141 | + /** |
| 142 | + * This uses the provided parameters set using the {@link Builder} and first checks that all |
| 143 | + * values are valid, and creates a new {@link MapboxRouteTiles} object with the values provided. |
| 144 | + * |
| 145 | + * @return a new instance of Mapbox Route Tiles |
| 146 | + * @throws ServicesException when a provided parameter is detected to be incorrect |
| 147 | + * @since 4.1.0 |
| 148 | + */ |
| 149 | + public MapboxRouteTiles build() { |
| 150 | + MapboxRouteTiles mapboxRouteTiles = autoBuild(); |
| 151 | + |
| 152 | + if (!MapboxUtils.isAccessTokenValid(mapboxRouteTiles.accessToken())) { |
| 153 | + throw new ServicesException("Using Mapbox Services requires setting a valid access token."); |
| 154 | + } |
| 155 | + |
| 156 | + return mapboxRouteTiles; |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments