diff --git a/README.md b/README.md index 267e2a608..3345c8000 100644 --- a/README.md +++ b/README.md @@ -15,13 +15,13 @@ Services] to your server-side Java application. The Java Client for Google Maps Services is a Java Client library for the following Google Maps APIs: - - [Directions API] - - [Distance Matrix API] - - [Elevation API] - - [Geocoding API] - - [Places API] - - [Roads API] - - [Time Zone API] +- [Directions API] +- [Distance Matrix API] +- [Elevation API] +- [Geocoding API] +- [Places API] +- [Roads API] +- [Time Zone API] Keep in mind that the same [terms and conditions](https://developers.google.com/maps/terms) apply to usage of the APIs when they're accessed through this library. @@ -46,30 +46,30 @@ contribute, please read [How to Contribute][contrib]. ## Requirements - - Java 1.7 or later. - - A Google Maps API key. +- Java 1.7 or later. +- A Google Maps API key. ### API keys Each Google Maps Web Service request requires an API key or client ID. API keys are freely available with a Google Account at -https://developers.google.com/console. The type of API key you need is a -**Server key**. +[developers.google.com/console](https://developers.google.com/console). The +type of API key you need is a **Server key**. To get an API key: - 1. Visit https://developers.google.com/console and log in with - a Google Account. + 1. Visit [developers.google.com/console](https://developers.google.com/console) + and log in with a Google Account. 1. Select one of your existing projects, or create a new project. 1. Enable the API(s) you want to use. The Java Client for Google Maps Services accesses the following APIs: - * Directions API - * Distance Matrix API - * Elevation API - * Geocoding API - * Places API - * Roads API - * Time Zone API + - Directions API + - Distance Matrix API + - Elevation API + - Geocoding API + - Places API + - Roads API + - Time Zone API 1. Create a new **Server key**. 1. If you'd like to restrict requests to a specific IP address, do so now. @@ -94,6 +94,7 @@ that will make more sense in other configurations. This will stop a warning mess when you start using `google-maps-services`. ### Maven + ```xml com.google.maps @@ -108,6 +109,7 @@ when you start using `google-maps-services`. ``` ### Gradle + ```groovy repositories { mavenCentral() @@ -124,18 +126,18 @@ Maven Central](https://search.maven.org/#search%7Cga%7C1%7Ca%3A%22google-maps-se ## Developer Documentation -View the [javadoc](https://googlemaps.github.io/google-maps-services-java/v0.2.1/javadoc/). +View the [javadoc](https://googlemaps.github.io/google-maps-services-java/v0.2.2/javadoc/). Additional documentation for the included web services is available at https://developers.google.com/maps/. - - [Directions API] - - [Distance Matrix API] - - [Elevation API] - - [Geocoding API] - - [Places API] - - [Roads API] - - [Time Zone API] +- [Directions API] +- [Distance Matrix API] +- [Elevation API] +- [Geocoding API] +- [Places API] +- [Roads API] +- [Time Zone API] ## Usage @@ -182,8 +184,7 @@ For more usage examples, check out [the tests](src/test/java/com/google/maps/). You can use this client library on Google App Engine with a single code change. ```java -GeoApiContext context = new GeoApiContext.Builder() - .requestHandlerBuilder(new GaeRequestHandler.Builder()) +new GeoApiContext.Builder(new GaeRequestHandler.Builder()) .apiKey("AIza...") .build(); ``` @@ -198,6 +199,7 @@ based strategy. Never sleep between requests again! By default, requests are sent at the expected rate limits for each web service, typically 10 queries per second for free users. If you want to speed up or slow down requests, you can do that too, using `new GeoApiContext.Builder().queryRateLimit(qps).build()`. +Note that you still need to manually handle the [delay between the initial request and successive pages](https://developers.google.com/places/web-service/search#PlaceSearchPaging) when you're paging through multiple result sets. ### Retry on Failure @@ -206,10 +208,10 @@ are returned from the API. To alter or disable automatic retries, see these methods in `GeoApiContext`: -* `.disableRetries()` -* `.maxRetries()` -* `.retryTimeout()` -* `.setIfExceptionIsAllowedToRetry()` +- `.disableRetries()` +- `.maxRetries()` +- `.retryTimeout()` +- `.setIfExceptionIsAllowedToRetry()` ### Client IDs diff --git a/gradle.properties b/gradle.properties index 5e2edd420..783275b46 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -version=0.2.2 +version=0.2.3 # variables required to allow build.gradle to parse, # override in ~/.gradle/gradle.properties diff --git a/src/main/java/com/google/maps/GaeRequestHandler.java b/src/main/java/com/google/maps/GaeRequestHandler.java index 226101378..4c2cc0f50 100644 --- a/src/main/java/com/google/maps/GaeRequestHandler.java +++ b/src/main/java/com/google/maps/GaeRequestHandler.java @@ -126,6 +126,12 @@ public void proxy(Proxy proxy) { throw new RuntimeException("setProxy not implemented for Google App Engine"); } + @Override + public void proxyAuthentication(String proxyUserName, String proxyUserPassword) { + // TODO: Investigate if GAE URL Fetch Service supports setting proxy authentication + throw new RuntimeException("setProxyAuthentication not implemented for Google App Engine"); + } + @Override public RequestHandler build() { return new GaeRequestHandler(); diff --git a/src/main/java/com/google/maps/GeoApiContext.java b/src/main/java/com/google/maps/GeoApiContext.java index 7058cf5bc..e6c7404c7 100644 --- a/src/main/java/com/google/maps/GeoApiContext.java +++ b/src/main/java/com/google/maps/GeoApiContext.java @@ -117,6 +117,8 @@ interface Builder { void proxy(Proxy proxy); + void proxyAuthentication(String proxyUserName, String proxyUserPassword); + RequestHandler build(); } } @@ -482,6 +484,18 @@ public Builder proxy(Proxy proxy) { return this; } + /** + * set authentication for proxy + * + * @param proxyUserName username for proxy authentication + * @param proxyUserPassword username for proxy authentication + * @return Returns this builder for call chaining. + */ + public Builder proxyAuthentication(String proxyUserName, String proxyUserPassword) { + builder.proxyAuthentication(proxyUserName, proxyUserPassword); + return this; + } + /** * Converts this builder into a {@code GeoApiContext}. * diff --git a/src/main/java/com/google/maps/OkHttpRequestHandler.java b/src/main/java/com/google/maps/OkHttpRequestHandler.java index 65c0cfef5..b75a4e43a 100644 --- a/src/main/java/com/google/maps/OkHttpRequestHandler.java +++ b/src/main/java/com/google/maps/OkHttpRequestHandler.java @@ -21,13 +21,18 @@ import com.google.maps.internal.ExceptionsAllowedToRetry; import com.google.maps.internal.OkHttpPendingResult; import com.google.maps.internal.RateLimitExecutorService; +import java.io.IOException; import java.net.Proxy; import java.util.concurrent.TimeUnit; +import okhttp3.Authenticator; +import okhttp3.Credentials; import okhttp3.Dispatcher; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; +import okhttp3.Response; +import okhttp3.Route; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -125,6 +130,25 @@ public void proxy(Proxy proxy) { builder.proxy(proxy); } + @Override + public void proxyAuthentication(String proxyUserName, String proxyUserPassword) { + final String userName = proxyUserName; + final String password = proxyUserPassword; + + builder.proxyAuthenticator( + new Authenticator() { + @Override + public Request authenticate(Route route, Response response) throws IOException { + String credential = Credentials.basic(userName, password); + return response + .request() + .newBuilder() + .header("Proxy-Authorization", credential) + .build(); + } + }); + } + @Override public RequestHandler build() { OkHttpClient client = builder.build();