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

Add X-Goog-Api-Client header #1146

Merged
merged 2 commits into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

package com.google.api.client.googleapis.services;

import com.google.api.client.googleapis.GoogleUtils;
import com.google.api.client.googleapis.MethodOverride;
import com.google.api.client.googleapis.batch.BatchCallback;
import com.google.api.client.googleapis.batch.BatchRequest;
Expand All @@ -37,6 +38,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Abstract Google client request for a {@link AbstractGoogleClient}.
Expand All @@ -59,6 +62,8 @@ public abstract class AbstractGoogleClientRequest<T> extends GenericData {
*/
public static final String USER_AGENT_SUFFIX = "Google-API-Java-Client";

private static final String API_VERSION_HEADER = "X-Goog-Api-Client";

/** Google client. */
private final AbstractGoogleClient abstractGoogleClient;

Expand Down Expand Up @@ -119,6 +124,48 @@ protected AbstractGoogleClientRequest(AbstractGoogleClient abstractGoogleClient,
} else {
requestHeaders.setUserAgent(USER_AGENT_SUFFIX);
}
// Set the header for the Api Client version (Java and OS version)
requestHeaders.set(API_VERSION_HEADER, ApiClientVersion.build(abstractGoogleClient));
}

/**
* Internal class to help build the X-Goog-Api-Client header. This header identifies the
* API Client version and environment.
*
* See <a href="https://cloud.google.com/apis/docs/system-parameters"></a>
*
*/
private static class ApiClientVersion {
private static final String JAVA_VERSION = formatSemver(System.getProperty("java.version"));
private static final String OS_NAME = formatName(System.getProperty("os.name"));
private static final String OS_VERSION = formatSemver(System.getProperty("os.version"));

private static String build(AbstractGoogleClient client) {
// TODO(chingor): add the API version from the generated client

This comment was marked as spam.

return String.format(
"java/%s http-google-%s/%s %s/%s",
JAVA_VERSION,
formatName(client.getClass().getSimpleName()),
formatSemver(GoogleUtils.VERSION),
OS_NAME,
OS_VERSION
);
}

private static String formatName(String name) {
// Only lowercase letters, digits, and "-" are allowed
return name.toLowerCase().replaceAll("[^\\w\\d\\-]", "-");
}

private static String formatSemver(String version) {
// Take only the semver version: x.y.z-a_b_c -> x.y.z
Matcher m = Pattern.compile("(\\d+\\.\\d+\\.\\d+).*").matcher(version);
if (m.find()) {
return m.group(1);
} else {
return version;
}
}
}

/** Returns whether to disable GZip compression of HTTP content. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ public LowLevelHttpResponse execute() {

public void testUserAgentSuffix() throws Exception {
AssertUserAgentTransport transport = new AssertUserAgentTransport();

// Specify an Application Name.
String applicationName = "Test Application";
transport.expectedUserAgent = applicationName + " "
Expand All @@ -187,17 +186,49 @@ public void testUserAgentSuffix() throws Exception {
MockGoogleClientRequest<Void> request =
new MockGoogleClientRequest<Void>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
request.executeUnparsed();
}

public void testUserAgent() throws Exception {
AssertUserAgentTransport transport = new AssertUserAgentTransport();
transport.expectedUserAgent = AbstractGoogleClientRequest.USER_AGENT_SUFFIX + " " + HttpRequest.USER_AGENT_SUFFIX;
// Don't specify an Application Name.
transport.expectedUserAgent = AbstractGoogleClientRequest.USER_AGENT_SUFFIX + " "
+ HttpRequest.USER_AGENT_SUFFIX;
client = new MockGoogleClient.Builder(
MockGoogleClient client = new MockGoogleClient.Builder(
transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).build();
MockGoogleClientRequest<Void> request =
new MockGoogleClientRequest<Void>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
request.executeUnparsed();
}

public void testSetsApiClientHeader() throws Exception {
HttpTransport transport = new AssertHeaderTransport("X-Goog-Api-Client", "java/\\d+\\.\\d+\\.\\d+.*");
MockGoogleClient client = new MockGoogleClient.Builder(
transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).build();
request =
MockGoogleClientRequest<Void> request =
new MockGoogleClientRequest<Void>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
request.executeUnparsed();
}

private class AssertHeaderTransport extends MockHttpTransport {
String expectedHeader;
String expectedHeaderValue;

AssertHeaderTransport(String header, String value) {
expectedHeader = header;
expectedHeaderValue = value;
}

@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
assertTrue(getFirstHeaderValue(expectedHeader).matches(expectedHeaderValue));
return new MockLowLevelHttpResponse();
}
};
}
}

private class AssertUserAgentTransport extends MockHttpTransport {
String expectedUserAgent;

Expand Down