From aec00f970f4ebfb8b185b6394b7950001207527b Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Wed, 22 May 2019 15:19:41 -0700 Subject: [PATCH 01/10] Add ability to set requestReason and userAgent when configuring the client. Introduces the builder pattern for these common configuration options so that it is extensible in the future. --- .../CommonGoogleClientRequestInitializer.java | 166 +++++++++++++++++- ...monGoogleJsonClientRequestInitializer.java | 27 ++- 2 files changed, 186 insertions(+), 7 deletions(-) diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java b/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java index e14fa4e89..36f7356bf 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java @@ -25,7 +25,9 @@ * *
   public static final GoogleClientRequestInitializer KEY_INITIALIZER =
-      new CommonGoogleClientRequestInitializer(KEY);
+      CommonGoogleClientRequestInitializer.newBuilder()
+          .setKey(KEY)
+          .build();
  * 
* *

@@ -34,7 +36,10 @@ * *

   public static final GoogleClientRequestInitializer INITIALIZER =
-      new CommonGoogleClientRequestInitializer(KEY, USER_IP);
+      CommonGoogleClientRequestInitializer.newBuilder()
+          .setKey(KEY)
+          .setUserIp(USER_IP)
+          .build();
  * 
* *

@@ -87,13 +92,25 @@ public class CommonGoogleClientRequestInitializer implements GoogleClientRequest /** User IP or {@code null} to leave it unchanged. */ private final String userIp; + /** User Agent or {@code null} to leave it unchanged. */ + private final String userAgent; + + /** Reason for request or {@code null} to leave it unchanged. */ + private final String requestReason; + + /** + * @deprecated Please use the builder interface + */ + @Deprecated public CommonGoogleClientRequestInitializer() { - this(null); + this(newBuilder()); } /** * @param key API key or {@code null} to leave it unchanged + * @deprecated Please use the builder interface */ + @Deprecated public CommonGoogleClientRequestInitializer(String key) { this(key, null); } @@ -101,10 +118,25 @@ public CommonGoogleClientRequestInitializer(String key) { /** * @param key API key or {@code null} to leave it unchanged * @param userIp user IP or {@code null} to leave it unchanged + * @deprecated Please use the builder interface */ + @Deprecated public CommonGoogleClientRequestInitializer(String key, String userIp) { - this.key = key; - this.userIp = userIp; + this(newBuilder().setKey(key).setUserIp(userIp)); + } + + protected CommonGoogleClientRequestInitializer(Builder builder) { + this.key = builder.getKey(); + this.userIp = builder.getUserIp(); + this.userAgent = builder.getUserAgent(); + this.requestReason = builder.getRequestReason(); + } + + /** + * Returns new builder + */ + public static Builder newBuilder() { + return new Builder(); } /** @@ -119,6 +151,14 @@ public void initialize(AbstractGoogleClientRequest request) throws IOExceptio if (userIp != null) { request.put("userIp", userIp); } + if (userAgent != null) { + // FIXME(chingor): set the correct header + request.put("userAgent", userAgent); + } + if (requestReason != null) { + // FIXME(chingor): set the correct header + request.put("requestReason", requestReason); + } } /** Returns the API key or {@code null} to leave it unchanged. */ @@ -130,4 +170,120 @@ public final String getKey() { public final String getUserIp() { return userIp; } + + /** Returns the user agent or {@code null} to leave it unchanged. */ + public final String getUserAgent() { + return userAgent; + } + + /** Returns the request reason or {@code null} to leave it unchanged. */ + public final String getRequestReason() { + return requestReason; + } + + /** + * Builder for {@code CommonGoogleClientRequestInitializer}. + */ + public static class Builder { + private String key; + private String userIp; + private String userAgent; + private String requestReason; + + /** + * Set the API Key for outgoing requests + * + * @param key the API key + * @return the builder + */ + public Builder setKey(String key) { + this.key = key; + return self(); + } + + /** + * Returns the API Key + * + * @return the API key + */ + public String getKey() { + return key; + } + + /** + * Set the IP address of the end user for whom the API call is being made + * + * @param userIp the user's IP + * @return the builder + */ + public Builder setUserIp(String userIp) { + this.userIp = userIp; + return self(); + } + + /** + * Returns the configured userIp + * + * @return the userIp + */ + public String getUserIp() { + return userIp; + } + + /** + * Set the user agent + * + * @param userAgent the user agent + * @return the builder + */ + public Builder setUserAgent(String userAgent) { + this.userAgent = userAgent; + return self(); + } + + /** + * Returns the configured user agent + * + * @return the user agent + */ + public String getUserAgent() { + return userAgent; + } + + /** + * Set the reason for making the request, which is intended to be recorded in audit logging. An example reason would + * be a support-case ticket number + * + * @param requestReason the reason for making the request + * @return the builder + */ + public Builder setRequestReason(String requestReason) { + this.requestReason = requestReason; + return self(); + } + + /** + * Get the configured request reason + * + * @return the request reason + */ + public String getRequestReason() { + return requestReason; + } + + /** + * Returns the constructed CommonGoogleClientRequestInitializer instance + * + * @return the constructed CommonGoogleClientRequestInitializer instance + */ + public CommonGoogleClientRequestInitializer build() { + return new CommonGoogleClientRequestInitializer(this); + } + + protected Builder self() { + return this; + } + + protected Builder() {} + } } diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/services/json/CommonGoogleJsonClientRequestInitializer.java b/google-api-client/src/main/java/com/google/api/client/googleapis/services/json/CommonGoogleJsonClientRequestInitializer.java index 162947b8a..4518c471f 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/services/json/CommonGoogleJsonClientRequestInitializer.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/services/json/CommonGoogleJsonClientRequestInitializer.java @@ -28,7 +28,9 @@ * *

   public static final GoogleClientRequestInitializer KEY_INITIALIZER =
-      new CommonGoogleJsonClientRequestInitializer(KEY);
+      CommonGoogleJsonClientRequestInitializer.newBuilder()
+          .setKey(KEY)
+          .build();
  * 
* *

@@ -37,7 +39,10 @@ * *

   public static final GoogleClientRequestInitializer INITIALIZER =
-      new CommonGoogleJsonClientRequestInitializer(KEY, USER_IP);
+      CommonGoogleJsonClientRequestInitializer.newBuilder()
+          .setKey(KEY)
+          .setUserIp(USER_IP)
+          .build();
  * 
* *

@@ -83,13 +88,19 @@ public void initializeJsonRequest( */ public class CommonGoogleJsonClientRequestInitializer extends CommonGoogleClientRequestInitializer { + /** + * @deprecated Please use the builder interface + */ + @Deprecated public CommonGoogleJsonClientRequestInitializer() { super(); } /** * @param key API key or {@code null} to leave it unchanged + * @deprecated Please use the builder interface */ + @Deprecated public CommonGoogleJsonClientRequestInitializer(String key) { super(key); } @@ -97,7 +108,9 @@ public CommonGoogleJsonClientRequestInitializer(String key) { /** * @param key API key or {@code null} to leave it unchanged * @param userIp user IP or {@code null} to leave it unchanged + * @deprecated Please use the builder interface */ + @Deprecated public CommonGoogleJsonClientRequestInitializer(String key, String userIp) { super(key, userIp); } @@ -121,4 +134,14 @@ public final void initialize(AbstractGoogleClientRequest request) throws IOEx protected void initializeJsonRequest(AbstractGoogleJsonClientRequest request) throws IOException { } + + /** + * Builder for {@code CommonGoogleJsonClientRequestInitializer} + */ + public static class Builder extends CommonGoogleClientRequestInitializer.Builder { + @Override + protected Builder self() { + return this; + } + } } From 138ffa619ce27b24397b0ab7ab892206b213d797 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Wed, 22 May 2019 15:48:20 -0700 Subject: [PATCH 02/10] Fix lint --- .../services/CommonGoogleClientRequestInitializer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java b/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java index 36f7356bf..08533dd70 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java @@ -251,8 +251,8 @@ public String getUserAgent() { } /** - * Set the reason for making the request, which is intended to be recorded in audit logging. An example reason would - * be a support-case ticket number + * Set the reason for making the request, which is intended to be recorded in audit logging. An + * example reason would be a support-case ticket number * * @param requestReason the reason for making the request * @return the builder From cc6eb5449eaaae232690ace283379847dc40f50d Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Wed, 22 May 2019 15:53:33 -0700 Subject: [PATCH 03/10] Fix lint --- .../services/CommonGoogleClientRequestInitializer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java b/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java index 08533dd70..3f6dcff7b 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java @@ -152,11 +152,11 @@ public void initialize(AbstractGoogleClientRequest request) throws IOExceptio request.put("userIp", userIp); } if (userAgent != null) { - // FIXME(chingor): set the correct header + // TODO(chingor): set the correct header request.put("userAgent", userAgent); } if (requestReason != null) { - // FIXME(chingor): set the correct header + // TODO(chingor): set the correct header request.put("requestReason", requestReason); } } From 1678fefd4105a6bf40f6ba0720602962a1f76f7a Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Wed, 22 May 2019 16:16:01 -0700 Subject: [PATCH 04/10] javadoc linting --- .../CommonGoogleClientRequestInitializer.java | 22 +++++++++---------- ...monGoogleJsonClientRequestInitializer.java | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java b/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java index 3f6dcff7b..4cfa7bb83 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java @@ -133,7 +133,7 @@ protected CommonGoogleClientRequestInitializer(Builder builder) { } /** - * Returns new builder + * Returns new builder. */ public static Builder newBuilder() { return new Builder(); @@ -191,7 +191,7 @@ public static class Builder { private String requestReason; /** - * Set the API Key for outgoing requests + * Set the API Key for outgoing requests. * * @param key the API key * @return the builder @@ -202,7 +202,7 @@ public Builder setKey(String key) { } /** - * Returns the API Key + * Returns the API key. * * @return the API key */ @@ -211,9 +211,9 @@ public String getKey() { } /** - * Set the IP address of the end user for whom the API call is being made + * Set the IP address of the end user for whom the API call is being made. * - * @param userIp the user's IP + * @param userIp the user's IP address * @return the builder */ public Builder setUserIp(String userIp) { @@ -222,7 +222,7 @@ public Builder setUserIp(String userIp) { } /** - * Returns the configured userIp + * Returns the configured userIp. * * @return the userIp */ @@ -231,7 +231,7 @@ public String getUserIp() { } /** - * Set the user agent + * Set the user agent. * * @param userAgent the user agent * @return the builder @@ -242,7 +242,7 @@ public Builder setUserAgent(String userAgent) { } /** - * Returns the configured user agent + * Returns the configured user agent. * * @return the user agent */ @@ -252,7 +252,7 @@ public String getUserAgent() { /** * Set the reason for making the request, which is intended to be recorded in audit logging. An - * example reason would be a support-case ticket number + * example reason would be a support-case ticket number. * * @param requestReason the reason for making the request * @return the builder @@ -263,7 +263,7 @@ public Builder setRequestReason(String requestReason) { } /** - * Get the configured request reason + * Get the configured request reason. * * @return the request reason */ @@ -272,7 +272,7 @@ public String getRequestReason() { } /** - * Returns the constructed CommonGoogleClientRequestInitializer instance + * Returns the constructed CommonGoogleClientRequestInitializer instance. * * @return the constructed CommonGoogleClientRequestInitializer instance */ diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/services/json/CommonGoogleJsonClientRequestInitializer.java b/google-api-client/src/main/java/com/google/api/client/googleapis/services/json/CommonGoogleJsonClientRequestInitializer.java index 4518c471f..eeabf184e 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/services/json/CommonGoogleJsonClientRequestInitializer.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/services/json/CommonGoogleJsonClientRequestInitializer.java @@ -136,7 +136,7 @@ protected void initializeJsonRequest(AbstractGoogleJsonClientRequest request) } /** - * Builder for {@code CommonGoogleJsonClientRequestInitializer} + * Builder for {@code CommonGoogleJsonClientRequestInitializer}. */ public static class Builder extends CommonGoogleClientRequestInitializer.Builder { @Override From 3a497f2d705bc6ff8f1e3fa25687604bea1f175c Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Thu, 6 Jun 2019 11:53:38 -0700 Subject: [PATCH 05/10] Add userProject configuration --- .../CommonGoogleClientRequestInitializer.java | 52 +++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java b/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java index 4cfa7bb83..4bb5cfc99 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java @@ -86,6 +86,18 @@ public void initialize(AbstractGoogleClientRequest{@literal <}?{@literal >} requ */ public class CommonGoogleClientRequestInitializer implements GoogleClientRequestInitializer { + /** + * Contains a reason for making the request, which is intended to be recorded in audit logging. + * An example reason would be a support-case ticket number. + */ + private static final String REQUEST_REASON_HEADER_NAME = "X-Goog-Request-Reason"; + + /** + * A caller-specified project for quota and billing purposes. The caller must have + * serviceusage.services.use permission on the project. + */ + private static final String USER_PROJECT_HEADER_NAME = "X-Goog-User-Project"; + /** API key or {@code null} to leave it unchanged. */ private final String key; @@ -98,6 +110,9 @@ public class CommonGoogleClientRequestInitializer implements GoogleClientRequest /** Reason for request or {@code null} to leave it unchanged. */ private final String requestReason; + /** Project for quota and billing purposes of {@code null} to leave it unchanged. */ + private final String userProject; + /** * @deprecated Please use the builder interface */ @@ -130,6 +145,7 @@ protected CommonGoogleClientRequestInitializer(Builder builder) { this.userIp = builder.getUserIp(); this.userAgent = builder.getUserAgent(); this.requestReason = builder.getRequestReason(); + this.userProject = builder.getUserProject(); } /** @@ -152,12 +168,13 @@ public void initialize(AbstractGoogleClientRequest request) throws IOExceptio request.put("userIp", userIp); } if (userAgent != null) { - // TODO(chingor): set the correct header - request.put("userAgent", userAgent); + request.getRequestHeaders().setUserAgent(userAgent); } if (requestReason != null) { - // TODO(chingor): set the correct header - request.put("requestReason", requestReason); + request.getRequestHeaders().set(REQUEST_REASON_HEADER_NAME, requestReason); + } + if (userProject != null) { + request.getRequestHeaders().set(USER_PROJECT_HEADER_NAME, userProject); } } @@ -181,6 +198,11 @@ public final String getRequestReason() { return requestReason; } + /** Returns the user project of {@code null} */ + public final String getUserProject() { + return userProject; + } + /** * Builder for {@code CommonGoogleClientRequestInitializer}. */ @@ -189,6 +211,7 @@ public static class Builder { private String userIp; private String userAgent; private String requestReason; + private String userProject; /** * Set the API Key for outgoing requests. @@ -271,6 +294,27 @@ public String getRequestReason() { return requestReason; } + /** + * Set the user project for the request. This is a caller-specified project for quota and + * billing purposes. The caller must have serviceusage.services.use permission on the project. + * + * @param userProject the user project + * @return the builder + */ + public Builder setUserProject(String userProject) { + this.userProject = userProject; + return self(); + } + + /** + * Get the configured user project. + * + * @return the user project + */ + public String getUserProject() { + return userProject; + } + /** * Returns the constructed CommonGoogleClientRequestInitializer instance. * From 170744068ed7d088c038ae4945b3d773dbbff414 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Fri, 7 Jun 2019 11:33:49 -0700 Subject: [PATCH 06/10] Add test for setting user project, request reason, user agent --- ...monGoogleClientRequestInitializerTest.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/google-api-client/src/test/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializerTest.java b/google-api-client/src/test/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializerTest.java index 8c73a0e3a..1973ede1a 100644 --- a/google-api-client/src/test/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializerTest.java +++ b/google-api-client/src/test/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializerTest.java @@ -14,10 +14,12 @@ import com.google.api.client.googleapis.testing.services.MockGoogleClient; import com.google.api.client.http.HttpContent; +import com.google.api.client.http.HttpHeaders; import com.google.api.client.testing.http.HttpTesting; import com.google.api.client.testing.http.MockHttpTransport; import com.google.api.client.util.Key; +import java.io.IOException; import junit.framework.TestCase; /** @@ -46,4 +48,46 @@ public void testInitialize() throws Exception { key.initialize(request); assertEquals("foo", request.key); } + + public void testInitializeSetsUserAgent() throws IOException { + GoogleClientRequestInitializer requestInitializer = CommonGoogleClientRequestInitializer.newBuilder() + .setUserAgent("test user agent") + .build(); + MockGoogleClient client = new MockGoogleClient.Builder(new MockHttpTransport(), HttpTesting.SIMPLE_URL, "test/", null, null) + .setGoogleClientRequestInitializer(requestInitializer) + .setApplicationName("My Application") + .build(); + MyRequest request = new MyRequest(client, "GET", "", null, String.class); + requestInitializer.initialize(request); + HttpHeaders headers = request.getRequestHeaders(); + assertEquals("test user agent", headers.getUserAgent()); + } + + public void testInitializeSetsUserProject() throws IOException { + GoogleClientRequestInitializer requestInitializer = CommonGoogleClientRequestInitializer.newBuilder() + .setUserProject("my quota project") + .build(); + MockGoogleClient client = new MockGoogleClient.Builder(new MockHttpTransport(), HttpTesting.SIMPLE_URL, "test/", null, null) + .setGoogleClientRequestInitializer(requestInitializer) + .setApplicationName("My Application") + .build(); + MyRequest request = new MyRequest(client, "GET", "", null, String.class); + requestInitializer.initialize(request); + HttpHeaders headers = request.getRequestHeaders(); + assertEquals("my quota project", headers.get("X-Goog-User-Project")); + } + + public void testInitializeSetsRequestReason() throws IOException { + GoogleClientRequestInitializer requestInitializer = CommonGoogleClientRequestInitializer.newBuilder() + .setRequestReason("some request reason") + .build(); + MockGoogleClient client = new MockGoogleClient.Builder(new MockHttpTransport(), HttpTesting.SIMPLE_URL, "test/", null, null) + .setGoogleClientRequestInitializer(requestInitializer) + .setApplicationName("My Application") + .build(); + MyRequest request = new MyRequest(client, "GET", "", null, String.class); + requestInitializer.initialize(request); + HttpHeaders headers = request.getRequestHeaders(); + assertEquals("some request reason", headers.get("X-Goog-Request-Reason")); + } } From d4d683eda5d541838cd92b9c50486ef994381a54 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 17 Jun 2019 15:50:32 -0700 Subject: [PATCH 07/10] Implement with autovalue --- google-api-client/pom.xml | 11 + .../CommonGoogleClientRequestInitializer.java | 193 +++--------------- .../services/CommonGoogleOptions.java | 40 ++++ ...monGoogleJsonClientRequestInitializer.java | 10 - ...monGoogleClientRequestInitializerTest.java | 24 ++- pom.xml | 3 +- 6 files changed, 91 insertions(+), 190 deletions(-) create mode 100644 google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleOptions.java diff --git a/google-api-client/pom.xml b/google-api-client/pom.xml index d17c1f9a0..f6042b537 100644 --- a/google-api-client/pom.xml +++ b/google-api-client/pom.xml @@ -104,6 +104,17 @@ com.google.http-client google-http-client-jackson2 + + com.google.auto.value + auto-value-annotations + ${project.auto-value.version} + + + com.google.auto.value + auto-value + ${project.auto-value.version} + provided + com.google.http-client google-http-client-gson diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java b/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java index 4bb5cfc99..9b05f2486 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java @@ -98,27 +98,14 @@ public class CommonGoogleClientRequestInitializer implements GoogleClientRequest */ private static final String USER_PROJECT_HEADER_NAME = "X-Goog-User-Project"; - /** API key or {@code null} to leave it unchanged. */ - private final String key; - - /** User IP or {@code null} to leave it unchanged. */ - private final String userIp; - - /** User Agent or {@code null} to leave it unchanged. */ - private final String userAgent; - - /** Reason for request or {@code null} to leave it unchanged. */ - private final String requestReason; - - /** Project for quota and billing purposes of {@code null} to leave it unchanged. */ - private final String userProject; + private final CommonGoogleOptions options; /** * @deprecated Please use the builder interface */ @Deprecated public CommonGoogleClientRequestInitializer() { - this(newBuilder()); + this(CommonGoogleOptions.newBuilder().build()); } /** @@ -137,22 +124,15 @@ public CommonGoogleClientRequestInitializer(String key) { */ @Deprecated public CommonGoogleClientRequestInitializer(String key, String userIp) { - this(newBuilder().setKey(key).setUserIp(userIp)); - } - - protected CommonGoogleClientRequestInitializer(Builder builder) { - this.key = builder.getKey(); - this.userIp = builder.getUserIp(); - this.userAgent = builder.getUserAgent(); - this.requestReason = builder.getRequestReason(); - this.userProject = builder.getUserProject(); + this(CommonGoogleOptions.newBuilder().setKey(key).setUserIp(userIp).build()); } /** - * Returns new builder. + * + * @param options */ - public static Builder newBuilder() { - return new Builder(); + public CommonGoogleClientRequestInitializer(CommonGoogleOptions options) { + this.options = options; } /** @@ -161,173 +141,46 @@ public static Builder newBuilder() { * @throws IOException I/O exception */ public void initialize(AbstractGoogleClientRequest request) throws IOException { - if (key != null) { - request.put("key", key); + if (getKey() != null) { + request.put("key", getKey()); } - if (userIp != null) { - request.put("userIp", userIp); + if (getUserIp() != null) { + request.put("userIp", getUserIp()); } - if (userAgent != null) { - request.getRequestHeaders().setUserAgent(userAgent); + if (getUserAgent() != null) { + request.getRequestHeaders().setUserAgent(getUserAgent()); } - if (requestReason != null) { - request.getRequestHeaders().set(REQUEST_REASON_HEADER_NAME, requestReason); + if (getRequestReason() != null) { + request.getRequestHeaders().set(REQUEST_REASON_HEADER_NAME, getRequestReason()); } - if (userProject != null) { - request.getRequestHeaders().set(USER_PROJECT_HEADER_NAME, userProject); + if (getUserProject() != null) { + request.getRequestHeaders().set(USER_PROJECT_HEADER_NAME, getUserProject()); } } /** Returns the API key or {@code null} to leave it unchanged. */ public final String getKey() { - return key; + return options.getKey(); } /** Returns the user IP or {@code null} to leave it unchanged. */ public final String getUserIp() { - return userIp; + return options.getUserIp(); } /** Returns the user agent or {@code null} to leave it unchanged. */ public final String getUserAgent() { - return userAgent; + return options.getUserAgent(); } /** Returns the request reason or {@code null} to leave it unchanged. */ public final String getRequestReason() { - return requestReason; + return options.getRequestReason(); } - /** Returns the user project of {@code null} */ + /** Returns the user project or {@code null} to leave it unchanged. */ public final String getUserProject() { - return userProject; + return options.getUserProject(); } - /** - * Builder for {@code CommonGoogleClientRequestInitializer}. - */ - public static class Builder { - private String key; - private String userIp; - private String userAgent; - private String requestReason; - private String userProject; - - /** - * Set the API Key for outgoing requests. - * - * @param key the API key - * @return the builder - */ - public Builder setKey(String key) { - this.key = key; - return self(); - } - - /** - * Returns the API key. - * - * @return the API key - */ - public String getKey() { - return key; - } - - /** - * Set the IP address of the end user for whom the API call is being made. - * - * @param userIp the user's IP address - * @return the builder - */ - public Builder setUserIp(String userIp) { - this.userIp = userIp; - return self(); - } - - /** - * Returns the configured userIp. - * - * @return the userIp - */ - public String getUserIp() { - return userIp; - } - - /** - * Set the user agent. - * - * @param userAgent the user agent - * @return the builder - */ - public Builder setUserAgent(String userAgent) { - this.userAgent = userAgent; - return self(); - } - - /** - * Returns the configured user agent. - * - * @return the user agent - */ - public String getUserAgent() { - return userAgent; - } - - /** - * Set the reason for making the request, which is intended to be recorded in audit logging. An - * example reason would be a support-case ticket number. - * - * @param requestReason the reason for making the request - * @return the builder - */ - public Builder setRequestReason(String requestReason) { - this.requestReason = requestReason; - return self(); - } - - /** - * Get the configured request reason. - * - * @return the request reason - */ - public String getRequestReason() { - return requestReason; - } - - /** - * Set the user project for the request. This is a caller-specified project for quota and - * billing purposes. The caller must have serviceusage.services.use permission on the project. - * - * @param userProject the user project - * @return the builder - */ - public Builder setUserProject(String userProject) { - this.userProject = userProject; - return self(); - } - - /** - * Get the configured user project. - * - * @return the user project - */ - public String getUserProject() { - return userProject; - } - - /** - * Returns the constructed CommonGoogleClientRequestInitializer instance. - * - * @return the constructed CommonGoogleClientRequestInitializer instance - */ - public CommonGoogleClientRequestInitializer build() { - return new CommonGoogleClientRequestInitializer(this); - } - - protected Builder self() { - return this; - } - - protected Builder() {} - } } diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleOptions.java b/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleOptions.java new file mode 100644 index 000000000..7050fd49a --- /dev/null +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleOptions.java @@ -0,0 +1,40 @@ +package com.google.api.client.googleapis.services; + +import com.google.auto.value.AutoValue; +import javax.annotation.Nullable; + +/** + * This class. + */ +@AutoValue +public abstract class CommonGoogleOptions { + @Nullable + abstract String getKey(); + + @Nullable + abstract String getRequestReason(); + + @Nullable + abstract String getUserAgent(); + + @Nullable + abstract String getUserIp(); + + @Nullable + abstract String getUserProject(); + + static CommonGoogleOptions.Builder newBuilder() { + return new AutoValue_CommonGoogleOptions.Builder(); + } + + @AutoValue.Builder + abstract static class Builder { + abstract Builder setKey(String key); + abstract Builder setRequestReason(String requestReason); + abstract Builder setUserAgent(String userAgent); + abstract Builder setUserIp(String userIp); + abstract Builder setUserProject(String userProject); + + abstract CommonGoogleOptions build(); + } +} diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/services/json/CommonGoogleJsonClientRequestInitializer.java b/google-api-client/src/main/java/com/google/api/client/googleapis/services/json/CommonGoogleJsonClientRequestInitializer.java index eeabf184e..3396d7abe 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/services/json/CommonGoogleJsonClientRequestInitializer.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/services/json/CommonGoogleJsonClientRequestInitializer.java @@ -134,14 +134,4 @@ public final void initialize(AbstractGoogleClientRequest request) throws IOEx protected void initializeJsonRequest(AbstractGoogleJsonClientRequest request) throws IOException { } - - /** - * Builder for {@code CommonGoogleJsonClientRequestInitializer}. - */ - public static class Builder extends CommonGoogleClientRequestInitializer.Builder { - @Override - protected Builder self() { - return this; - } - } } diff --git a/google-api-client/src/test/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializerTest.java b/google-api-client/src/test/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializerTest.java index 1973ede1a..0255bf4a4 100644 --- a/google-api-client/src/test/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializerTest.java +++ b/google-api-client/src/test/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializerTest.java @@ -50,9 +50,11 @@ public void testInitialize() throws Exception { } public void testInitializeSetsUserAgent() throws IOException { - GoogleClientRequestInitializer requestInitializer = CommonGoogleClientRequestInitializer.newBuilder() - .setUserAgent("test user agent") - .build(); + GoogleClientRequestInitializer requestInitializer = new CommonGoogleClientRequestInitializer( + CommonGoogleOptions.newBuilder() + .setUserAgent("test user agent") + .build() + ); MockGoogleClient client = new MockGoogleClient.Builder(new MockHttpTransport(), HttpTesting.SIMPLE_URL, "test/", null, null) .setGoogleClientRequestInitializer(requestInitializer) .setApplicationName("My Application") @@ -64,9 +66,11 @@ public void testInitializeSetsUserAgent() throws IOException { } public void testInitializeSetsUserProject() throws IOException { - GoogleClientRequestInitializer requestInitializer = CommonGoogleClientRequestInitializer.newBuilder() - .setUserProject("my quota project") - .build(); + GoogleClientRequestInitializer requestInitializer = new CommonGoogleClientRequestInitializer( + CommonGoogleOptions.newBuilder() + .setUserProject("my quota project") + .build() + ); MockGoogleClient client = new MockGoogleClient.Builder(new MockHttpTransport(), HttpTesting.SIMPLE_URL, "test/", null, null) .setGoogleClientRequestInitializer(requestInitializer) .setApplicationName("My Application") @@ -78,9 +82,11 @@ public void testInitializeSetsUserProject() throws IOException { } public void testInitializeSetsRequestReason() throws IOException { - GoogleClientRequestInitializer requestInitializer = CommonGoogleClientRequestInitializer.newBuilder() - .setRequestReason("some request reason") - .build(); + GoogleClientRequestInitializer requestInitializer = new CommonGoogleClientRequestInitializer( + CommonGoogleOptions.newBuilder() + .setRequestReason("some request reason") + .build() + ); MockGoogleClient client = new MockGoogleClient.Builder(new MockHttpTransport(), HttpTesting.SIMPLE_URL, "test/", null, null) .setGoogleClientRequestInitializer(requestInitializer) .setApplicationName("My Application") diff --git a/pom.xml b/pom.xml index ac2b09f98..004ea44f4 100644 --- a/pom.xml +++ b/pom.xml @@ -313,7 +313,7 @@ org.codehaus.mojo animal-sniffer-maven-plugin - 1.9 + 1.16 org.apache.maven.plugins @@ -512,6 +512,7 @@ 4.0.3 2.5 1.1 + 1.6.5 false From 40cae08fdc396376404347d60b019436df074d65 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 17 Jun 2019 16:09:03 -0700 Subject: [PATCH 08/10] Downgrade autovalue for Java 7 support --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 004ea44f4..adf6daaef 100644 --- a/pom.xml +++ b/pom.xml @@ -512,7 +512,7 @@ 4.0.3 2.5 1.1 - 1.6.5 + 1.4 false From a9bea3fd2d551453109f8dededcc934b270abc4a Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 17 Jun 2019 16:09:09 -0700 Subject: [PATCH 09/10] Update docs --- .../services/CommonGoogleClientRequestInitializer.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java b/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java index 9b05f2486..149afeecc 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/services/CommonGoogleClientRequestInitializer.java @@ -25,9 +25,9 @@ * *

   public static final GoogleClientRequestInitializer KEY_INITIALIZER =
-      CommonGoogleClientRequestInitializer.newBuilder()
+      new CommonGoogleClientRequestInitializer(CommonGoogleOptions.newBuilder()
           .setKey(KEY)
-          .build();
+          .build());
  * 
* *

@@ -36,10 +36,10 @@ * *

   public static final GoogleClientRequestInitializer INITIALIZER =
-      CommonGoogleClientRequestInitializer.newBuilder()
+      new CommonGoogleClientRequestInitializer(CommonGoogleOptions.newBuilder()
           .setKey(KEY)
           .setUserIp(USER_IP)
-          .build();
+          .build());
  * 
* *

From 5f4934754f2c198cc4242113678849c76841178a Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 17 Jun 2019 16:15:32 -0700 Subject: [PATCH 10/10] There is no auto-value-annotations until 1.6.x --- google-api-client/pom.xml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/google-api-client/pom.xml b/google-api-client/pom.xml index f6042b537..f2a53ec28 100644 --- a/google-api-client/pom.xml +++ b/google-api-client/pom.xml @@ -104,11 +104,6 @@ com.google.http-client google-http-client-jackson2 - - com.google.auto.value - auto-value-annotations - ${project.auto-value.version} - com.google.auto.value auto-value