Skip to content

Commit 0e337fa

Browse files
committed
Add 'Client.{publisher,subscriber,iam_policy}_api' properties.
These API objects will represent logical groupings of the JSON api endpoints, and correspond one-for-one to the stubs exposed by the generated gRPC code. Further commits will move the endpoint-specific methods from the 'Connecttion' class into the relevant API classes, and adjust the callers. See: #1700 (comment)
1 parent a8ee698 commit 0e337fa

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

gcloud/pubsub/client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
from gcloud.client import JSONClient
1919
from gcloud.pubsub.connection import Connection
20+
from gcloud.pubsub.connection import _PublisherAPI
21+
from gcloud.pubsub.connection import _SubscriberAPI
22+
from gcloud.pubsub.connection import _IAMPolicyAPI
2023
from gcloud.pubsub.subscription import Subscription
2124
from gcloud.pubsub.topic import Topic
2225

@@ -43,6 +46,28 @@ class Client(JSONClient):
4346
"""
4447

4548
_connection_class = Connection
49+
_publisher_api = _subscriber_api = _iam_policy_api = None
50+
51+
@property
52+
def publisher_api(self):
53+
"""Helper for publisher-related API calls."""
54+
if self._publisher_api is None:
55+
self._publisher_api = _PublisherAPI(self.connection)
56+
return self._publisher_api
57+
58+
@property
59+
def subscriber_api(self):
60+
"""Helper for subscriber-related API calls."""
61+
if self._subscriber_api is None:
62+
self._subscriber_api = _SubscriberAPI(self.connection)
63+
return self._subscriber_api
64+
65+
@property
66+
def iam_policy_api(self):
67+
"""Helper for IAM policy-related API calls."""
68+
if self._iam_policy_api is None:
69+
self._iam_policy_api = _IAMPolicyAPI(self.connection)
70+
return self._iam_policy_api
4671

4772
def list_topics(self, page_size=None, page_token=None):
4873
"""List topics for the project associated with this client.

gcloud/pubsub/connection.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,36 @@ def subscription_modify_ack_deadline(self, subscription_path, ack_ids,
486486
'ackDeadlineSeconds': ack_deadline,
487487
}
488488
self.api_request(method='POST', path=path, data=data)
489+
490+
491+
class _PublisherAPI(object):
492+
"""Helper mapping publisher-related APIs.
493+
494+
:type connection: :class:`Connection`
495+
:param connection: the connection used to make API requests.
496+
"""
497+
498+
def __init__(self, connection):
499+
self._connection = connection
500+
501+
502+
class _SubscriberAPI(object):
503+
"""Helper mapping subscriber-related APIs.
504+
505+
:type connection: :class:`Connection`
506+
:param connection: the connection used to make API requests.
507+
"""
508+
509+
def __init__(self, connection):
510+
self._connection = connection
511+
512+
513+
class _IAMPolicyAPI(object):
514+
"""Helper mapping IAM policy-related APIs.
515+
516+
:type connection: :class:`Connection`
517+
:param connection: the connection used to make API requests.
518+
"""
519+
520+
def __init__(self, connection):
521+
self._connection = connection

gcloud/pubsub/test_client.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,42 @@ def _getTargetClass(self):
2929
def _makeOne(self, *args, **kw):
3030
return self._getTargetClass()(*args, **kw)
3131

32+
def test_publisher_api(self):
33+
from gcloud.pubsub.connection import _PublisherAPI
34+
creds = _Credentials()
35+
client = self._makeOne(project=self.PROJECT, credentials=creds)
36+
conn = client.connection = _Connection()
37+
api = client.publisher_api
38+
self.assertTrue(isinstance(api, _PublisherAPI))
39+
self.assertTrue(api._connection is conn)
40+
# API instance is cached
41+
again = client.publisher_api
42+
self.assertTrue(again is api)
43+
44+
def test_subscriber_api(self):
45+
from gcloud.pubsub.connection import _SubscriberAPI
46+
creds = _Credentials()
47+
client = self._makeOne(project=self.PROJECT, credentials=creds)
48+
conn = client.connection = _Connection()
49+
api = client.subscriber_api
50+
self.assertTrue(isinstance(api, _SubscriberAPI))
51+
self.assertTrue(api._connection is conn)
52+
# API instance is cached
53+
again = client.subscriber_api
54+
self.assertTrue(again is api)
55+
56+
def test_iam_policy_api(self):
57+
from gcloud.pubsub.connection import _IAMPolicyAPI
58+
creds = _Credentials()
59+
client = self._makeOne(project=self.PROJECT, credentials=creds)
60+
conn = client.connection = _Connection()
61+
api = client.iam_policy_api
62+
self.assertTrue(isinstance(api, _IAMPolicyAPI))
63+
self.assertTrue(api._connection is conn)
64+
# API instance is cached
65+
again = client.iam_policy_api
66+
self.assertTrue(again is api)
67+
3268
def test_list_topics_no_paging(self):
3369
from gcloud.pubsub.topic import Topic
3470
creds = _Credentials()

gcloud/pubsub/test_connection.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,51 @@ def test_subscription_modify_ack_deadline(self):
751751
self.assertEqual(http._called_with['body'], json.dumps(BODY))
752752

753753

754+
class Test_PublisherAPI(unittest2.TestCase):
755+
756+
def _getTargetClass(self):
757+
from gcloud.pubsub.connection import _PublisherAPI
758+
return _PublisherAPI
759+
760+
def _makeOne(self, *args, **kw):
761+
return self._getTargetClass()(*args, **kw)
762+
763+
def test_ctor(self):
764+
connection = _Connection()
765+
api = self._makeOne(connection)
766+
self.assertTrue(api._connection is connection)
767+
768+
769+
class Test_SubscriberAPI(unittest2.TestCase):
770+
771+
def _getTargetClass(self):
772+
from gcloud.pubsub.connection import _SubscriberAPI
773+
return _SubscriberAPI
774+
775+
def _makeOne(self, *args, **kw):
776+
return self._getTargetClass()(*args, **kw)
777+
778+
def test_ctor(self):
779+
connection = _Connection()
780+
api = self._makeOne(connection)
781+
self.assertTrue(api._connection is connection)
782+
783+
784+
class Test_IAMPolicyAPI(unittest2.TestCase):
785+
786+
def _getTargetClass(self):
787+
from gcloud.pubsub.connection import _IAMPolicyAPI
788+
return _IAMPolicyAPI
789+
790+
def _makeOne(self, *args, **kw):
791+
return self._getTargetClass()(*args, **kw)
792+
793+
def test_ctor(self):
794+
connection = _Connection()
795+
api = self._makeOne(connection)
796+
self.assertTrue(api._connection is connection)
797+
798+
754799
class _Http(object):
755800

756801
_called_with = None
@@ -763,3 +808,11 @@ def __init__(self, headers, content):
763808
def request(self, **kw):
764809
self._called_with = kw
765810
return self._response, self._content
811+
812+
813+
class _Connection(object):
814+
815+
_called_with = None
816+
817+
def __init__(self, *responses):
818+
self._responses = responses

0 commit comments

Comments
 (0)