Skip to content

Commit afdb204

Browse files
authored
Add x-goog-api-client support to HTTP1.1 (#1790)
1 parent 3f496f3 commit afdb204

4 files changed

Lines changed: 31 additions & 5 deletions

File tree

google-cloud-core/src/main/java/com/google/cloud/HttpTransportOptions.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.base.MoreObjects.firstNonNull;
2020

2121
import com.google.api.client.extensions.appengine.http.UrlFetchTransport;
22+
import com.google.api.client.http.HttpHeaders;
2223
import com.google.api.client.http.HttpRequest;
2324
import com.google.api.client.http.HttpRequestInitializer;
2425
import com.google.api.client.http.HttpTransport;
@@ -154,10 +155,26 @@ public void initialize(HttpRequest httpRequest) throws IOException {
154155
if (readTimeout >= 0) {
155156
httpRequest.setReadTimeout(readTimeout);
156157
}
158+
159+
HttpHeaders headers = httpRequest.getHeaders();
160+
headers.set("x-goog-api-client", getXGoogApiClientHeader());
157161
}
158162
};
159163
}
160164

165+
String getXGoogApiClientHeader() {
166+
return String.format(
167+
"gl-java/%s %s/%s",
168+
getJavaVersion(),
169+
ServiceOptions.getGoogApiClientLibName(),
170+
ServiceOptions.getLibraryVersion());
171+
}
172+
173+
private static String getJavaVersion() {
174+
String javaVersion = Runtime.class.getPackage().getImplementationVersion();
175+
return javaVersion != null ? javaVersion : "";
176+
}
177+
161178
/**
162179
* Returns the timeout in milliseconds to establish a connection. 0 is an infinite timeout, a
163180
* negative number is the default value (20000).

google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,29 +525,29 @@ public TransportOptions getTransportOptions() {
525525
/**
526526
* Returns the application's name as a string in the format {@code gcloud-java/[version]}.
527527
*/
528-
public String getApplicationName() {
528+
public static String getApplicationName() {
529529
return APPLICATION_NAME;
530530
}
531531

532532

533533
/**
534534
* Returns the library's name, {@code gcloud-java}, as a string.
535535
*/
536-
public String getLibraryName() {
536+
public static String getLibraryName() {
537537
return LIBRARY_NAME;
538538
}
539539

540540
/**
541541
* Returns the library's name used by x-goog-api-client header as a string.
542542
*/
543-
public String getGoogApiClientLibName() {
543+
public static String getGoogApiClientLibName() {
544544
return X_GOOGLE_CLIENT_HEADER_NAME;
545545
}
546546

547547
/**
548548
* Returns the library's version as a string.
549549
*/
550-
public String getLibraryVersion() {
550+
public static String getLibraryVersion() {
551551
return LIBRARY_VERSION;
552552
}
553553

google-cloud-core/src/test/java/com/google/cloud/HttpTransportOptionsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import com.google.auth.http.HttpTransportFactory;
2525
import com.google.cloud.HttpTransportOptions.DefaultHttpTransportFactory;
26+
import java.util.regex.Pattern;
2627
import org.easymock.EasyMock;
2728
import org.junit.Test;
2829

@@ -60,4 +61,12 @@ public void testBaseHashCode() {
6061
assertEquals(OPTIONS.hashCode(), OPTIONS_COPY.hashCode());
6162
assertNotEquals(DEFAULT_OPTIONS.hashCode(), OPTIONS.hashCode());
6263
}
64+
65+
@Test
66+
public void testHeader() {
67+
String expectedHeaderPattern = "^gl-java/.* gccl/.*";
68+
assertTrue(Pattern.compile(expectedHeaderPattern)
69+
.matcher(DEFAULT_OPTIONS.getXGoogApiClientHeader())
70+
.find());
71+
}
6372
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
<github.global.server>github</github.global.server>
9393
<google.auth.version>0.6.0</google.auth.version>
9494
<grpc.version>1.0.3</grpc.version>
95-
<gax.version>0.6.0</gax.version>
95+
<gax.version>0.6.1</gax.version>
9696
<generatedProto.version>0.1.5</generatedProto.version>
9797
<core.version>0.10.1-alpha-SNAPSHOT</core.version>
9898
<beta.version>0.10.1-beta-SNAPSHOT</beta.version>

0 commit comments

Comments
 (0)