Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make gapic's wrapper accept generic metadata #4339

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions api_core/google/api_core/gapic_v1/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,17 @@ class _GapicCallable(object):
timeout (google.api_core.timeout.Timeout): The default timeout
for the callable. If ``None``, this callable will not specify
a timeout argument to the low-level RPC method by default.
user_agent_metadata (Tuple[str, str]): The user agent metadata key and
value to provide to the RPC method. If ``None``, no additional
metadata will be passed to the RPC method.
metadata (Sequence[Tuple[str, str]]): Additional metadata that is
provided to the RPC method on every invocation. This is merged with
any metadata specified during invocation. If ``None``, no
additional metadata will be passed to the RPC method.
"""

def __init__(self, target, retry, timeout, user_agent_metadata=None):
def __init__(self, target, retry, timeout, metadata=None):
self._target = target
self._retry = retry
self._timeout = timeout
self._user_agent_metadata = user_agent_metadata
self._metadata = metadata

def __call__(self, *args, **kwargs):
"""Invoke the low-level RPC with retry, timeout, and metadata."""
Expand All @@ -126,9 +127,9 @@ def __call__(self, *args, **kwargs):
wrapped_func = _apply_decorators(self._target, [retry, timeout_])

# Add the user agent metadata to the call.
if self._user_agent_metadata is not None:
metadata = kwargs.get('metadata', [])
metadata.append(self._user_agent_metadata)
if self._metadata is not None:
metadata = list(kwargs.get('metadata', []))
metadata.extend(self._metadata)
kwargs['metadata'] = metadata

return wrapped_func(*args, **kwargs)
Expand Down Expand Up @@ -219,11 +220,11 @@ def get_topic(name, timeout=None):
func = grpc_helpers.wrap_errors(func)

if client_info is not None:
user_agent_metadata = client_info.to_grpc_metadata()
user_agent_metadata = [client_info.to_grpc_metadata()]
else:
user_agent_metadata = None

return general_helpers.wraps(func)(
_GapicCallable(
func, default_retry, default_timeout,
user_agent_metadata=user_agent_metadata))
metadata=user_agent_metadata))
15 changes: 15 additions & 0 deletions api_core/tests/unit/gapic/test_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ def test_wrap_method_with_custom_client_info():
assert client_info.to_grpc_metadata() in metadata


def test_invoke_wrapped_method_with_metadata():
method = mock.Mock(spec=['__call__'])

wrapped_method = google.api_core.gapic_v1.method.wrap_method(method)

wrapped_method(mock.sentinel.request, metadata=[('a', 'b')])

method.assert_called_once_with(mock.sentinel.request, metadata=mock.ANY)
metadata = method.call_args[1]['metadata']
# Metadata should have two items: the client info metadata and our custom
# metadata.
assert len(metadata) == 2
assert ('a', 'b') in metadata


@mock.patch('time.sleep')
def test_wrap_method_with_default_retry_and_timeout(unusued_sleep):
method = mock.Mock(
Expand Down