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

Add grpc_helpers.create_channel #4069

Merged
merged 2 commits into from
Sep 27, 2017
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
29 changes: 29 additions & 0 deletions core/google/api/core/helpers/grpc_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import six

from google.api.core import exceptions
import google.auth
import google.auth.transport.grpc
import google.auth.transport.requests


# The list of gRPC Callable interfaces that return iterators.
Expand Down Expand Up @@ -102,3 +105,29 @@ def wrap_errors(callable_):
return _wrap_stream_errors(callable_)
else:
return _wrap_unary_errors(callable_)


def create_channel(target, credentials=None, scopes=None, **kwargs):
"""Create a secure channel with credentials.

Args:
target (str): The target service address in the format 'hostname:port'.
credentials (google.auth.credentials.Credentials): The credentials. If
not specified, then this function will attempt to ascertain the
credentials from the environment using :func:`google.auth.default`.
scopes (Sequence[str]): A optional list of scopes needed for this
service. These are only used when credentials are not specified and
are passed to :func:`google.auth.default`.
kwargs: Additional key-word args passed to
:func:`google.auth.transport.grpc.secure_authorized_channel`.

Returns:
grpc.Channel: The created channel.
"""
if credentials is None:
credentials, _ = google.auth.default(scopes=scopes)

This comment was marked as spam.

This comment was marked as spam.


request = google.auth.transport.requests.Request()

return google.auth.transport.grpc.secure_authorized_channel(
credentials, request, target, **kwargs)
41 changes: 41 additions & 0 deletions core/tests/unit/api_core/helpers/test_grpc_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,44 @@ def test_wrap_errors_streaming(wrap_stream_errors):

assert result == wrap_stream_errors.return_value
wrap_stream_errors.assert_called_once_with(callable_)


@mock.patch(
'google.auth.default',
return_value=(mock.sentinel.credentials, mock.sentinel.projet))
@mock.patch('google.auth.transport.grpc.secure_authorized_channel')
def test_create_channel_implicit(secure_authorized_channel, default):
target = 'example.com:443'

channel = grpc_helpers.create_channel(target)

assert channel is secure_authorized_channel.return_value
default.assert_called_once_with(scopes=None)
secure_authorized_channel.assert_called_once_with(
mock.sentinel.credentials, mock.ANY, target)


@mock.patch(
'google.auth.default',
return_value=(mock.sentinel.credentials, mock.sentinel.projet))
@mock.patch('google.auth.transport.grpc.secure_authorized_channel')
def test_create_channel_implicit_with_scopes(
secure_authorized_channel, default):
target = 'example.com:443'

channel = grpc_helpers.create_channel(target, scopes=['one', 'two'])

assert channel is secure_authorized_channel.return_value
default.assert_called_once_with(scopes=['one', 'two'])


@mock.patch('google.auth.transport.grpc.secure_authorized_channel')
def test_create_channel_explicit(secure_authorized_channel):
target = 'example.com:443'

channel = grpc_helpers.create_channel(
target, credentials=mock.sentinel.credentials)

assert channel is secure_authorized_channel.return_value
secure_authorized_channel.assert_called_once_with(
mock.sentinel.credentials, mock.ANY, target)