Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ApiVersion Support #2462

Merged
merged 6 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protected access as this will be used in the generated client classes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add that (the generated code uses this field to set the header) in the source code comment?

Also if there's any public document available for API versions in https://google.aip.dev, reference it from here.


/** 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);
}
Comment on lines +53 to +75
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a constructor with new ApiVersion param. This allows us to mimic the behavior where the generated clients pass the apiVersion to the AbstractGoogleClientRequest class.

}

@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
Loading