Skip to content

Commit 45a4821

Browse files
authored
Update x-goog-api-client header to use gl-java and gdcl tokens (#1354)
* 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.
1 parent c33ffc3 commit 45a4821

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequest.java

+11-19
Original file line numberDiff line numberDiff line change
@@ -131,47 +131,39 @@ protected AbstractGoogleClientRequest(AbstractGoogleClient abstractGoogleClient,
131131
requestHeaders.setUserAgent(USER_AGENT_SUFFIX);
132132
}
133133
// Set the header for the Api Client version (Java and OS version)
134-
requestHeaders.set(
135-
API_VERSION_HEADER,
136-
ApiClientVersion.getDefault().build(abstractGoogleClient.getClass().getSimpleName())
137-
);
134+
requestHeaders.set(API_VERSION_HEADER, ApiClientVersion.DEFAULT_VERSION);
138135
}
139136

140137
/**
141-
* Internal class to help build the X-Goog-Api-Client header. This header identifies the
142-
* API Client version and environment.
143-
*
144-
* See <a href="https://cloud.google.com/apis/docs/system-parameters"></a>
138+
* Internal class to help build the X-Goog-Api-Client header. This header identifies the API
139+
* Client version and environment.
145140
*
141+
* <p>See <a href="https://cloud.google.com/apis/docs/system-parameters"></a>
146142
*/
147143
static class ApiClientVersion {
148-
private static final ApiClientVersion DEFAULT_VERSION = new ApiClientVersion();
149-
private final String headerTemplate;
144+
static final String DEFAULT_VERSION = new ApiClientVersion().toString();
145+
private final String versionString;
150146

151147
ApiClientVersion() {
152148
this(getJavaVersion(), OS_NAME.value(), OS_VERSION.value(), GoogleUtils.VERSION);
153149
}
154150

155151
ApiClientVersion(String javaVersion, String osName, String osVersion, String clientVersion) {
156-
StringBuilder sb = new StringBuilder("java/");
152+
StringBuilder sb = new StringBuilder("gl-java/");
157153
sb.append(formatSemver(javaVersion));
158-
sb.append(" http-google-%s/");
154+
sb.append(" gdcl/");
159155
sb.append(formatSemver(clientVersion));
160156
if (osName != null && osVersion != null) {
161157
sb.append(" ");
162158
sb.append(formatName(osName));
163159
sb.append("/");
164160
sb.append(formatSemver(osVersion));
165161
}
166-
this.headerTemplate = sb.toString();
167-
}
168-
169-
String build(String clientName) {
170-
return String.format(headerTemplate, formatName(clientName));
162+
this.versionString = sb.toString();
171163
}
172164

173-
private static ApiClientVersion getDefault() {
174-
return DEFAULT_VERSION;
165+
public String toString() {
166+
return versionString;
175167
}
176168

177169
private static String getJavaVersion() {

google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequestTest.java

+18-9
Original file line numberDiff line numberDiff line change
@@ -214,43 +214,52 @@ public void testUserAgentSuffix() throws Exception {
214214
request.executeUnparsed();
215215
}
216216

217-
public void testUserAgent() throws Exception {
217+
public void testUserAgent() throws IOException {
218218
AssertUserAgentTransport transport = new AssertUserAgentTransport();
219219
transport.expectedUserAgent = AbstractGoogleClientRequest.USER_AGENT_SUFFIX + " " + HttpRequest.USER_AGENT_SUFFIX;
220220
// Don't specify an Application Name.
221221
MockGoogleClient client = new MockGoogleClient.Builder(
222222
transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).build();
223223
MockGoogleClientRequest<Void> request =
224-
new MockGoogleClientRequest<Void>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
224+
new MockGoogleClientRequest<>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
225225
request.executeUnparsed();
226226
}
227227

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

237-
public void testSetsApiClientHeaderWithOsVersion() throws Exception {
237+
public void testSetsApiClientHeaderWithOsVersion() {
238238
System.setProperty("os.name", "My OS");
239239
System.setProperty("os.version", "1.2.3");
240240

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

245-
public void testSetsApiClientHeaderWithoutOsVersion() throws Exception {
245+
public void testSetsApiClientHeaderWithoutOsVersion() {
246246
System.setProperty("os.name", "My OS");
247247
System.clearProperty("os.version");
248248
assertNull(System.getProperty("os.version"));
249249

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

254+
public void testSetsApiClientHeaderDiscoveryVersion() throws IOException {
255+
HttpTransport transport = new AssertHeaderTransport("X-Goog-Api-Client", ".*gdcl/\\d+\\.\\d+\\.\\d+.*");
256+
MockGoogleClient client = new MockGoogleClient.Builder(
257+
transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null).build();
258+
MockGoogleClientRequest<Void> request =
259+
new MockGoogleClientRequest<>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
260+
request.executeUnparsed();
261+
}
262+
254263
public void testReturnRawInputStream_defaultFalse() throws Exception {
255264
HttpTransport transport = new MockHttpTransport() {
256265
@Override

0 commit comments

Comments
 (0)