Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extensible client options using autovalue #1326

Closed
6 changes: 6 additions & 0 deletions google-api-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
</dependency>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>${project.auto-value.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-gson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
*
* <pre>
public static final GoogleClientRequestInitializer KEY_INITIALIZER =
new CommonGoogleClientRequestInitializer(KEY);
new CommonGoogleClientRequestInitializer(CommonGoogleOptions.newBuilder()
.setKey(KEY)
.build());
* </pre>
*
* <p>
Expand All @@ -34,7 +36,10 @@
*
* <pre>
public static final GoogleClientRequestInitializer INITIALIZER =
new CommonGoogleClientRequestInitializer(KEY, USER_IP);
new CommonGoogleClientRequestInitializer(CommonGoogleOptions.newBuilder()
.setKey(KEY)
.setUserIp(USER_IP)
.build());
* </pre>
*
* <p>
Expand Down Expand Up @@ -81,30 +86,53 @@ 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);
}

/**
* @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;
}

/**
Expand All @@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
*
* <pre>
public static final GoogleClientRequestInitializer KEY_INITIALIZER =
new CommonGoogleJsonClientRequestInitializer(KEY);
CommonGoogleJsonClientRequestInitializer.newBuilder()
.setKey(KEY)
.build();
* </pre>
*
* <p>
Expand All @@ -37,7 +39,10 @@
*
* <pre>
public static final GoogleClientRequestInitializer INITIALIZER =
new CommonGoogleJsonClientRequestInitializer(KEY, USER_IP);
CommonGoogleJsonClientRequestInitializer.newBuilder()
.setKey(KEY)
.setUserIp(USER_IP)
.build();
* </pre>
*
* <p>
Expand Down Expand Up @@ -83,21 +88,29 @@ 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);
}

/**
* @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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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"));
}
}
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
<version>1.9</version>
<version>1.16</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -512,6 +512,7 @@
<project.datanucleus-maven-plugin.version>4.0.3</project.datanucleus-maven-plugin.version>
<project.servlet-api.version>2.5</project.servlet-api.version>
<project.transaction-api.version>1.1</project.transaction-api.version>
<project.auto-value.version>1.4</project.auto-value.version>
<deploy.autorelease>false</deploy.autorelease>
</properties>

Expand Down