Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Adding basic client class in pubsub.
Allows implicit determination of project and credentials
if no values are passed.
  • Loading branch information
dhermes committed Jun 5, 2015
commit 4bf5c05890344e2f19d2937e916a9c582c0fcb84
54 changes: 54 additions & 0 deletions gcloud/pubsub/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""gcloud pubsub client for interacting with API."""


from gcloud._helpers import _get_production_project
from gcloud.credentials import get_credentials
from gcloud.pubsub.connection import Connection


class Client(object):
"""Client to bundle configuration needed for API requests.

:type project: string
:param project: the project which the client acts on behalf of. Will be
passed when creating a topic. If not passed,
falls back to the default inferred from the environment.

:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
:class:`NoneType`
:param credentials: The OAuth2 Credentials to use for the connection
owned by this client. If not passed (and if no ``http``
object is passed), falls back to the default inferred
from the environment.

:type http: :class:`httplib2.Http` or class that defines ``request()``.
:param http: An optional HTTP object to make requests.

:raises: :class:`ValueError` if the project is neither passed in nor
set in the environment.
"""
def __init__(self, project=None, credentials=None, http=None):
if project is None:
project = _get_production_project()

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

if project is None:
raise ValueError('Project was not passed and could not be '
'determined from the environment.')
self.project = project

if credentials is None and http is None:
credentials = get_credentials()
self.connection = Connection(credentials=credentials, http=http)
96 changes: 96 additions & 0 deletions gcloud/pubsub/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest2


class TestClient(unittest2.TestCase):

def _getTargetClass(self):
from gcloud.pubsub.client import Client
return Client

def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)

def test_ctor_defaults(self):
from gcloud._testing import _Monkey
from gcloud.pubsub import SCOPE
from gcloud.pubsub import client
from gcloud.pubsub.connection import Connection

PROJECT = 'PROJECT'
CREDS = _Credentials()
FUNC_CALLS = []

def mock_get_proj():
FUNC_CALLS.append('_get_production_project')
return PROJECT

def mock_get_credentials():
FUNC_CALLS.append('get_credentials')
return CREDS

with _Monkey(client, get_credentials=mock_get_credentials,
_get_production_project=mock_get_proj):
client_obj = self._makeOne()

self.assertEqual(client_obj.project, PROJECT)
self.assertTrue(isinstance(client_obj.connection, Connection))
self.assertTrue(client_obj.connection._credentials is CREDS)
self.assertEqual(client_obj.connection._credentials._scopes, SCOPE)
self.assertEqual(FUNC_CALLS,
['_get_production_project', 'get_credentials'])

def test_ctor_missing_project(self):
from gcloud._testing import _Monkey
from gcloud.pubsub import client

FUNC_CALLS = []

def mock_get_proj():
FUNC_CALLS.append('_get_production_project')
return None

with _Monkey(client, _get_production_project=mock_get_proj):
self.assertRaises(ValueError, self._makeOne)

self.assertEqual(FUNC_CALLS, ['_get_production_project'])

def test_ctor_explicit(self):
from gcloud.pubsub import SCOPE
from gcloud.pubsub.connection import Connection

PROJECT = 'PROJECT'
CREDS = _Credentials()

client_obj = self._makeOne(project=PROJECT, credentials=CREDS)

self.assertEqual(client_obj.project, PROJECT)
self.assertTrue(isinstance(client_obj.connection, Connection))
self.assertTrue(client_obj.connection._credentials is CREDS)
self.assertEqual(CREDS._scopes, SCOPE)


class _Credentials(object):

_scopes = None

@staticmethod
def create_scoped_required():
return True

def create_scoped(self, scope):
self._scopes = scope
return self