Skip to content

Commit 69d998d

Browse files
committed
Changing Pub / Sub Connection to only accept client.
1 parent ad9417f commit 69d998d

3 files changed

Lines changed: 16 additions & 21 deletions

File tree

pubsub/google/cloud/pubsub/_http.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,8 @@
3434
class Connection(_http.JSONConnection):
3535
"""A connection to Google Cloud Pub/Sub via the JSON REST API.
3636
37-
:type credentials: :class:`oauth2client.client.OAuth2Credentials`
38-
:param credentials: (Optional) The OAuth2 Credentials to use for this
39-
connection.
40-
41-
:type http: :class:`httplib2.Http` or class that defines ``request()``.
42-
:param http: (Optional) HTTP object to make requests.
37+
:type client: :class:`~google.cloud.pubsub.client.Client`
38+
:param client: The client that owns the current connection.
4339
"""
4440

4541
API_BASE_URL = 'https://' + PUBSUB_API_HOST
@@ -51,12 +47,8 @@ class Connection(_http.JSONConnection):
5147
API_URL_TEMPLATE = '{api_base_url}/{api_version}{path}'
5248
"""A template for the URL of a particular API call."""
5349

54-
SCOPE = ('https://www.googleapis.com/auth/pubsub',
55-
'https://www.googleapis.com/auth/cloud-platform')
56-
"""The scopes required for authenticating as a Cloud Pub/Sub consumer."""
57-
58-
def __init__(self, credentials=None, http=None):
59-
super(Connection, self).__init__(credentials=credentials, http=http)
50+
def __init__(self, client):
51+
super(Connection, self).__init__(client)
6052
emulator_host = os.getenv(PUBSUB_EMULATOR)
6153
if emulator_host is None:
6254
self.host = self.__class__.API_BASE_URL

pubsub/google/cloud/pubsub/client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,15 @@ class Client(ClientWithProject):
7575
_subscriber_api = None
7676
_iam_policy_api = None
7777

78+
SCOPE = ('https://www.googleapis.com/auth/pubsub',
79+
'https://www.googleapis.com/auth/cloud-platform')
80+
"""The scopes required for authenticating as a Cloud Pub/Sub consumer."""
81+
7882
def __init__(self, project=None, credentials=None,
7983
http=None, use_gax=None):
8084
super(Client, self).__init__(
8185
project=project, credentials=credentials, http=http)
82-
self._connection = Connection(
83-
credentials=self._credentials, http=self._http)
86+
self._connection = Connection(self)
8487
if use_gax is None:
8588
self._use_gax = _USE_GAX
8689
else:
@@ -96,7 +99,7 @@ def publisher_api(self):
9699
host=self._connection.host)
97100
else:
98101
generated = make_gax_publisher_api(
99-
credentials=self._connection._credentials)
102+
credentials=self._credentials)
100103
self._publisher_api = GAXPublisherAPI(generated, self)
101104
else:
102105
self._publisher_api = JSONPublisherAPI(self)
@@ -112,7 +115,7 @@ def subscriber_api(self):
112115
host=self._connection.host)
113116
else:
114117
generated = make_gax_subscriber_api(
115-
credentials=self._connection._credentials)
118+
credentials=self._credentials)
116119
self._subscriber_api = GAXSubscriberAPI(generated, self)
117120
else:
118121
self._subscriber_api = JSONSubscriberAPI(self)

pubsub/unit_tests/test__http.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def _get_target_class():
4646
return Connection
4747

4848
def test_default_url(self):
49-
conn = self._make_one()
49+
conn = self._make_one(object())
5050
klass = self._get_target_class()
5151
self.assertEqual(conn.api_base_url, klass.API_BASE_URL)
5252

@@ -57,14 +57,14 @@ def test_custom_url_from_env(self):
5757
fake_environ = {PUBSUB_EMULATOR: HOST}
5858

5959
with mock.patch('os.environ', new=fake_environ):
60-
conn = self._make_one()
60+
conn = self._make_one(object())
6161

6262
klass = self._get_target_class()
6363
self.assertNotEqual(conn.api_base_url, klass.API_BASE_URL)
6464
self.assertEqual(conn.api_base_url, 'http://' + HOST)
6565

6666
def test_build_api_url_no_extra_query_params(self):
67-
conn = self._make_one()
67+
conn = self._make_one(object())
6868
URI = '/'.join([
6969
conn.API_BASE_URL,
7070
conn.API_VERSION,
@@ -76,7 +76,7 @@ def test_build_api_url_w_extra_query_params(self):
7676
from six.moves.urllib.parse import parse_qsl
7777
from six.moves.urllib.parse import urlsplit
7878

79-
conn = self._make_one()
79+
conn = self._make_one(object())
8080
uri = conn.build_api_url('/foo', {'bar': 'baz'})
8181
scheme, netloc, path, qs, _ = urlsplit(uri)
8282
self.assertEqual('%s://%s' % (scheme, netloc), conn.API_BASE_URL)
@@ -88,7 +88,7 @@ def test_build_api_url_w_extra_query_params(self):
8888
def test_build_api_url_w_base_url_override(self):
8989
base_url1 = 'api-base-url1'
9090
base_url2 = 'api-base-url2'
91-
conn = self._make_one()
91+
conn = self._make_one(object())
9292
conn.api_base_url = base_url1
9393
URI = '/'.join([
9494
base_url2,

0 commit comments

Comments
 (0)