Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit01a57a7

Browse files
feat: support with_call for wrapped rpcs (#550)
1 parent533fbde commit01a57a7

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

‎google/api_core/gapic_v1/method.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ def wrap_method(
137137
default_timeout=None,
138138
default_compression=None,
139139
client_info=client_info.DEFAULT_CLIENT_INFO,
140+
*,
141+
with_call=False,
140142
):
141143
"""Wrap an RPC method with common behavior.
142144
@@ -216,13 +218,24 @@ def get_topic(name, timeout=None):
216218
passed as gRPC metadata to the method. If unspecified, then
217219
a sane default will be used. If ``None``, then no user agent
218220
metadata will be provided to the RPC method.
221+
with_call (bool): If True, wrapped grpc.UnaryUnaryMulticallables will
222+
return a tuple of (response, grpc.Call) instead of just the response.
223+
This is useful for extracting trailing metadata from unary calls.
224+
Defaults to False.
219225
220226
Returns:
221227
Callable: A new callable that takes optional ``retry``, ``timeout``,
222228
and ``compression``
223229
arguments and applies the common error mapping, retry, timeout, compression,
224230
and metadata behavior to the low-level RPC method.
225231
"""
232+
ifwith_call:
233+
try:
234+
func=func.with_call
235+
exceptAttributeErrorasexc:
236+
raiseValueError(
237+
"with_call=True is only supported for unary calls."
238+
)fromexc
226239
func=grpc_helpers.wrap_errors(func)
227240
ifclient_infoisnotNone:
228241
user_agent_metadata= [client_info.to_grpc_metadata()]

‎tests/unit/gapic/test_method.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,24 @@ def test_wrap_method_with_overriding_timeout_as_a_number():
201201

202202
assertresult==42
203203
method.assert_called_once_with(timeout=22,metadata=mock.ANY)
204+
205+
206+
deftest_wrap_method_with_call():
207+
method=mock.Mock()
208+
mock_call=mock.Mock()
209+
method.with_call.return_value=42,mock_call
210+
211+
wrapped_method=google.api_core.gapic_v1.method.wrap_method(method,with_call=True)
212+
result=wrapped_method()
213+
assertlen(result)==2
214+
assertresult[0]==42
215+
assertresult[1]==mock_call
216+
217+
218+
deftest_wrap_method_with_call_not_supported():
219+
"""Raises an error if wrapped callable doesn't have with_call method."""
220+
method=lambda:None# noqa: E731
221+
222+
withpytest.raises(ValueError)asexc_info:
223+
google.api_core.gapic_v1.method.wrap_method(method,with_call=True)
224+
assert"with_call=True is only supported for unary calls"instr(exc_info.value)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp