Skip to content

Commit 1b5ca83

Browse files
committed
Merge pull request #1344 from dhermes/pubsub-emulator-env-var
Support Pub/Sub emulator environment var.
2 parents f284923 + e6df6fb commit 1b5ca83

6 files changed

Lines changed: 108 additions & 5 deletions

File tree

gcloud/connection.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,6 @@ def build_api_url(cls, path, query_params=None,
171171
:rtype: string
172172
:returns: The URL assembled from the pieces provided.
173173
"""
174-
api_base_url = api_base_url or cls.API_BASE_URL
175-
176174
url = cls.API_URL_TEMPLATE.format(
177175
api_base_url=(api_base_url or cls.API_BASE_URL),
178176
api_version=(api_version or cls.API_VERSION),

gcloud/datastore/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Connection(connection.Connection):
3737
3838
:type api_base_url: string
3939
:param api_base_url: The base of the API call URL. Defaults to the value
40-
from :mod:`gcloud.connection`.
40+
:attr:`Connection.API_BASE_URL`.
4141
"""
4242

4343
API_BASE_URL = 'https://www.googleapis.com'

gcloud/datastore/test_connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_custom_url_from_env(self):
5656
import os
5757
from gcloud._testing import _Monkey
5858
from gcloud.connection import API_BASE_URL
59-
from gcloud.datastore.connection import GCD_HOST
59+
from gcloud.environment_vars import GCD_HOST
6060

6161
HOST = object()
6262
fake_environ = {GCD_HOST: HOST}
@@ -79,7 +79,7 @@ def test_custom_url_constructor_and_env(self):
7979
import os
8080
from gcloud._testing import _Monkey
8181
from gcloud.connection import API_BASE_URL
82-
from gcloud.datastore.connection import GCD_HOST
82+
from gcloud.environment_vars import GCD_HOST
8383

8484
HOST1 = object()
8585
HOST2 = object()

gcloud/environment_vars.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
GCD_HOST = 'DATASTORE_HOST'
3434
"""Environment variable defining host for GCD dataset server."""
3535

36+
PUBSUB_EMULATOR = 'PUBSUB_HOST'
37+
"""Environment variable defining host for Pub/Sub emulator."""
38+
3639
TESTS_DATASET = 'GCLOUD_TESTS_DATASET_ID'
3740
"""Environment variable defining dataset ID for tests."""
3841

gcloud/pubsub/connection.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515
"""Create / interact with gcloud pubsub connections."""
1616

17+
import os
18+
1719
from gcloud import connection as base_connection
20+
from gcloud.environment_vars import PUBSUB_EMULATOR
1821

1922

2023
class Connection(base_connection.JSONConnection):
@@ -26,6 +29,10 @@ class Connection(base_connection.JSONConnection):
2629
2730
:type http: :class:`httplib2.Http` or class that defines ``request()``.
2831
:param http: (Optional) HTTP object to make requests.
32+
33+
:type api_base_url: string
34+
:param api_base_url: The base of the API call URL. Defaults to the value
35+
:attr:`Connection.API_BASE_URL`.
2936
"""
3037

3138
API_BASE_URL = 'https://pubsub.googleapis.com'
@@ -40,3 +47,41 @@ class Connection(base_connection.JSONConnection):
4047
SCOPE = ('https://www.googleapis.com/auth/pubsub',
4148
'https://www.googleapis.com/auth/cloud-platform')
4249
"""The scopes required for authenticating as a Cloud Pub/Sub consumer."""
50+
51+
def __init__(self, credentials=None, http=None, api_base_url=None):
52+
super(Connection, self).__init__(credentials=credentials, http=http)
53+
if api_base_url is None:
54+
api_base_url = os.getenv(PUBSUB_EMULATOR,
55+
self.__class__.API_BASE_URL)
56+
self.api_base_url = api_base_url
57+
58+
def build_api_url(self, path, query_params=None,
59+
api_base_url=None, api_version=None):
60+
"""Construct an API url given a few components, some optional.
61+
62+
Typically, you shouldn't need to use this method.
63+
64+
:type path: string
65+
:param path: The path to the resource.
66+
67+
:type query_params: dict
68+
:param query_params: A dictionary of keys and values to insert into
69+
the query string of the URL.
70+
71+
:type api_base_url: string
72+
:param api_base_url: The base URL for the API endpoint.
73+
Typically you won't have to provide this.
74+
75+
:type api_version: string
76+
:param api_version: The version of the API to call.
77+
Typically you shouldn't provide this and instead
78+
use the default for the library.
79+
80+
:rtype: string
81+
:returns: The URL assembled from the pieces provided.
82+
"""
83+
if api_base_url is None:
84+
api_base_url = self.api_base_url
85+
return super(Connection, self.__class__).build_api_url(
86+
path, query_params=query_params,
87+
api_base_url=api_base_url, api_version=api_version)

gcloud/pubsub/test_connection.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,51 @@ def _getTargetClass(self):
2424
def _makeOne(self, *args, **kw):
2525
return self._getTargetClass()(*args, **kw)
2626

27+
def test_default_url(self):
28+
conn = self._makeOne()
29+
klass = self._getTargetClass()
30+
self.assertEqual(conn.api_base_url, klass.API_BASE_URL)
31+
32+
def test_custom_url_from_env(self):
33+
import os
34+
from gcloud._testing import _Monkey
35+
from gcloud.environment_vars import PUBSUB_EMULATOR
36+
37+
HOST = object()
38+
fake_environ = {PUBSUB_EMULATOR: HOST}
39+
40+
with _Monkey(os, getenv=fake_environ.get):
41+
conn = self._makeOne()
42+
43+
klass = self._getTargetClass()
44+
self.assertNotEqual(conn.api_base_url, klass.API_BASE_URL)
45+
self.assertEqual(conn.api_base_url, HOST)
46+
47+
def test_custom_url_from_constructor(self):
48+
HOST = object()
49+
conn = self._makeOne(api_base_url=HOST)
50+
51+
klass = self._getTargetClass()
52+
self.assertNotEqual(conn.api_base_url, klass.API_BASE_URL)
53+
self.assertEqual(conn.api_base_url, HOST)
54+
55+
def test_custom_url_constructor_and_env(self):
56+
import os
57+
from gcloud._testing import _Monkey
58+
from gcloud.environment_vars import PUBSUB_EMULATOR
59+
60+
HOST1 = object()
61+
HOST2 = object()
62+
fake_environ = {PUBSUB_EMULATOR: HOST1}
63+
64+
with _Monkey(os, getenv=fake_environ.get):
65+
conn = self._makeOne(api_base_url=HOST2)
66+
67+
klass = self._getTargetClass()
68+
self.assertNotEqual(conn.api_base_url, klass.API_BASE_URL)
69+
self.assertNotEqual(conn.api_base_url, HOST1)
70+
self.assertEqual(conn.api_base_url, HOST2)
71+
2772
def test_build_api_url_no_extra_query_params(self):
2873
conn = self._makeOne()
2974
URI = '/'.join([
@@ -44,3 +89,15 @@ def test_build_api_url_w_extra_query_params(self):
4489
'/'.join(['', conn.API_VERSION, 'foo']))
4590
parms = dict(parse_qsl(qs))
4691
self.assertEqual(parms['bar'], 'baz')
92+
93+
def test_build_api_url_w_base_url_override(self):
94+
base_url1 = 'api-base-url1'
95+
base_url2 = 'api-base-url2'
96+
conn = self._makeOne(api_base_url=base_url1)
97+
URI = '/'.join([
98+
base_url2,
99+
conn.API_VERSION,
100+
'foo',
101+
])
102+
self.assertEqual(
103+
conn.build_api_url('/foo', api_base_url=base_url2), URI)

0 commit comments

Comments
 (0)