Skip to content

Commit f2f692b

Browse files
committed
chore: refactor setting of auto-populated field in request
1 parent 69a9623 commit f2f692b

7 files changed

Lines changed: 69 additions & 23 deletions

File tree

packages/gapic-generator/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/_shared_macros.j2

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# Do not diverge from the copy of `_shared_macros.j2` in standard templates.
2222
#}
2323

24-
{% macro auto_populate_uuid4_fields(api, method) %}
24+
{% macro auto_populate_uuid4_fields(api, method, is_async=False) %}
2525
{#
2626
Automatically populate UUID4 fields according to
2727
https://google.aip.dev/client-libraries/4235 when the
@@ -34,12 +34,12 @@
3434
{% with method_settings = api.all_method_settings.get(method.meta.address.proto) %}
3535
{% if method_settings is not none %}
3636
{% for auto_populated_field in method_settings.auto_populated_fields %}
37-
{% if method.input.fields[auto_populated_field].proto3_optional %}
38-
if '{{ auto_populated_field }}' not in request:
37+
{% set is_proto3_optional = method.input.fields[auto_populated_field].proto3_optional %}
38+
{% if is_async %}
39+
self._client._setup_request_id(request, '{{ auto_populated_field }}', {{ is_proto3_optional }})
3940
{% else %}
40-
if not request.{{ auto_populated_field }}:
41+
self._setup_request_id(request, '{{ auto_populated_field }}', {{ is_proto3_optional }})
4142
{% endif %}
42-
request.{{ auto_populated_field }} = str(uuid.uuid4())
4343
{% endfor %}
4444
{% endif %}{# if method_settings is not none #}
4545
{% endwith %}{# method_settings #}

packages/gapic-generator/gapic/ads-templates/%namespace/%name/%version/%sub/services/%service/client.py.j2

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,24 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
227227

228228
{% endfor %} {# common resources #}
229229

230+
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|list %}
231+
@staticmethod
232+
def _setup_request_id(request, field_name: str, is_proto3_optional: bool):
233+
"""Populate a UUID4 field in the request if it is not already set.
234+
235+
Args:
236+
request (Union[google.protobuf.message.Message, dict]): The request object.
237+
field_name (str): The name of the field to populate.
238+
is_proto3_optional (bool): Whether the field is proto3 optional.
239+
"""
240+
if is_proto3_optional:
241+
if field_name not in request:
242+
setattr(request, field_name, str(uuid.uuid4()))
243+
else:
244+
if not getattr(request, field_name):
245+
setattr(request, field_name, str(uuid.uuid4()))
246+
{% endif %}
247+
230248
def __init__(self, *,
231249
credentials: Optional[ga_credentials.Credentials] = None,
232250
transport: Optional[Union[str, {{ service.name }}Transport]] = None,

packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/_shared_macros.j2

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515
#}
1616

17-
{% macro auto_populate_uuid4_fields(api, method) %}
17+
{% macro auto_populate_uuid4_fields(api, method, is_async=False) %}
1818
{#
1919
Automatically populate UUID4 fields according to
2020
https://google.aip.dev/client-libraries/4235 when the
@@ -27,12 +27,12 @@
2727
{% with method_settings = api.all_method_settings.get(method.meta.address.proto) %}
2828
{% if method_settings is not none %}
2929
{% for auto_populated_field in method_settings.auto_populated_fields %}
30-
{% if method.input.fields[auto_populated_field].proto3_optional %}
31-
if '{{ auto_populated_field }}' not in request:
30+
{% set is_proto3_optional = method.input.fields[auto_populated_field].proto3_optional %}
31+
{% if is_async %}
32+
self._client._setup_request_id(request, '{{ auto_populated_field }}', {{ is_proto3_optional }})
3233
{% else %}
33-
if not request.{{ auto_populated_field }}:
34+
self._setup_request_id(request, '{{ auto_populated_field }}', {{ is_proto3_optional }})
3435
{% endif %}
35-
request.{{ auto_populated_field }} = str(uuid.uuid4())
3636
{% endfor %}
3737
{% endif %}{# if method_settings is not none #}
3838
{% endwith %}{# method_settings #}

packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/async_client.py.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ class {{ service.async_client_name }}:
395395

396396
{{ shared_macros.create_metadata(method) }}
397397
{{ shared_macros.add_api_version_header_to_metadata(service.version) }}
398-
{{ shared_macros.auto_populate_uuid4_fields(api, method) }}
398+
{{ shared_macros.auto_populate_uuid4_fields(api, method, is_async=True) }}
399399

400400
# Validate the universe domain.
401401
self._client._validate_universe_domain()

packages/gapic-generator/gapic/templates/%namespace/%name_%version/%sub/services/%service/client.py.j2

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,24 @@ class {{ service.client_name }}(metaclass={{ service.client_name }}Meta):
453453
# NOTE (b/349488459): universe validation is disabled until further notice.
454454
return True
455455

456+
{% if api.all_method_settings.values()|map(attribute="auto_populated_fields", default=[])|list %}
457+
@staticmethod
458+
def _setup_request_id(request, field_name: str, is_proto3_optional: bool):
459+
"""Populate a UUID4 field in the request if it is not already set.
460+
461+
Args:
462+
request (Union[google.protobuf.message.Message, dict]): The request object.
463+
field_name (str): The name of the field to populate.
464+
is_proto3_optional (bool): Whether the field is proto3 optional.
465+
"""
466+
if is_proto3_optional:
467+
if field_name not in request:
468+
setattr(request, field_name, str(uuid.uuid4()))
469+
else:
470+
if not getattr(request, field_name):
471+
setattr(request, field_name, str(uuid.uuid4()))
472+
{% endif %}
473+
456474
def _add_cred_info_for_auth_errors(
457475
self,
458476
error: core_exceptions.GoogleAPICallError

packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/async_client.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,7 @@ async def sample_create_job():
621621
)),
622622
)
623623

624-
if not request.request_id:
625-
request.request_id = str(uuid.uuid4())
624+
self._client._setup_request_id(request, 'request_id', False)
626625

627626
# Validate the universe domain.
628627
self._client._validate_universe_domain()
@@ -728,8 +727,7 @@ async def sample_delete_job():
728727
)),
729728
)
730729

731-
if not request.request_id:
732-
request.request_id = str(uuid.uuid4())
730+
self._client._setup_request_id(request, 'request_id', False)
733731

734732
# Validate the universe domain.
735733
self._client._validate_universe_domain()
@@ -831,8 +829,7 @@ async def sample_cancel_job():
831829
)),
832830
)
833831

834-
if not request.request_id:
835-
request.request_id = str(uuid.uuid4())
832+
self._client._setup_request_id(request, 'request_id', False)
836833

837834
# Validate the universe domain.
838835
self._client._validate_universe_domain()

packages/gapic-generator/tests/integration/goldens/storagebatchoperations/google/cloud/storagebatchoperations_v1/services/storage_batch_operations/client.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,22 @@ def _validate_universe_domain(self):
471471
# NOTE (b/349488459): universe validation is disabled until further notice.
472472
return True
473473

474+
@staticmethod
475+
def _setup_request_id(request, field_name: str, is_proto3_optional: bool):
476+
"""Populate a UUID4 field in the request if it is not already set.
477+
478+
Args:
479+
request (Union[google.protobuf.message.Message, dict]): The request object.
480+
field_name (str): The name of the field to populate.
481+
is_proto3_optional (bool): Whether the field is proto3 optional.
482+
"""
483+
if is_proto3_optional:
484+
if field_name not in request:
485+
setattr(request, field_name, str(uuid.uuid4()))
486+
else:
487+
if not getattr(request, field_name):
488+
setattr(request, field_name, str(uuid.uuid4()))
489+
474490
def _add_cred_info_for_auth_errors(
475491
self,
476492
error: core_exceptions.GoogleAPICallError
@@ -1005,8 +1021,7 @@ def sample_create_job():
10051021
)),
10061022
)
10071023

1008-
if not request.request_id:
1009-
request.request_id = str(uuid.uuid4())
1024+
self._setup_request_id(request, 'request_id', False)
10101025

10111026
# Validate the universe domain.
10121027
self._validate_universe_domain()
@@ -1111,8 +1126,7 @@ def sample_delete_job():
11111126
)),
11121127
)
11131128

1114-
if not request.request_id:
1115-
request.request_id = str(uuid.uuid4())
1129+
self._setup_request_id(request, 'request_id', False)
11161130

11171131
# Validate the universe domain.
11181132
self._validate_universe_domain()
@@ -1213,8 +1227,7 @@ def sample_cancel_job():
12131227
)),
12141228
)
12151229

1216-
if not request.request_id:
1217-
request.request_id = str(uuid.uuid4())
1230+
self._setup_request_id(request, 'request_id', False)
12181231

12191232
# Validate the universe domain.
12201233
self._validate_universe_domain()

0 commit comments

Comments
 (0)