Skip to content

Commit

Permalink
Merge pull request #1344 from dhermes/pubsub-emulator-env-var
Browse files Browse the repository at this point in the history
Support Pub/Sub emulator environment var.
  • Loading branch information
dhermes committed Jan 7, 2016
2 parents f284923 + e6df6fb commit 1b5ca83
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 5 deletions.
2 changes: 0 additions & 2 deletions gcloud/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,6 @@ def build_api_url(cls, path, query_params=None,
:rtype: string
:returns: The URL assembled from the pieces provided.
"""
api_base_url = api_base_url or cls.API_BASE_URL

url = cls.API_URL_TEMPLATE.format(
api_base_url=(api_base_url or cls.API_BASE_URL),
api_version=(api_version or cls.API_VERSION),
Expand Down
2 changes: 1 addition & 1 deletion gcloud/datastore/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Connection(connection.Connection):
:type api_base_url: string
:param api_base_url: The base of the API call URL. Defaults to the value
from :mod:`gcloud.connection`.
:attr:`Connection.API_BASE_URL`.
"""

API_BASE_URL = 'https://www.googleapis.com'
Expand Down
4 changes: 2 additions & 2 deletions gcloud/datastore/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_custom_url_from_env(self):
import os
from gcloud._testing import _Monkey
from gcloud.connection import API_BASE_URL
from gcloud.datastore.connection import GCD_HOST
from gcloud.environment_vars import GCD_HOST

HOST = object()
fake_environ = {GCD_HOST: HOST}
Expand All @@ -79,7 +79,7 @@ def test_custom_url_constructor_and_env(self):
import os
from gcloud._testing import _Monkey
from gcloud.connection import API_BASE_URL
from gcloud.datastore.connection import GCD_HOST
from gcloud.environment_vars import GCD_HOST

HOST1 = object()
HOST2 = object()
Expand Down
3 changes: 3 additions & 0 deletions gcloud/environment_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
GCD_HOST = 'DATASTORE_HOST'
"""Environment variable defining host for GCD dataset server."""

PUBSUB_EMULATOR = 'PUBSUB_HOST'
"""Environment variable defining host for Pub/Sub emulator."""

TESTS_DATASET = 'GCLOUD_TESTS_DATASET_ID'
"""Environment variable defining dataset ID for tests."""

Expand Down
45 changes: 45 additions & 0 deletions gcloud/pubsub/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

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

import os

from gcloud import connection as base_connection
from gcloud.environment_vars import PUBSUB_EMULATOR


class Connection(base_connection.JSONConnection):
Expand All @@ -26,6 +29,10 @@ class Connection(base_connection.JSONConnection):
:type http: :class:`httplib2.Http` or class that defines ``request()``.
:param http: (Optional) HTTP object to make requests.
:type api_base_url: string
:param api_base_url: The base of the API call URL. Defaults to the value
:attr:`Connection.API_BASE_URL`.
"""

API_BASE_URL = 'https://pubsub.googleapis.com'
Expand All @@ -40,3 +47,41 @@ class Connection(base_connection.JSONConnection):
SCOPE = ('https://www.googleapis.com/auth/pubsub',
'https://www.googleapis.com/auth/cloud-platform')
"""The scopes required for authenticating as a Cloud Pub/Sub consumer."""

def __init__(self, credentials=None, http=None, api_base_url=None):
super(Connection, self).__init__(credentials=credentials, http=http)
if api_base_url is None:
api_base_url = os.getenv(PUBSUB_EMULATOR,
self.__class__.API_BASE_URL)
self.api_base_url = api_base_url

def build_api_url(self, path, query_params=None,
api_base_url=None, api_version=None):
"""Construct an API url given a few components, some optional.
Typically, you shouldn't need to use this method.
:type path: string
:param path: The path to the resource.
:type query_params: dict
:param query_params: A dictionary of keys and values to insert into
the query string of the URL.
:type api_base_url: string
:param api_base_url: The base URL for the API endpoint.
Typically you won't have to provide this.
:type api_version: string
:param api_version: The version of the API to call.
Typically you shouldn't provide this and instead
use the default for the library.
:rtype: string
:returns: The URL assembled from the pieces provided.
"""
if api_base_url is None:
api_base_url = self.api_base_url
return super(Connection, self.__class__).build_api_url(
path, query_params=query_params,
api_base_url=api_base_url, api_version=api_version)
57 changes: 57 additions & 0 deletions gcloud/pubsub/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,51 @@ def _getTargetClass(self):
def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)

def test_default_url(self):
conn = self._makeOne()
klass = self._getTargetClass()
self.assertEqual(conn.api_base_url, klass.API_BASE_URL)

def test_custom_url_from_env(self):
import os
from gcloud._testing import _Monkey
from gcloud.environment_vars import PUBSUB_EMULATOR

HOST = object()
fake_environ = {PUBSUB_EMULATOR: HOST}

with _Monkey(os, getenv=fake_environ.get):
conn = self._makeOne()

klass = self._getTargetClass()
self.assertNotEqual(conn.api_base_url, klass.API_BASE_URL)
self.assertEqual(conn.api_base_url, HOST)

def test_custom_url_from_constructor(self):
HOST = object()
conn = self._makeOne(api_base_url=HOST)

klass = self._getTargetClass()
self.assertNotEqual(conn.api_base_url, klass.API_BASE_URL)
self.assertEqual(conn.api_base_url, HOST)

def test_custom_url_constructor_and_env(self):
import os
from gcloud._testing import _Monkey
from gcloud.environment_vars import PUBSUB_EMULATOR

HOST1 = object()
HOST2 = object()
fake_environ = {PUBSUB_EMULATOR: HOST1}

with _Monkey(os, getenv=fake_environ.get):
conn = self._makeOne(api_base_url=HOST2)

klass = self._getTargetClass()
self.assertNotEqual(conn.api_base_url, klass.API_BASE_URL)
self.assertNotEqual(conn.api_base_url, HOST1)
self.assertEqual(conn.api_base_url, HOST2)

def test_build_api_url_no_extra_query_params(self):
conn = self._makeOne()
URI = '/'.join([
Expand All @@ -44,3 +89,15 @@ def test_build_api_url_w_extra_query_params(self):
'/'.join(['', conn.API_VERSION, 'foo']))
parms = dict(parse_qsl(qs))
self.assertEqual(parms['bar'], 'baz')

def test_build_api_url_w_base_url_override(self):
base_url1 = 'api-base-url1'
base_url2 = 'api-base-url2'
conn = self._makeOne(api_base_url=base_url1)
URI = '/'.join([
base_url2,
conn.API_VERSION,
'foo',
])
self.assertEqual(
conn.build_api_url('/foo', api_base_url=base_url2), URI)

0 comments on commit 1b5ca83

Please sign in to comment.