Skip to content

Commit 9a8ad7c

Browse files
committed
Merge pull request googleapis#953 from dhermes/client-parent-in-core
Adding base client class in core.
2 parents 68694ab + a65c4ab commit 9a8ad7c

File tree

5 files changed

+363
-181
lines changed

5 files changed

+363
-181
lines changed

gcloud/client.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""gcloud client base class for interacting with API."""
16+
17+
18+
from gcloud._helpers import _get_production_project
19+
from gcloud.connection import Connection
20+
from gcloud.credentials import get_credentials
21+
from gcloud.credentials import get_for_service_account_json
22+
from gcloud.credentials import get_for_service_account_p12
23+
24+
25+
class Client(object):
26+
"""Client to bundle configuration needed for API requests.
27+
28+
Assumes that the associated ``_connection_class`` only accepts
29+
``http`` and ``credentials`` in its constructor.
30+
31+
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
32+
:class:`NoneType`
33+
:param credentials: The OAuth2 Credentials to use for the connection
34+
owned by this client. If not passed (and if no ``http``
35+
object is passed), falls back to the default inferred
36+
from the environment.
37+
38+
:type http: :class:`httplib2.Http` or class that defines ``request()``.
39+
:param http: An optional HTTP object to make requests. If not passed, an
40+
``http`` object is created that is bound to the
41+
``credentials`` for the current object.
42+
"""
43+
44+
_connection_class = Connection
45+
46+
def __init__(self, credentials=None, http=None):
47+
if credentials is None and http is None:
48+
credentials = get_credentials()
49+
self.connection = self._connection_class(
50+
credentials=credentials, http=http)
51+
52+
@classmethod
53+
def from_service_account_json(cls, json_credentials_path, *args, **kwargs):
54+
"""Factory to retrieve JSON credentials while creating client.
55+
56+
:type json_credentials_path: string
57+
:param json_credentials_path: The path to a private key file (this file
58+
was given to you when you created the
59+
service account). This file must contain
60+
a JSON object with a private key and
61+
other credentials information (downloaded
62+
from the Google APIs console).
63+
64+
:type args: tuple
65+
:param args: Remaining positional arguments to pass to constructor.
66+
67+
:type kwargs: dictionary
68+
:param kwargs: Remaining keyword arguments to pass to constructor.
69+
70+
:rtype: :class:`gcloud.pubsub.client.Client`
71+
:returns: The client created with the retrieved JSON credentials.
72+
:raises: class:`TypeError` if there is a conflict with the kwargs
73+
and the credentials created by the factory.
74+
"""
75+
if 'credentials' in kwargs:
76+
raise TypeError('credentials must not be in keyword arguments')
77+
credentials = get_for_service_account_json(json_credentials_path)
78+
kwargs['credentials'] = credentials
79+
return cls(*args, **kwargs)
80+
81+
@classmethod
82+
def from_service_account_p12(cls, client_email, private_key_path,
83+
*args, **kwargs):
84+
"""Factory to retrieve P12 credentials while creating client.
85+
86+
.. note::
87+
Unless you have an explicit reason to use a PKCS12 key for your
88+
service account, we recommend using a JSON key.
89+
90+
:type client_email: string
91+
:param client_email: The e-mail attached to the service account.
92+
93+
:type private_key_path: string
94+
:param private_key_path: The path to a private key file (this file was
95+
given to you when you created the service
96+
account). This file must be in P12 format.
97+
98+
:type args: tuple
99+
:param args: Remaining positional arguments to pass to constructor.
100+
101+
:type kwargs: dictionary
102+
:param kwargs: Remaining keyword arguments to pass to constructor.
103+
104+
:rtype: :class:`gcloud.client.Client`
105+
:returns: The client created with the retrieved P12 credentials.
106+
:raises: class:`TypeError` if there is a conflict with the kwargs
107+
and the credentials created by the factory.
108+
"""
109+
if 'credentials' in kwargs:
110+
raise TypeError('credentials must not be in keyword arguments')
111+
credentials = get_for_service_account_p12(client_email,
112+
private_key_path)
113+
kwargs['credentials'] = credentials
114+
return cls(*args, **kwargs)
115+
116+
117+
class JSONClient(Client):
118+
"""Client to for Google JSON-based API.
119+
120+
Assumes such APIs use the `project` and the client needs to store this
121+
value.
122+
123+
:type project: string
124+
:param project: the project which the client acts on behalf of. If not
125+
passed falls back to the default inferred from the
126+
environment.
127+
128+
:type credentials: :class:`oauth2client.client.OAuth2Credentials` or
129+
:class:`NoneType`
130+
:param credentials: The OAuth2 Credentials to use for the connection
131+
owned by this client. If not passed (and if no ``http``
132+
object is passed), falls back to the default inferred
133+
from the environment.
134+
135+
:type http: :class:`httplib2.Http` or class that defines ``request()``.
136+
:param http: An optional HTTP object to make requests. If not passed, an
137+
``http`` object is created that is bound to the
138+
``credentials`` for the current object.
139+
140+
:raises: :class:`ValueError` if the project is neither passed in nor
141+
set in the environment.
142+
"""
143+
144+
def __init__(self, project=None, credentials=None, http=None):
145+
if project is None:
146+
project = _get_production_project()
147+
if project is None:
148+
raise ValueError('Project was not passed and could not be '
149+
'determined from the environment.')
150+
self.project = project
151+
152+
super(JSONClient, self).__init__(credentials=credentials, http=http)

gcloud/connection.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,12 @@ def from_service_account_json(cls, json_credentials_path, *args, **kwargs):
135135
136136
:rtype: :class:`gcloud.connection.Connection`
137137
:returns: The connection created with the retrieved JSON credentials.
138+
:raises: class:`TypeError` if there is a conflict with the kwargs
139+
and the credentials created by the factory.
138140
"""
139-
credentials = get_for_service_account_json(json_credentials_path)
140141
if 'credentials' in kwargs:
141142
raise TypeError('credentials must not be in keyword arguments')
143+
credentials = get_for_service_account_json(json_credentials_path)
142144
kwargs['credentials'] = credentials
143145
return cls(*args, **kwargs)
144146

@@ -167,25 +169,35 @@ def from_service_account_p12(cls, client_email, private_key_path,
167169
168170
:rtype: :class:`gcloud.connection.Connection`
169171
:returns: The connection created with the retrieved P12 credentials.
172+
:raises: class:`TypeError` if there is a conflict with the kwargs
173+
and the credentials created by the factory.
170174
"""
171-
credentials = get_for_service_account_p12(client_email,
172-
private_key_path)
173175
if 'credentials' in kwargs:
174176
raise TypeError('credentials must not be in keyword arguments')
177+
credentials = get_for_service_account_p12(client_email,
178+
private_key_path)
175179
kwargs['credentials'] = credentials
176180
return cls(*args, **kwargs)
177181

178182
@classmethod
179183
def from_environment(cls, *args, **kwargs):
180184
"""Factory to retrieve implicit credentials while creating connection.
181185
186+
:type args: tuple
187+
:param args: Remaining positional arguments to pass to constructor.
188+
189+
:type kwargs: dictionary
190+
:param kwargs: Remaining keyword arguments to pass to constructor.
191+
182192
:rtype: :class:`gcloud.connection.Connection`
183193
:returns: The connection created with the retrieved implicit
184194
credentials.
195+
:raises: class:`TypeError` if there is a conflict with the kwargs
196+
and the credentials created by the factory.
185197
"""
186-
credentials = get_credentials()
187198
if 'credentials' in kwargs:
188199
raise TypeError('credentials must not be in keyword arguments')
200+
credentials = get_credentials()
189201
kwargs['credentials'] = credentials
190202
return cls(*args, **kwargs)
191203

gcloud/pubsub/client.py

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@
1515
"""gcloud pubsub client for interacting with API."""
1616

1717

18-
from gcloud._helpers import _get_production_project
19-
from gcloud.credentials import get_credentials
20-
from gcloud.credentials import get_for_service_account_json
21-
from gcloud.credentials import get_for_service_account_p12
18+
from gcloud.client import JSONClient
2219
from gcloud.pubsub.connection import Connection
2320
from gcloud.pubsub.subscription import Subscription
2421
from gcloud.pubsub.topic import Topic
2522

2623

27-
class Client(object):
24+
class Client(JSONClient):
2825
"""Client to bundle configuration needed for API requests.
2926
3027
:type project: string
@@ -43,73 +40,9 @@ class Client(object):
4340
:param http: An optional HTTP object to make requests. If not passed, an
4441
``http`` object is created that is bound to the
4542
``credentials`` for the current object.
46-
47-
:raises: :class:`ValueError` if the project is neither passed in nor
48-
set in the environment.
4943
"""
50-
def __init__(self, project=None, credentials=None, http=None):
51-
if project is None:
52-
project = _get_production_project()
53-
if project is None:
54-
raise ValueError('Project was not passed and could not be '
55-
'determined from the environment.')
56-
self.project = project
57-
58-
if credentials is None and http is None:
59-
credentials = get_credentials()
60-
self.connection = Connection(credentials=credentials, http=http)
61-
62-
@classmethod
63-
def from_service_account_json(cls, json_credentials_path, project=None):
64-
"""Factory to retrieve JSON credentials while creating client.
65-
66-
:type json_credentials_path: string
67-
:param json_credentials_path: The path to a private key file (this file
68-
was given to you when you created the
69-
service account). This file must contain
70-
a JSON object with a private key and
71-
other credentials information (downloaded
72-
from the Google APIs console).
73-
74-
:type project: string
75-
:param project: the project which the client acts on behalf of. Will be
76-
passed when creating a topic. If not passed, falls
77-
back to the default inferred from the environment.
78-
79-
:rtype: :class:`gcloud.pubsub.client.Client`
80-
:returns: The client created with the retrieved JSON credentials.
81-
"""
82-
credentials = get_for_service_account_json(json_credentials_path)
83-
return cls(project=project, credentials=credentials)
84-
85-
@classmethod
86-
def from_service_account_p12(cls, client_email, private_key_path,
87-
project=None):
88-
"""Factory to retrieve P12 credentials while creating client.
89-
90-
.. note::
91-
Unless you have an explicit reason to use a PKCS12 key for your
92-
service account, we recommend using a JSON key.
9344

94-
:type client_email: string
95-
:param client_email: The e-mail attached to the service account.
96-
97-
:type private_key_path: string
98-
:param private_key_path: The path to a private key file (this file was
99-
given to you when you created the service
100-
account). This file must be in P12 format.
101-
102-
:type project: string
103-
:param project: the project which the client acts on behalf of. Will be
104-
passed when creating a topic. If not passed, falls
105-
back to the default inferred from the environment.
106-
107-
:rtype: :class:`gcloud.pubsub.client.Client`
108-
:returns: The client created with the retrieved P12 credentials.
109-
"""
110-
credentials = get_for_service_account_p12(client_email,
111-
private_key_path)
112-
return cls(project=project, credentials=credentials)
45+
_connection_class = Connection
11346

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

0 commit comments

Comments
 (0)