Skip to content

Commit

Permalink
feat: Add ApiVersion support (#20930)
Browse files Browse the repository at this point in the history
* feat: Add ApiVersion support

* chore: Update to set the headers in constructor

* chore: Add empty string check

* chore: Update the template

* chore: Remove not needed file

* chore: Add comment for API_VERSION
  • Loading branch information
lqiu96 authored May 13, 2024
1 parent b3e039d commit 0f9e928
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions generator/src/googleapis/codegen/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,10 @@ def __init__(self, api, name, def_dict, parent=None):
# TODO(user): if rest_path is not set, raise a good error and fail fast.
self.SetTemplateValue('restPath', rest_path)

# Check that ApiVersion field exists and is not empty string ("")
if 'apiVersion' in def_dict and def_dict.get('apiVersion'):
self.SetTemplateValue('apiVersion', def_dict.get('apiVersion'))

# Figure out the input and output types and schemas for this method.
expected_request = self.values.get('request')
if expected_request:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public class {{ method.className }} extends {{ api.className }}Request<{{ method

private static final String REST_PATH = "{{ method.restPath }}";

{% if method.apiVersion %}
private static final String API_VERSION = "{{ method.apiVersion }}";
{% endif %}

{% for param in method.parameters %}
{% if param.pattern %}
private final java.util.regex.Pattern {{ param.constantName }}_PATTERN =
Expand Down Expand Up @@ -136,6 +140,13 @@ public class {{ method.className }} extends {{ api.className }}Request<{{ method
{% if method.supportsMediaDownload %}
initializeMediaDownload();
{% endif %}
{% if method.apiVersion %}
// Does not override the API_VERSION if already configured
com.google.api.client.http.HttpHeaders headers = getRequestHeaders();
if (headers.get(API_VERSION_HEADER) == null) {
headers.set(API_VERSION_HEADER, API_VERSION);
}
{% endif %}
}
{% endfilter %}

Expand Down Expand Up @@ -184,6 +195,13 @@ public class {{ method.className }} extends {{ api.className }}Request<{{ method
com.google.api.client.util.Preconditions.checkNotNull(mediaContent, "Required parameter mediaContent must be specified.");
initializeMediaUpload(mediaContent);
{% call_template _alt_def method=method %}
{% if method.apiVersion %}
// Does not override the API_VERSION if already configured
com.google.api.client.http.HttpHeaders headers = getRequestHeaders();
if (headers.get(API_VERSION_HEADER) == null) {
headers.set(API_VERSION_HEADER, API_VERSION);
}
{% endif %}
}
{% endif %}
{% endfilter %}
Expand Down

0 comments on commit 0f9e928

Please sign in to comment.