Skip to content

Commit

Permalink
Update x-goog-api-client header to use gl-java and gdcl tokens (#1354)
Browse files Browse the repository at this point in the history
* Update x-goog-api-client header to use gl-java and gdcl tokens

* Refactor to use constant.

We no longer need to inspect which service client is being used at
runtime so switch to using a constant.
  • Loading branch information
chingor13 authored Aug 19, 2019
1 parent c33ffc3 commit 45a4821
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,47 +131,39 @@ protected AbstractGoogleClientRequest(AbstractGoogleClient abstractGoogleClient,
requestHeaders.setUserAgent(USER_AGENT_SUFFIX);
}
// Set the header for the Api Client version (Java and OS version)
requestHeaders.set(
API_VERSION_HEADER,
ApiClientVersion.getDefault().build(abstractGoogleClient.getClass().getSimpleName())
);
requestHeaders.set(API_VERSION_HEADER, ApiClientVersion.DEFAULT_VERSION);
}

/**
* 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>
* Internal class to help build the X-Goog-Api-Client header. This header identifies the API
* Client version and environment.
*
* <p>See <a href="https://cloud.google.com/apis/docs/system-parameters"></a>
*/
static class ApiClientVersion {
private static final ApiClientVersion DEFAULT_VERSION = new ApiClientVersion();
private final String headerTemplate;
static final String DEFAULT_VERSION = new ApiClientVersion().toString();
private final String versionString;

ApiClientVersion() {
this(getJavaVersion(), OS_NAME.value(), OS_VERSION.value(), GoogleUtils.VERSION);
}

ApiClientVersion(String javaVersion, String osName, String osVersion, String clientVersion) {
StringBuilder sb = new StringBuilder("java/");
StringBuilder sb = new StringBuilder("gl-java/");
sb.append(formatSemver(javaVersion));
sb.append(" http-google-%s/");
sb.append(" gdcl/");
sb.append(formatSemver(clientVersion));
if (osName != null && osVersion != null) {
sb.append(" ");
sb.append(formatName(osName));
sb.append("/");
sb.append(formatSemver(osVersion));
}
this.headerTemplate = sb.toString();
}

String build(String clientName) {
return String.format(headerTemplate, formatName(clientName));
this.versionString = sb.toString();
}

private static ApiClientVersion getDefault() {
return DEFAULT_VERSION;
public String toString() {
return versionString;
}

private static String getJavaVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,43 +214,52 @@ public void testUserAgentSuffix() throws Exception {
request.executeUnparsed();
}

public void testUserAgent() throws Exception {
public void testUserAgent() throws IOException {
AssertUserAgentTransport transport = new AssertUserAgentTransport();
transport.expectedUserAgent = AbstractGoogleClientRequest.USER_AGENT_SUFFIX + " " + HttpRequest.USER_AGENT_SUFFIX;
// Don't specify an Application Name.
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);
new MockGoogleClientRequest<>(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+.*");
public void testSetsApiClientHeader() throws IOException {
HttpTransport transport = new AssertHeaderTransport("X-Goog-Api-Client", "gl-java/\\d+\\.\\d+\\.\\d+.*");
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);
new MockGoogleClientRequest<>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
request.executeUnparsed();
}

public void testSetsApiClientHeaderWithOsVersion() throws Exception {
public void testSetsApiClientHeaderWithOsVersion() {
System.setProperty("os.name", "My OS");
System.setProperty("os.version", "1.2.3");

String version = new ApiClientVersion().build("My Client");
String version = new ApiClientVersion().toString();
assertTrue("Api version should contain the os version", version.matches(".* my-os/1.2.3"));
}

public void testSetsApiClientHeaderWithoutOsVersion() throws Exception {
public void testSetsApiClientHeaderWithoutOsVersion() {
System.setProperty("os.name", "My OS");
System.clearProperty("os.version");
assertNull(System.getProperty("os.version"));

String version = new ApiClientVersion().build("My Client");
String version = new ApiClientVersion().toString();
assertFalse("Api version should not contain the os version", version.matches(".*my-os.*"));
}

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

public void testReturnRawInputStream_defaultFalse() throws Exception {
HttpTransport transport = new MockHttpTransport() {
@Override
Expand Down

0 comments on commit 45a4821

Please sign in to comment.