Skip to content

Commit af98897

Browse files
committed
chore: refactor wrapping method helper into a macro
1 parent c440807 commit af98897

File tree

18 files changed

+345
-149
lines changed

18 files changed

+345
-149
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,45 @@ def _get_response(
159159
raise core_exceptions.from_http_response(response)
160160

161161
{% endmacro %}
162+
163+
164+
{% macro prep_wrapped_messages_method(service, is_async=True) %}
165+
def _prep_wrapped_messages(self, client_info):
166+
""" Precompute the wrapped methods, overriding the base class method to use async wrappers."""
167+
self._wrapped_methods = {
168+
{% for method in service.methods.values() %}
169+
self.{{ method.transport_safe_name|snake_case }}: self._wrap_method_with_kind(
170+
self.{{ method.transport_safe_name|snake_case }},
171+
{% if method.retry %}
172+
default_retry=retries.AsyncRetry(
173+
{% if method.retry.initial_backoff %}
174+
initial={{ method.retry.initial_backoff }},
175+
{% endif %}
176+
{% if method.retry.max_backoff %}
177+
maximum={{ method.retry.max_backoff }},
178+
{% endif %}
179+
{% if method.retry.backoff_multiplier %}
180+
multiplier={{ method.retry.backoff_multiplier }},
181+
{% endif %}
182+
predicate=retries.if_exception_type(
183+
{% for ex in method.retry.retryable_exceptions|sort(attribute='__name__') %}
184+
core_exceptions.{{ ex.__name__ }},
185+
{% endfor %}
186+
),
187+
deadline={{ method.timeout }},
188+
),
189+
{% endif %}
190+
default_timeout={{ method.timeout }},
191+
client_info=client_info,
192+
),
193+
{% endfor %}{# service.methods.values() #}
194+
}
195+
{% endmacro %}
196+
197+
{% macro wrap_method_macro() %}
198+
def _wrap_method_with_kind(self, func, *args, **kwargs):
199+
{# TODO: Remove `pragma: NO COVER` once https://github.com/googleapis/python-api-core/pull/688 is merged. #}
200+
if "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters: # pragma: NO COVER
201+
kwargs["kind"] = self.kind
202+
return gapic_v1.method_async.wrap_method(func, *args, **kwargs)
203+
{% endmacro %}

gapic/templates/%namespace/%name_%version/%sub/services/%service/transports/grpc_asyncio.py.j2

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{% extends '_base.py.j2' %}
22

33
{% block content %}
4+
{% import "%namespace/%name_%version/%sub/services/%service/_shared_macros.j2" as shared_macros %}
45

6+
import inspect
57
import warnings
68
from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union
79

@@ -385,39 +387,16 @@ class {{ service.grpc_asyncio_transport_name }}({{ service.name }}Transport):
385387
return self._stubs["test_iam_permissions"]
386388
{% endif %}
387389

388-
def _prep_wrapped_messages(self, client_info):
389-
""" Precompute the wrapped methods, overriding the base class method to use async wrappers."""
390-
self._wrapped_methods = {
391-
{% for method in service.methods.values() %}
392-
self.{{ method.transport_safe_name|snake_case }}: gapic_v1.method_async.wrap_method(
393-
self.{{ method.transport_safe_name|snake_case }},
394-
{% if method.retry %}
395-
default_retry=retries.AsyncRetry(
396-
{% if method.retry.initial_backoff %}
397-
initial={{ method.retry.initial_backoff }},
398-
{% endif %}
399-
{% if method.retry.max_backoff %}
400-
maximum={{ method.retry.max_backoff }},
401-
{% endif %}
402-
{% if method.retry.backoff_multiplier %}
403-
multiplier={{ method.retry.backoff_multiplier }},
404-
{% endif %}
405-
predicate=retries.if_exception_type(
406-
{% for ex in method.retry.retryable_exceptions|sort(attribute='__name__') %}
407-
core_exceptions.{{ ex.__name__ }},
408-
{% endfor %}
409-
),
410-
deadline={{ method.timeout }},
411-
),
412-
{% endif %}
413-
default_timeout={{ method.timeout }},
414-
client_info=client_info,
415-
),
416-
{% endfor %} {# service.methods.values() #}
417-
}
390+
{{ shared_macros.prep_wrapped_messages_method(service)|indent(4) }}
391+
392+
{{ shared_macros.wrap_method_macro()|indent(4) }}
418393

419394
def close(self):
420395
return self.grpc_channel.close()
396+
397+
@property
398+
def kind(self) -> str:
399+
return "grpc_asyncio"
421400

422401
{% include '%namespace/%name_%version/%sub/services/%service/transports/_mixins.py.j2' %}
423402

gapic/templates/tests/unit/gapic/%name_%version/%sub/test_%service.py.j2

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,19 +1041,9 @@ def test_transport_adc(transport_class):
10411041
transport_class()
10421042
adc.assert_called_once()
10431043

1044-
@pytest.mark.parametrize("transport_name", [
1045-
{% if "grpc" in opts.transport %}
1046-
"grpc",
1047-
{% endif %}
1048-
{% if "rest" in opts.transport %}
1049-
"rest",
1050-
{% endif %}
1051-
])
1052-
def test_transport_kind(transport_name):
1053-
transport = {{ service.client_name }}.get_transport_class(transport_name)(
1054-
credentials=ga_credentials.AnonymousCredentials(),
1055-
)
1056-
assert transport.kind == transport_name
1044+
{{ test_macros.transport_kind_test(service, opts) }}
1045+
1046+
{{ test_macros.transport_kind_test(service, opts, is_async=True) }}
10571047

10581048
{% if 'grpc' in opts.transport %}
10591049
def test_transport_grpc_default():

gapic/templates/tests/unit/gapic/%name_%version/%sub/test_macros.j2

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,3 +1876,34 @@ def test_{{ method_name }}_empty_call():
18761876
{% endwith %}{# method_settings #}
18771877
assert args[0] == {{ method.input.ident }}()
18781878
{% endmacro %}
1879+
1880+
1881+
{% macro transport_kind_test(service, opts, is_async=False) %}
1882+
@pytest.mark.parametrize("transport_name", [
1883+
{% if is_async %}
1884+
{% if "grpc" in opts.transport %}
1885+
"grpc_asyncio",
1886+
{% endif %}
1887+
{% else %}{# if not is_async #}
1888+
{% if "grpc" in opts.transport%}
1889+
"grpc",
1890+
{% endif %}
1891+
{% if "rest" in opts.transport %}
1892+
"rest",
1893+
{% endif %}
1894+
{% endif %}{# is_async #}
1895+
])
1896+
{% if is_async %}
1897+
@pytest.mark.asyncio
1898+
async def test_transport_kind_async(transport_name):
1899+
transport = {{ service.async_client_name }}.get_transport_class(transport_name)(
1900+
credentials=async_anonymous_credentials(),
1901+
)
1902+
{% else %}
1903+
def test_transport_kind(transport_name):
1904+
transport = {{ service.client_name }}.get_transport_class(transport_name)(
1905+
credentials=ga_credentials.AnonymousCredentials(),
1906+
)
1907+
{% endif %}
1908+
assert transport.kind == transport_name
1909+
{% endmacro %}

tests/integration/goldens/asset/google/cloud/asset_v1/services/asset_service/transports/grpc_asyncio.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16+
import inspect
1617
import warnings
1718
from typing import Awaitable, Callable, Dict, Optional, Sequence, Tuple, Union
1819

@@ -941,17 +942,17 @@ def analyze_org_policy_governed_assets(self) -> Callable[
941942
def _prep_wrapped_messages(self, client_info):
942943
""" Precompute the wrapped methods, overriding the base class method to use async wrappers."""
943944
self._wrapped_methods = {
944-
self.export_assets: gapic_v1.method_async.wrap_method(
945+
self.export_assets: self._wrap_method_with_kind(
945946
self.export_assets,
946947
default_timeout=60.0,
947948
client_info=client_info,
948949
),
949-
self.list_assets: gapic_v1.method_async.wrap_method(
950+
self.list_assets: self._wrap_method_with_kind(
950951
self.list_assets,
951952
default_timeout=None,
952953
client_info=client_info,
953954
),
954-
self.batch_get_assets_history: gapic_v1.method_async.wrap_method(
955+
self.batch_get_assets_history: self._wrap_method_with_kind(
955956
self.batch_get_assets_history,
956957
default_retry=retries.AsyncRetry(
957958
initial=0.1,
@@ -966,12 +967,12 @@ def _prep_wrapped_messages(self, client_info):
966967
default_timeout=60.0,
967968
client_info=client_info,
968969
),
969-
self.create_feed: gapic_v1.method_async.wrap_method(
970+
self.create_feed: self._wrap_method_with_kind(
970971
self.create_feed,
971972
default_timeout=60.0,
972973
client_info=client_info,
973974
),
974-
self.get_feed: gapic_v1.method_async.wrap_method(
975+
self.get_feed: self._wrap_method_with_kind(
975976
self.get_feed,
976977
default_retry=retries.AsyncRetry(
977978
initial=0.1,
@@ -986,7 +987,7 @@ def _prep_wrapped_messages(self, client_info):
986987
default_timeout=60.0,
987988
client_info=client_info,
988989
),
989-
self.list_feeds: gapic_v1.method_async.wrap_method(
990+
self.list_feeds: self._wrap_method_with_kind(
990991
self.list_feeds,
991992
default_retry=retries.AsyncRetry(
992993
initial=0.1,
@@ -1001,12 +1002,12 @@ def _prep_wrapped_messages(self, client_info):
10011002
default_timeout=60.0,
10021003
client_info=client_info,
10031004
),
1004-
self.update_feed: gapic_v1.method_async.wrap_method(
1005+
self.update_feed: self._wrap_method_with_kind(
10051006
self.update_feed,
10061007
default_timeout=60.0,
10071008
client_info=client_info,
10081009
),
1009-
self.delete_feed: gapic_v1.method_async.wrap_method(
1010+
self.delete_feed: self._wrap_method_with_kind(
10101011
self.delete_feed,
10111012
default_retry=retries.AsyncRetry(
10121013
initial=0.1,
@@ -1021,7 +1022,7 @@ def _prep_wrapped_messages(self, client_info):
10211022
default_timeout=60.0,
10221023
client_info=client_info,
10231024
),
1024-
self.search_all_resources: gapic_v1.method_async.wrap_method(
1025+
self.search_all_resources: self._wrap_method_with_kind(
10251026
self.search_all_resources,
10261027
default_retry=retries.AsyncRetry(
10271028
initial=0.1,
@@ -1036,7 +1037,7 @@ def _prep_wrapped_messages(self, client_info):
10361037
default_timeout=15.0,
10371038
client_info=client_info,
10381039
),
1039-
self.search_all_iam_policies: gapic_v1.method_async.wrap_method(
1040+
self.search_all_iam_policies: self._wrap_method_with_kind(
10401041
self.search_all_iam_policies,
10411042
default_retry=retries.AsyncRetry(
10421043
initial=0.1,
@@ -1051,7 +1052,7 @@ def _prep_wrapped_messages(self, client_info):
10511052
default_timeout=15.0,
10521053
client_info=client_info,
10531054
),
1054-
self.analyze_iam_policy: gapic_v1.method_async.wrap_method(
1055+
self.analyze_iam_policy: self._wrap_method_with_kind(
10551056
self.analyze_iam_policy,
10561057
default_retry=retries.AsyncRetry(
10571058
initial=0.1,
@@ -1065,71 +1066,80 @@ def _prep_wrapped_messages(self, client_info):
10651066
default_timeout=300.0,
10661067
client_info=client_info,
10671068
),
1068-
self.analyze_iam_policy_longrunning: gapic_v1.method_async.wrap_method(
1069+
self.analyze_iam_policy_longrunning: self._wrap_method_with_kind(
10691070
self.analyze_iam_policy_longrunning,
10701071
default_timeout=60.0,
10711072
client_info=client_info,
10721073
),
1073-
self.analyze_move: gapic_v1.method_async.wrap_method(
1074+
self.analyze_move: self._wrap_method_with_kind(
10741075
self.analyze_move,
10751076
default_timeout=None,
10761077
client_info=client_info,
10771078
),
1078-
self.query_assets: gapic_v1.method_async.wrap_method(
1079+
self.query_assets: self._wrap_method_with_kind(
10791080
self.query_assets,
10801081
default_timeout=None,
10811082
client_info=client_info,
10821083
),
1083-
self.create_saved_query: gapic_v1.method_async.wrap_method(
1084+
self.create_saved_query: self._wrap_method_with_kind(
10841085
self.create_saved_query,
10851086
default_timeout=None,
10861087
client_info=client_info,
10871088
),
1088-
self.get_saved_query: gapic_v1.method_async.wrap_method(
1089+
self.get_saved_query: self._wrap_method_with_kind(
10891090
self.get_saved_query,
10901091
default_timeout=None,
10911092
client_info=client_info,
10921093
),
1093-
self.list_saved_queries: gapic_v1.method_async.wrap_method(
1094+
self.list_saved_queries: self._wrap_method_with_kind(
10941095
self.list_saved_queries,
10951096
default_timeout=None,
10961097
client_info=client_info,
10971098
),
1098-
self.update_saved_query: gapic_v1.method_async.wrap_method(
1099+
self.update_saved_query: self._wrap_method_with_kind(
10991100
self.update_saved_query,
11001101
default_timeout=None,
11011102
client_info=client_info,
11021103
),
1103-
self.delete_saved_query: gapic_v1.method_async.wrap_method(
1104+
self.delete_saved_query: self._wrap_method_with_kind(
11041105
self.delete_saved_query,
11051106
default_timeout=None,
11061107
client_info=client_info,
11071108
),
1108-
self.batch_get_effective_iam_policies: gapic_v1.method_async.wrap_method(
1109+
self.batch_get_effective_iam_policies: self._wrap_method_with_kind(
11091110
self.batch_get_effective_iam_policies,
11101111
default_timeout=None,
11111112
client_info=client_info,
11121113
),
1113-
self.analyze_org_policies: gapic_v1.method_async.wrap_method(
1114+
self.analyze_org_policies: self._wrap_method_with_kind(
11141115
self.analyze_org_policies,
11151116
default_timeout=None,
11161117
client_info=client_info,
11171118
),
1118-
self.analyze_org_policy_governed_containers: gapic_v1.method_async.wrap_method(
1119+
self.analyze_org_policy_governed_containers: self._wrap_method_with_kind(
11191120
self.analyze_org_policy_governed_containers,
11201121
default_timeout=None,
11211122
client_info=client_info,
11221123
),
1123-
self.analyze_org_policy_governed_assets: gapic_v1.method_async.wrap_method(
1124+
self.analyze_org_policy_governed_assets: self._wrap_method_with_kind(
11241125
self.analyze_org_policy_governed_assets,
11251126
default_timeout=None,
11261127
client_info=client_info,
11271128
),
1128-
}
1129+
}
1130+
1131+
def _wrap_method_with_kind(self, func, *args, **kwargs):
1132+
if "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters: # pragma: NO COVER
1133+
kwargs["kind"] = self.kind
1134+
return gapic_v1.method_async.wrap_method(func, *args, **kwargs)
11291135

11301136
def close(self):
11311137
return self.grpc_channel.close()
11321138

1139+
@property
1140+
def kind(self) -> str:
1141+
return "grpc_asyncio"
1142+
11331143
@property
11341144
def get_operation(
11351145
self,

tests/integration/goldens/asset/tests/unit/gapic/asset_v1/test_asset_service.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16415,6 +16415,18 @@ def test_transport_kind(transport_name):
1641516415
)
1641616416
assert transport.kind == transport_name
1641716417

16418+
16419+
@pytest.mark.parametrize("transport_name", [
16420+
"grpc_asyncio",
16421+
])
16422+
@pytest.mark.asyncio
16423+
async def test_transport_kind_async(transport_name):
16424+
transport = AssetServiceAsyncClient.get_transport_class(transport_name)(
16425+
credentials=async_anonymous_credentials(),
16426+
)
16427+
assert transport.kind == transport_name
16428+
16429+
1641816430
def test_transport_grpc_default():
1641916431
# A client should use the gRPC transport by default.
1642016432
client = AssetServiceClient(

0 commit comments

Comments
 (0)