Skip to content

Commit 9f3f64c

Browse files
authored
feat: Add ApiVersion Support (#2462)
* feat: Add ApiVersion Support * feat: Update tests * chore: Update test cases * chore: Fix lint issues * chore: Change constant access to protected * doc: Add comment for the constant
1 parent 93d9da1 commit 9f3f64c

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

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

+15-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.google.api.client.http.UriTemplate;
3838
import com.google.api.client.util.GenericData;
3939
import com.google.api.client.util.Preconditions;
40+
import com.google.common.annotations.VisibleForTesting;
4041
import com.google.common.base.Joiner;
4142
import java.io.IOException;
4243
import java.io.InputStream;
@@ -62,7 +63,13 @@ public abstract class AbstractGoogleClientRequest<T> extends GenericData {
6263
*/
6364
public static final String USER_AGENT_SUFFIX = "Google-API-Java-Client";
6465

65-
private static final String API_VERSION_HEADER = "X-Goog-Api-Client";
66+
private static final String API_CLIENT_HEADER = "X-Goog-Api-Client";
67+
68+
/**
69+
* The generated request class will pass this constant as part of the header if the RPC supports
70+
* ApiVersion.
71+
*/
72+
protected static final String API_VERSION_HEADER = "X-Google-Api-Version";
6673

6774
/** Google client. */
6875
private final AbstractGoogleClient abstractGoogleClient;
@@ -133,7 +140,7 @@ protected AbstractGoogleClientRequest(
133140
requestHeaders.setUserAgent(USER_AGENT_SUFFIX + "/" + GoogleUtils.VERSION);
134141
}
135142
// Set the header for the Api Client version (Java and OS version)
136-
requestHeaders.set(API_VERSION_HEADER, ApiClientVersion.DEFAULT_VERSION);
143+
requestHeaders.set(API_CLIENT_HEADER, ApiClientVersion.DEFAULT_VERSION);
137144
}
138145

139146
/**
@@ -312,6 +319,12 @@ public AbstractGoogleClientRequest<T> setRequestHeaders(HttpHeaders headers) {
312319
return this;
313320
}
314321

322+
/** Returns the ApiVersion set in the headers. If ApiVersion is not set, null is returned. */
323+
@VisibleForTesting
324+
String getApiVersionHeader() {
325+
return (String) requestHeaders.get(API_VERSION_HEADER);
326+
}
327+
315328
/**
316329
* Returns the HTTP headers of the last response or {@code null} before request has been executed.
317330
*/

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

+26
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.api.client.http.HttpHeaders;
1919
import com.google.api.client.http.UriTemplate;
2020
import com.google.api.client.util.Beta;
21+
import com.google.common.base.Strings;
2122

2223
/**
2324
* {@link Beta} <br>
@@ -46,7 +47,32 @@ public MockGoogleClientRequest(
4647
String uriTemplate,
4748
HttpContent content,
4849
Class<T> responseClass) {
50+
this(client, method, uriTemplate, content, responseClass, null);
51+
}
52+
53+
/**
54+
* @param client Google client
55+
* @param method HTTP Method
56+
* @param uriTemplate URI template for the path relative to the base URL. If it starts with a "/"
57+
* the base path from the base URL will be stripped out. The URI template can also be a full
58+
* URL. URI template expansion is done using {@link UriTemplate#expand(String, String, Object,
59+
* boolean)}
60+
* @param content HTTP content or {@code null} for none
61+
* @param responseClass response class to parse into
62+
* @param apiVersion ApiVersion to be passed to the header
63+
*/
64+
public MockGoogleClientRequest(
65+
AbstractGoogleClient client,
66+
String method,
67+
String uriTemplate,
68+
HttpContent content,
69+
Class<T> responseClass,
70+
String apiVersion) {
4971
super(client, method, uriTemplate, content, responseClass);
72+
// Matches generator code: Null or Empty String is not set to the header
73+
if (!Strings.isNullOrEmpty(apiVersion)) {
74+
getRequestHeaders().set(API_VERSION_HEADER, apiVersion);
75+
}
5076
}
5177

5278
@Override

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

+43
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,49 @@ public void testUserAgent() throws IOException {
242242
request.executeUnparsed();
243243
}
244244

245+
public void testSetsApiVersionHeader_apiVersionSet() {
246+
String apiVersion = "testVersion";
247+
HttpTransport transport = new MockHttpTransport();
248+
MockGoogleClient client =
249+
new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null)
250+
.build();
251+
AbstractGoogleClientRequest<Void> request =
252+
new MockGoogleClientRequest<>(
253+
client, HttpMethods.GET, URI_TEMPLATE, null, Void.class, apiVersion);
254+
assertEquals(apiVersion, request.getApiVersionHeader());
255+
}
256+
257+
public void testDoesNotSetsApiVersionHeader_noApiVersionSet() {
258+
HttpTransport transport = new MockHttpTransport();
259+
MockGoogleClient client =
260+
new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null)
261+
.build();
262+
AbstractGoogleClientRequest<Void> request =
263+
new MockGoogleClientRequest<>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
264+
assertNull(request.getApiVersionHeader());
265+
}
266+
267+
public void testNullApiVersionHeader_noApiVersionSet() {
268+
HttpTransport transport = new MockHttpTransport();
269+
MockGoogleClient client =
270+
new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null)
271+
.build();
272+
final AbstractGoogleClientRequest<Void> request =
273+
new MockGoogleClientRequest<>(
274+
client, HttpMethods.GET, URI_TEMPLATE, null, Void.class, null);
275+
assertNull(request.getApiVersionHeader());
276+
}
277+
278+
public void testEmptyStringApiVersionHeader_noApiVersionSet() {
279+
HttpTransport transport = new MockHttpTransport();
280+
MockGoogleClient client =
281+
new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null)
282+
.build();
283+
final AbstractGoogleClientRequest<Void> request =
284+
new MockGoogleClientRequest<>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class, "");
285+
assertNull(request.getApiVersionHeader());
286+
}
287+
245288
public void testSetsApiClientHeader() throws IOException {
246289
HttpTransport transport =
247290
new AssertHeaderTransport("X-Goog-Api-Client", "gl-java/\\d+\\.\\d+\\.\\d+.*");

0 commit comments

Comments
 (0)