public static final GoogleClientRequestInitializer KEY_INITIALIZER = - new CommonGoogleClientRequestInitializer(KEY); + new CommonGoogleClientRequestInitializer(CommonGoogleOptions.newBuilder() + .setKey(KEY) + .build()); ** *
@@ -34,7 +36,10 @@ * *
public static final GoogleClientRequestInitializer INITIALIZER = - new CommonGoogleClientRequestInitializer(KEY, USER_IP); + new CommonGoogleClientRequestInitializer(CommonGoogleOptions.newBuilder() + .setKey(KEY) + .setUserIp(USER_IP) + .build()); ** *
@@ -81,19 +86,33 @@ public void initialize(AbstractGoogleClientRequest{@literal <}?{@literal >} requ */ public class CommonGoogleClientRequestInitializer implements GoogleClientRequestInitializer { - /** API key or {@code null} to leave it unchanged. */ - private final String key; + /** + * 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"; - /** User IP or {@code null} to leave it unchanged. */ - private final String userIp; + private final CommonGoogleOptions options; + /** + * @deprecated Please use the builder interface + */ + @Deprecated public CommonGoogleClientRequestInitializer() { - this(null); + this(CommonGoogleOptions.newBuilder().build()); } /** * @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 +120,19 @@ 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(CommonGoogleOptions.newBuilder().setKey(key).setUserIp(userIp).build()); + } + + /** + * + * @param options + */ + public CommonGoogleClientRequestInitializer(CommonGoogleOptions options) { + this.options = options; } /** @@ -113,21 +141,46 @@ public CommonGoogleClientRequestInitializer(String key, String userIp) { * @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 (getUserIp() != null) { + request.put("userIp", getUserIp()); + } + if (getUserAgent() != null) { + request.getRequestHeaders().setUserAgent(getUserAgent()); } - if (userIp != null) { - request.put("userIp", userIp); + if (getRequestReason() != null) { + request.getRequestHeaders().set(REQUEST_REASON_HEADER_NAME, getRequestReason()); + } + 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 options.getUserAgent(); } + + /** Returns the request reason or {@code null} to leave it unchanged. */ + public final String getRequestReason() { + return options.getRequestReason(); + } + + /** Returns the user project or {@code null} to leave it unchanged. */ + public final String getUserProject() { + return options.getUserProject(); + } + } 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 162947b8a..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 @@ -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);
}
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..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
@@ -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,52 @@ public void testInitialize() throws Exception {
key.initialize(request);
assertEquals("foo", request.key);
}
+
+ public void testInitializeSetsUserAgent() throws IOException {
+ 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")
+ .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 = 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")
+ .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 = 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")
+ .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"));
+ }
}
diff --git a/pom.xml b/pom.xml
index ac2b09f98..adf6daaef 100644
--- a/pom.xml
+++ b/pom.xml
@@ -313,7 +313,7 @@