File tree Expand file tree Collapse file tree
main/java/com/google/cloud
test/java/com/google/cloud Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1919import static com .google .common .base .MoreObjects .firstNonNull ;
2020
2121import com .google .api .client .extensions .appengine .http .UrlFetchTransport ;
22+ import com .google .api .client .http .HttpHeaders ;
2223import com .google .api .client .http .HttpRequest ;
2324import com .google .api .client .http .HttpRequestInitializer ;
2425import 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).
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 2323
2424import com .google .auth .http .HttpTransportFactory ;
2525import com .google .cloud .HttpTransportOptions .DefaultHttpTransportFactory ;
26+ import java .util .regex .Pattern ;
2627import org .easymock .EasyMock ;
2728import 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}
Original file line number Diff line number Diff line change 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>
You can’t perform that action at this time.
0 commit comments