forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is pre-work in googleapis#952, but came up in googleapis#933 as well.
- Loading branch information
Showing
5 changed files
with
205 additions
and
108 deletions.
There are no files selected for viewing
This file contains 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,104 @@ | ||
# 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 client base class for interacting with API.""" | ||
|
||
|
||
from gcloud.credentials import get_for_service_account_json | ||
from gcloud.credentials import get_for_service_account_p12 | ||
|
||
|
||
class Client(object): | ||
"""Client to bundle configuration needed for API requests. | ||
: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. If not passed, an | ||
``http`` object is created that is bound to the | ||
``credentials`` for the current object. | ||
""" | ||
|
||
def __init__(self, credentials=None, http=None): | ||
self.credentials = credentials | ||
self.http = http | ||
|
||
@classmethod | ||
def from_service_account_json(cls, json_credentials_path, *args, **kwargs): | ||
"""Factory to retrieve JSON credentials while creating client. | ||
:type json_credentials_path: string | ||
:param json_credentials_path: The path to a private key file (this file | ||
was given to you when you created the | ||
service account). This file must contain | ||
a JSON object with a private key and | ||
other credentials information (downloaded | ||
from the Google APIs console). | ||
:type args: tuple | ||
:param args: Remaining positional arguments to pass to constructor. | ||
:type kwargs: dictionary | ||
:param kwargs: Remaining keyword arguments to pass to constructor. | ||
:rtype: :class:`gcloud.pubsub.client.Client` | ||
:returns: The client created with the retrieved JSON credentials. | ||
:raises: class:`TypeError` if there is a conflict with the kwargs | ||
and the credentials created by the factory. | ||
""" | ||
if 'credentials' in kwargs: | ||
raise TypeError('credentials must not be in keyword arguments') | ||
credentials = get_for_service_account_json(json_credentials_path) | ||
kwargs['credentials'] = credentials | ||
return cls(*args, **kwargs) | ||
|
||
@classmethod | ||
def from_service_account_p12(cls, client_email, private_key_path, | ||
*args, **kwargs): | ||
"""Factory to retrieve P12 credentials while creating client. | ||
.. note:: | ||
Unless you have an explicit reason to use a PKCS12 key for your | ||
service account, we recommend using a JSON key. | ||
:type client_email: string | ||
:param client_email: The e-mail attached to the service account. | ||
:type private_key_path: string | ||
:param private_key_path: The path to a private key file (this file was | ||
given to you when you created the service | ||
account). This file must be in P12 format. | ||
:type args: tuple | ||
:param args: Remaining positional arguments to pass to constructor. | ||
:type kwargs: dictionary | ||
:param kwargs: Remaining keyword arguments to pass to constructor. | ||
:rtype: :class:`gcloud.client.Client` | ||
:returns: The client created with the retrieved P12 credentials. | ||
:raises: class:`TypeError` if there is a conflict with the kwargs | ||
and the credentials created by the factory. | ||
""" | ||
if 'credentials' in kwargs: | ||
raise TypeError('credentials must not be in keyword arguments') | ||
credentials = get_for_service_account_p12(client_email, | ||
private_key_path) | ||
kwargs['credentials'] = credentials | ||
return cls(*args, **kwargs) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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,83 @@ | ||
# 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.client import Client | ||
return Client | ||
|
||
def _makeOne(self, *args, **kw): | ||
return self._getTargetClass()(*args, **kw) | ||
|
||
def test_ctor_explict(self): | ||
CREDENTIALS = object() | ||
HTTP = object() | ||
CLIENT_OBJ = self._makeOne(credentials=CREDENTIALS, http=HTTP) | ||
self.assertTrue(CLIENT_OBJ.credentials is CREDENTIALS) | ||
self.assertTrue(CLIENT_OBJ.http is HTTP) | ||
|
||
def test_from_service_account_json(self): | ||
from gcloud._testing import _Monkey | ||
from gcloud import client | ||
|
||
KLASS = self._getTargetClass() | ||
CREDENTIALS = object() | ||
_CALLED = [] | ||
|
||
def mock_creds(arg1): | ||
_CALLED.append((arg1,)) | ||
return CREDENTIALS | ||
|
||
BOGUS_ARG = object() | ||
with _Monkey(client, get_for_service_account_json=mock_creds): | ||
CLIENT_OBJ = KLASS.from_service_account_json(BOGUS_ARG) | ||
|
||
self.assertTrue(CLIENT_OBJ.credentials is CREDENTIALS) | ||
self.assertEqual(_CALLED, [(BOGUS_ARG,)]) | ||
|
||
def test_from_service_account_json_fail(self): | ||
KLASS = self._getTargetClass() | ||
CREDENTIALS = object() | ||
self.assertRaises(TypeError, KLASS.from_service_account_json, None, | ||
credentials=CREDENTIALS) | ||
|
||
def test_from_service_account_p12(self): | ||
from gcloud._testing import _Monkey | ||
from gcloud import client | ||
|
||
KLASS = self._getTargetClass() | ||
CREDENTIALS = object() | ||
_CALLED = [] | ||
|
||
def mock_creds(arg1, arg2): | ||
_CALLED.append((arg1, arg2)) | ||
return CREDENTIALS | ||
|
||
BOGUS_ARG1 = object() | ||
BOGUS_ARG2 = object() | ||
with _Monkey(client, get_for_service_account_p12=mock_creds): | ||
CLIENT_OBJ = KLASS.from_service_account_p12(BOGUS_ARG1, BOGUS_ARG2) | ||
|
||
self.assertTrue(CLIENT_OBJ.credentials is CREDENTIALS) | ||
self.assertEqual(_CALLED, [(BOGUS_ARG1, BOGUS_ARG2)]) | ||
|
||
def test_from_service_account_p12_fail(self): | ||
KLASS = self._getTargetClass() | ||
CREDENTIALS = object() | ||
self.assertRaises(TypeError, KLASS.from_service_account_p12, None, | ||
None, credentials=CREDENTIALS) |