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

Pubsub: override client classmethod factories inherited from GAPIC. #6453

Merged
merged 1 commit into from
Nov 8, 2018
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
32 changes: 31 additions & 1 deletion pubsub/google/cloud/pubsub_v1/publisher/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import six

from google.api_core import grpc_helpers
from google.oauth2 import service_account

from google.cloud.pubsub_v1 import _gapic
from google.cloud.pubsub_v1 import types
Expand All @@ -31,8 +32,15 @@

__version__ = pkg_resources.get_distribution('google-cloud-pubsub').version

_BLACKLISTED_METHODS = (
'publish',
'from_service_account_file',
'from_service_account_json',
)

@_gapic.add_methods(publisher_client.PublisherClient, blacklist=('publish',))

@_gapic.add_methods(
publisher_client.PublisherClient, blacklist=_BLACKLISTED_METHODS)
class Client(object):
"""A publisher client for Google Cloud Pub/Sub.

Expand Down Expand Up @@ -86,6 +94,28 @@ def __init__(self, batch_settings=(), **kwargs):
self._batch_lock = self._batch_class.make_lock()
self._batches = {}

@classmethod
def from_service_account_file(cls, filename, batch_settings=(), **kwargs):
"""Creates an instance of this client using the provided credentials
file.

Args:
filename (str): The path to the service account private key json
file.
batch_settings (~google.cloud.pubsub_v1.types.BatchSettings): The
settings for batch publishing.
kwargs: Additional arguments to pass to the constructor.

Returns:
PublisherClient: The constructed client.
"""
credentials = service_account.Credentials.from_service_account_file(
filename)
kwargs['credentials'] = credentials
return cls(batch_settings, **kwargs)

from_service_account_json = from_service_account_file

@property
def target(self):
"""Return the target (where the API is).
Expand Down
31 changes: 29 additions & 2 deletions pubsub/google/cloud/pubsub_v1/subscriber/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import grpc

from google.api_core import grpc_helpers
from google.oauth2 import service_account

from google.cloud.pubsub_v1 import _gapic
from google.cloud.pubsub_v1 import types
Expand All @@ -30,9 +31,15 @@

__version__ = pkg_resources.get_distribution('google-cloud-pubsub').version

_BLACKLISTED_METHODS = (
'publish',
'from_service_account_file',
'from_service_account_json',
)

@_gapic.add_methods(subscriber_client.SubscriberClient,
blacklist=('streaming_pull',))

@_gapic.add_methods(
subscriber_client.SubscriberClient, blacklist=_BLACKLISTED_METHODS)
class Client(object):
"""A subscriber client for Google Cloud Pub/Sub.

Expand Down Expand Up @@ -75,6 +82,26 @@ def __init__(self, **kwargs):
# client.
self._api = subscriber_client.SubscriberClient(**kwargs)

@classmethod
def from_service_account_file(cls, filename, **kwargs):
"""Creates an instance of this client using the provided credentials
file.

Args:
filename (str): The path to the service account private key json
file.
kwargs: Additional arguments to pass to the constructor.

Returns:
PublisherClient: The constructed client.
"""
credentials = service_account.Credentials.from_service_account_file(
filename)
kwargs['credentials'] = credentials
return cls(**kwargs)

from_service_account_json = from_service_account_file

@property
def target(self):
"""Return the target (where the API is).
Expand Down
10 changes: 10 additions & 0 deletions pubsub/tests/unit/pubsub_v1/publisher/test_publisher_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ def test_gapic_class_method_on_class():
assert answer == 'projects/foo/topics/bar'


def test_class_method_factory():
patch = mock.patch(
'google.oauth2.service_account.Credentials.from_service_account_file')

with patch:
client = publisher.Client.from_service_account_file('filename.json')

assert isinstance(client, publisher.Client)


def test_gapic_class_method_on_instance():
creds = mock.Mock(spec=credentials.Credentials)
client = publisher.Client(credentials=creds)
Expand Down
10 changes: 10 additions & 0 deletions pubsub/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ def test_init_emulator(monkeypatch):
assert channel.target().decode('utf8') == '/baz/bacon/'


def test_class_method_factory():
patch = mock.patch(
'google.oauth2.service_account.Credentials.from_service_account_file')

with patch:
client = subscriber.Client.from_service_account_file('filename.json')

assert isinstance(client, subscriber.Client)


@mock.patch(
'google.cloud.pubsub_v1.subscriber._protocol.streaming_pull_manager.'
'StreamingPullManager.open', autospec=True)
Expand Down