Repository navigation
Attempt at implementing clients for Pub/Sub #910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4bf5c05
Adding basic client class in pubsub.
dhermes 12460a9
Adding factory constructors to pubsub client.
dhermes f058b3e
Implementing Client.list_topics in pubsub.
dhermes 47cc25a
Implementing Client.list_subscriptions in pubsub.
dhermes be42839
Allowing client to be used in pubsub.Topic.
dhermes ce34ef3
Removing gcloud.pubsub.api.
dhermes e187345
Making Topic.from_api_repr accept a client.
dhermes d9e3dc3
Adding client to Subscription.from_api_repr.
dhermes 8360e69
Making client mandatory in most of pubsub.
dhermes a1a19a3
Using client on all Topic methods.
dhermes d8626ef
Using client on all Subscription methods.
dhermes ff39a92
Removing unused implict code in pubsub.
dhermes 805225b
Updating pubsub regression test for new code.
dhermes 29f35d0
Updating Pub/Sub docs based on client changes.
dhermes c206638
Addressing review comments in PR #910.
dhermes File filter
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
commit 4bf5c05890344e2f19d2937e916a9c582c0fcb84
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| 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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.