|
| 1 | +{# |
| 2 | + # Copyright (C) 2024 Google LLC |
| 3 | + # |
| 4 | + # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + # you may not use this file except in compliance with the License. |
| 6 | + # You may obtain a copy of the License at |
| 7 | + # |
| 8 | + # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + # |
| 10 | + # Unless required by applicable law or agreed to in writing, software |
| 11 | + # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + # See the License for the specific language governing permissions and |
| 14 | + # limitations under the License. |
| 15 | +#} |
| 16 | + |
| 17 | +{% macro auto_populate_uuid4_fields(api, method) %} |
| 18 | +{# |
| 19 | + Automatically populate UUID4 fields according to |
| 20 | + https://google.aip.dev/client-libraries/4235 when the |
| 21 | + field satisfies either of: |
| 22 | + - The field supports explicit presence and has not been set by the user. |
| 23 | + - The field doesn't support explicit presence, and its value is the empty |
| 24 | + string (i.e. the default value). |
| 25 | + When using this macro, ensure the calling template generates a line `import uuid` |
| 26 | +#} |
| 27 | +{% with method_settings = api.all_method_settings.get(method.meta.address.proto) %} |
| 28 | +{% if method_settings is not none %} |
| 29 | +{% 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: |
| 32 | + {% else %} |
| 33 | + if not request.{{ auto_populated_field }}: |
| 34 | + {% endif %} |
| 35 | + request.{{ auto_populated_field }} = str(uuid.uuid4()) |
| 36 | +{% endfor %} |
| 37 | +{% endif %}{# if method_settings is not none #} |
| 38 | +{% endwith %}{# method_settings #} |
| 39 | +{% endmacro %} |
| 40 | + |
| 41 | +{% macro add_google_api_core_version_header_import(service) %} |
| 42 | +{# |
| 43 | +The `version_header` module was added to `google-api-core` |
| 44 | +in version 2.19.0. |
| 45 | +https://github.com/googleapis/python-api-core/releases/tag/v2.19.0 |
| 46 | +The `try/except` below can be removed once the minimum version of |
| 47 | +`google-api-core` is 2.19.0 or newer. |
| 48 | +#} |
| 49 | +{% if service.version %} |
| 50 | +try: |
| 51 | + from google.api_core import version_header |
| 52 | + HAS_GOOGLE_API_CORE_VERSION_HEADER = True # pragma: NO COVER |
| 53 | +except ImportError: # pragma: NO COVER |
| 54 | + HAS_GOOGLE_API_CORE_VERSION_HEADER = False |
| 55 | +{% endif %}{# service.version #} |
| 56 | +{% endmacro %} |
| 57 | +{% macro add_api_version_header_to_metadata(service) %} |
| 58 | +{# |
| 59 | + Add API Version to metadata as per https://github.com/aip-dev/google.aip.dev/pull/1331. |
| 60 | + When using this macro, ensure the calling template also calls macro |
| 61 | + `add_google_api_core_version_header_import` to add the necessary import statements. |
| 62 | +#} |
| 63 | + {% if service.version %} |
| 64 | + if HAS_GOOGLE_API_CORE_VERSION_HEADER: # pragma: NO COVER |
| 65 | + metadata = tuple(metadata) + ( |
| 66 | + version_header.to_api_version_header("{{ service.version }}"), |
| 67 | + ) |
| 68 | + {% endif %}{# service.version #} |
| 69 | +{% endmacro %} |
0 commit comments