Skip to content
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

Adding base client class in core. #953

Merged
merged 3 commits into from
Jul 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
152 changes: 152 additions & 0 deletions gcloud/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# 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._helpers import _get_production_project
from gcloud.connection import Connection
from gcloud.credentials import get_credentials
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.

Assumes that the associated ``_connection_class`` only accepts
``http`` and ``credentials`` in its constructor.

: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.
"""

_connection_class = Connection

def __init__(self, credentials=None, http=None):
if credentials is None and http is None:
credentials = get_credentials()
self.connection = self._connection_class(
credentials=credentials, 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)


class JSONClient(Client):
"""Client to for Google JSON-based API.

Assumes such APIs use the `project` and the client needs to store this
value.

:type project: string
:param project: the project which the client acts on behalf of. 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. If not passed, an
``http`` object is created that is bound to the
``credentials`` for the current object.

: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

super(JSONClient, self).__init__(credentials=credentials, http=http)
20 changes: 16 additions & 4 deletions gcloud/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,12 @@ def from_service_account_json(cls, json_credentials_path, *args, **kwargs):

:rtype: :class:`gcloud.connection.Connection`
:returns: The connection created with the retrieved JSON credentials.
:raises: class:`TypeError` if there is a conflict with the kwargs
and the credentials created by the factory.
"""
credentials = get_for_service_account_json(json_credentials_path)
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)

Expand Down Expand Up @@ -167,25 +169,35 @@ def from_service_account_p12(cls, client_email, private_key_path,

:rtype: :class:`gcloud.connection.Connection`
:returns: The connection created with the retrieved P12 credentials.
:raises: class:`TypeError` if there is a conflict with the kwargs
and the credentials created by the factory.
"""
credentials = get_for_service_account_p12(client_email,
private_key_path)
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)

@classmethod
def from_environment(cls, *args, **kwargs):
"""Factory to retrieve implicit credentials while creating connection.

: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.connection.Connection`
:returns: The connection created with the retrieved implicit
credentials.
:raises: class:`TypeError` if there is a conflict with the kwargs
and the credentials created by the factory.
"""
credentials = get_credentials()
if 'credentials' in kwargs:
raise TypeError('credentials must not be in keyword arguments')
credentials = get_credentials()
kwargs['credentials'] = credentials
return cls(*args, **kwargs)

Expand Down
73 changes: 3 additions & 70 deletions gcloud/pubsub/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@
"""gcloud pubsub client for interacting with API."""


from gcloud._helpers import _get_production_project
from gcloud.credentials import get_credentials
from gcloud.credentials import get_for_service_account_json
from gcloud.credentials import get_for_service_account_p12
from gcloud.client import JSONClient
from gcloud.pubsub.connection import Connection
from gcloud.pubsub.subscription import Subscription
from gcloud.pubsub.topic import Topic


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

:type project: string
Expand All @@ -43,73 +40,9 @@ class Client(object):
: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.

: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)

@classmethod
def from_service_account_json(cls, json_credentials_path, project=None):
"""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 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.

:rtype: :class:`gcloud.pubsub.client.Client`
:returns: The client created with the retrieved JSON credentials.
"""
credentials = get_for_service_account_json(json_credentials_path)
return cls(project=project, credentials=credentials)

@classmethod
def from_service_account_p12(cls, client_email, private_key_path,
project=None):
"""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 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.

:rtype: :class:`gcloud.pubsub.client.Client`
:returns: The client created with the retrieved P12 credentials.
"""
credentials = get_for_service_account_p12(client_email,
private_key_path)
return cls(project=project, credentials=credentials)
_connection_class = Connection

def list_topics(self, page_size=None, page_token=None):
"""List topics for the project associated with this client.
Expand Down
Loading