Skip to content

Commit 94a46f4

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

File tree

9 files changed

+226
-136
lines changed

9 files changed

+226
-136
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,44 @@ 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+
if "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters:
200+
kwargs["kind"] = self.kind
201+
return gapic_v1.method_async.wrap_method(func, *args, **kwargs)
202+
{% 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"
421400

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

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:
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"
1142+
11331143
@property
11341144
def get_operation(
11351145
self,

tests/integration/goldens/credentials/google/iam/credentials_v1/services/iam_credentials/transports/grpc_asyncio.py

Lines changed: 15 additions & 5 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

@@ -355,7 +356,7 @@ def sign_jwt(self) -> Callable[
355356
def _prep_wrapped_messages(self, client_info):
356357
""" Precompute the wrapped methods, overriding the base class method to use async wrappers."""
357358
self._wrapped_methods = {
358-
self.generate_access_token: gapic_v1.method_async.wrap_method(
359+
self.generate_access_token: self._wrap_method_with_kind(
359360
self.generate_access_token,
360361
default_retry=retries.AsyncRetry(
361362
initial=0.1,
@@ -370,7 +371,7 @@ def _prep_wrapped_messages(self, client_info):
370371
default_timeout=60.0,
371372
client_info=client_info,
372373
),
373-
self.generate_id_token: gapic_v1.method_async.wrap_method(
374+
self.generate_id_token: self._wrap_method_with_kind(
374375
self.generate_id_token,
375376
default_retry=retries.AsyncRetry(
376377
initial=0.1,
@@ -385,7 +386,7 @@ def _prep_wrapped_messages(self, client_info):
385386
default_timeout=60.0,
386387
client_info=client_info,
387388
),
388-
self.sign_blob: gapic_v1.method_async.wrap_method(
389+
self.sign_blob: self._wrap_method_with_kind(
389390
self.sign_blob,
390391
default_retry=retries.AsyncRetry(
391392
initial=0.1,
@@ -400,7 +401,7 @@ def _prep_wrapped_messages(self, client_info):
400401
default_timeout=60.0,
401402
client_info=client_info,
402403
),
403-
self.sign_jwt: gapic_v1.method_async.wrap_method(
404+
self.sign_jwt: self._wrap_method_with_kind(
404405
self.sign_jwt,
405406
default_retry=retries.AsyncRetry(
406407
initial=0.1,
@@ -415,11 +416,20 @@ def _prep_wrapped_messages(self, client_info):
415416
default_timeout=60.0,
416417
client_info=client_info,
417418
),
418-
}
419+
}
420+
421+
def _wrap_method_with_kind(self, func, *args, **kwargs):
422+
if "kind" in inspect.signature(gapic_v1.method_async.wrap_method).parameters:
423+
kwargs["kind"] = self.kind
424+
return gapic_v1.method_async.wrap_method(func, *args, **kwargs)
419425

420426
def close(self):
421427
return self.grpc_channel.close()
422428

429+
@property
430+
def kind(self) -> str:
431+
return "grpc"
432+
423433

424434
__all__ = (
425435
'IAMCredentialsGrpcAsyncIOTransport',

0 commit comments

Comments
 (0)