Skip to content

Commit

Permalink
feat: Add ApiVersion Support (#2462)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
lqiu96 authored May 9, 2024
1 parent 93d9da1 commit 9f3f64c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.api.client.http.UriTemplate;
import com.google.api.client.util.GenericData;
import com.google.api.client.util.Preconditions;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -62,7 +63,13 @@ public abstract class AbstractGoogleClientRequest<T> extends GenericData {
*/
public static final String USER_AGENT_SUFFIX = "Google-API-Java-Client";

private static final String API_VERSION_HEADER = "X-Goog-Api-Client";
private static final String API_CLIENT_HEADER = "X-Goog-Api-Client";

/**
* The generated request class will pass this constant as part of the header if the RPC supports
* ApiVersion.
*/
protected static final String API_VERSION_HEADER = "X-Google-Api-Version";

/** Google client. */
private final AbstractGoogleClient abstractGoogleClient;
Expand Down Expand Up @@ -133,7 +140,7 @@ protected AbstractGoogleClientRequest(
requestHeaders.setUserAgent(USER_AGENT_SUFFIX + "/" + GoogleUtils.VERSION);
}
// Set the header for the Api Client version (Java and OS version)
requestHeaders.set(API_VERSION_HEADER, ApiClientVersion.DEFAULT_VERSION);
requestHeaders.set(API_CLIENT_HEADER, ApiClientVersion.DEFAULT_VERSION);
}

/**
Expand Down Expand Up @@ -312,6 +319,12 @@ public AbstractGoogleClientRequest<T> setRequestHeaders(HttpHeaders headers) {
return this;
}

/** Returns the ApiVersion set in the headers. If ApiVersion is not set, null is returned. */
@VisibleForTesting
String getApiVersionHeader() {
return (String) requestHeaders.get(API_VERSION_HEADER);
}

/**
* Returns the HTTP headers of the last response or {@code null} before request has been executed.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.UriTemplate;
import com.google.api.client.util.Beta;
import com.google.common.base.Strings;

/**
* {@link Beta} <br>
Expand Down Expand Up @@ -46,7 +47,32 @@ public MockGoogleClientRequest(
String uriTemplate,
HttpContent content,
Class<T> responseClass) {
this(client, method, uriTemplate, content, responseClass, null);
}

/**
* @param client Google client
* @param method HTTP Method
* @param uriTemplate URI template for the path relative to the base URL. If it starts with a "/"
* the base path from the base URL will be stripped out. The URI template can also be a full
* URL. URI template expansion is done using {@link UriTemplate#expand(String, String, Object,
* boolean)}
* @param content HTTP content or {@code null} for none
* @param responseClass response class to parse into
* @param apiVersion ApiVersion to be passed to the header
*/
public MockGoogleClientRequest(
AbstractGoogleClient client,
String method,
String uriTemplate,
HttpContent content,
Class<T> responseClass,
String apiVersion) {
super(client, method, uriTemplate, content, responseClass);
// Matches generator code: Null or Empty String is not set to the header
if (!Strings.isNullOrEmpty(apiVersion)) {
getRequestHeaders().set(API_VERSION_HEADER, apiVersion);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,49 @@ public void testUserAgent() throws IOException {
request.executeUnparsed();
}

public void testSetsApiVersionHeader_apiVersionSet() {
String apiVersion = "testVersion";
HttpTransport transport = new MockHttpTransport();
MockGoogleClient client =
new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null)
.build();
AbstractGoogleClientRequest<Void> request =
new MockGoogleClientRequest<>(
client, HttpMethods.GET, URI_TEMPLATE, null, Void.class, apiVersion);
assertEquals(apiVersion, request.getApiVersionHeader());
}

public void testDoesNotSetsApiVersionHeader_noApiVersionSet() {
HttpTransport transport = new MockHttpTransport();
MockGoogleClient client =
new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null)
.build();
AbstractGoogleClientRequest<Void> request =
new MockGoogleClientRequest<>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class);
assertNull(request.getApiVersionHeader());
}

public void testNullApiVersionHeader_noApiVersionSet() {
HttpTransport transport = new MockHttpTransport();
MockGoogleClient client =
new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null)
.build();
final AbstractGoogleClientRequest<Void> request =
new MockGoogleClientRequest<>(
client, HttpMethods.GET, URI_TEMPLATE, null, Void.class, null);
assertNull(request.getApiVersionHeader());
}

public void testEmptyStringApiVersionHeader_noApiVersionSet() {
HttpTransport transport = new MockHttpTransport();
MockGoogleClient client =
new MockGoogleClient.Builder(transport, ROOT_URL, SERVICE_PATH, JSON_OBJECT_PARSER, null)
.build();
final AbstractGoogleClientRequest<Void> request =
new MockGoogleClientRequest<>(client, HttpMethods.GET, URI_TEMPLATE, null, Void.class, "");
assertNull(request.getApiVersionHeader());
}

public void testSetsApiClientHeader() throws IOException {
HttpTransport transport =
new AssertHeaderTransport("X-Goog-Api-Client", "gl-java/\\d+\\.\\d+\\.\\d+.*");
Expand Down

0 comments on commit 9f3f64c

Please sign in to comment.