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 #20930

Merged
merged 8 commits into from
May 13, 2024
Merged

feat: Add ApiVersion support #20930

merged 8 commits into from
May 13, 2024

Conversation

lqiu96
Copy link
Contributor

@lqiu96 lqiu96 commented May 6, 2024

Generator changes that depends on Google-Api-Java-Client changes in googleapis/google-api-java-client#2462

Example of the generated code:

...
          private static final String API_VERSION = "v1_20230601";
...
          protected List(java.lang.String parent) {
            super(DeltaVExample.this, "GET", REST_PATH, null, com.google.api.services.dev_deltav_pa_sandbox.v1.model.ListPlanetsResponse.class);
            this.parent = com.google.api.client.util.Preconditions.checkNotNull(parent, "Required parameter parent must be specified.");
            if (!getSuppressPatternChecks()) {
              com.google.api.client.util.Preconditions.checkArgument(PARENT_PATTERN.matcher(parent).matches(),
                  "Parameter parent must conform to the pattern " +
                  "^projects/[^/]+/locations/[^/]+$");
            }
            getRequestHeaders().set(API_VERSION_HEADER, API_VERSION);
          }

Comment on lines +144 to +147
com.google.api.client.http.HttpHeaders headers = getRequestHeaders();
if (headers.get(API_VERSION_HEADER) == null) {
headers.set(API_VERSION_HEADER, API_VERSION);
}
Copy link
Contributor Author

@lqiu96 lqiu96 May 10, 2024

Choose a reason for hiding this comment

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

If the ApiVersion header has already been set, do not override with the version. This is possible in two different ways:

  1. Using the AbstractRequest's setRequestHeader(): https://github.com/googleapis/google-api-java-client/blob/fbbc37446b2a34b03ca5401bbe9ca038ff087972/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequest.java#L317-L319
  2. From a few handwritten Apiary wrapped libraries (i.e. Storage and Bigquery) as Java-Core provides an option to set a HeaderProvider via ServiceOptions: https://github.com/googleapis/sdk-platform-java/blob/e1e1fb66d95adc08baea71c5a0607c20d5a57a8b/java-core/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java#L281-L303

Customers can technically set the X-Goog-Api-Version header themselves via the two options above and this implementation will respect those custom values.

Copy link
Member

Choose a reason for hiding this comment

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

Nice. Can you record that as source code comment at line 145?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, will add a small comment about not override user set value

@lqiu96 lqiu96 requested review from suztomo and diegomarquezp May 10, 2024 17:07
@lqiu96 lqiu96 marked this pull request as ready for review May 10, 2024 17:09
@lqiu96 lqiu96 requested a review from a team as a code owner May 10, 2024 17:09
@lqiu96 lqiu96 merged commit 0f9e928 into main May 13, 2024
6 checks passed
@lqiu96 lqiu96 deleted the add-apiversion-support branch May 13, 2024 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants