From af1d430fedb453b30daf0d35062854d88a3f2b4d Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 12 Aug 2019 11:25:27 -0700 Subject: [PATCH 1/2] Update x-goog-api-client header to use gl-java and gdcl tokens --- .../services/AbstractGoogleClientRequest.java | 24 +++++++---------- .../AbstractGoogleClientRequestTest.java | 27 ++++++++++++------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequest.java b/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequest.java index 489bfec94..0ecca672e 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequest.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequest.java @@ -131,31 +131,27 @@ 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.getDefault().toString()); } /** - * Internal class to help build the X-Goog-Api-Client header. This header identifies the - * API Client version and environment. - * - * See + * Internal class to help build the X-Goog-Api-Client header. This header identifies the API + * Client version and environment. * + *

See */ static class ApiClientVersion { private static final ApiClientVersion DEFAULT_VERSION = new ApiClientVersion(); - private final String headerTemplate; + 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(" "); @@ -163,11 +159,11 @@ static class ApiClientVersion { sb.append("/"); sb.append(formatSemver(osVersion)); } - this.headerTemplate = sb.toString(); + this.versionString = sb.toString(); } - String build(String clientName) { - return String.format(headerTemplate, formatName(clientName)); + public String toString() { + return versionString; } private static ApiClientVersion getDefault() { diff --git a/google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequestTest.java b/google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequestTest.java index 458e991d5..3eb4353f4 100644 --- a/google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequestTest.java +++ b/google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequestTest.java @@ -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 request = - new MockGoogleClientRequest(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 request = - new MockGoogleClientRequest(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 request = + new MockGoogleClientRequest<>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class); + request.executeUnparsed(); + } + public void testReturnRawInputStream_defaultFalse() throws Exception { HttpTransport transport = new MockHttpTransport() { @Override From 0928cd3354ea78afbd0d805a285a2ff51b5ca7e4 Mon Sep 17 00:00:00 2001 From: Jeff Ching Date: Mon, 12 Aug 2019 11:28:53 -0700 Subject: [PATCH 2/2] Refactor to use constant. We no longer need to inspect which service client is being used at runtime so switch to using a constant. --- .../googleapis/services/AbstractGoogleClientRequest.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequest.java b/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequest.java index 0ecca672e..f5cbd8f77 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequest.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequest.java @@ -131,7 +131,7 @@ 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().toString()); + requestHeaders.set(API_VERSION_HEADER, ApiClientVersion.DEFAULT_VERSION); } /** @@ -141,7 +141,7 @@ protected AbstractGoogleClientRequest(AbstractGoogleClient abstractGoogleClient, *

See */ static class ApiClientVersion { - private static final ApiClientVersion DEFAULT_VERSION = new ApiClientVersion(); + static final String DEFAULT_VERSION = new ApiClientVersion().toString(); private final String versionString; ApiClientVersion() { @@ -166,10 +166,6 @@ public String toString() { return versionString; } - private static ApiClientVersion getDefault() { - return DEFAULT_VERSION; - } - private static String getJavaVersion() { String version = System.getProperty("java.version"); if (version == null) {