Skip to content

Commit 090ef1b

Browse files
committed
Allow bytes or string as project value in datastore clients.
Fixes googleapis#1625. Was "fixed" in googleapis#1505 but missed datastore.
1 parent 98edc64 commit 090ef1b

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

gcloud/client.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,26 @@ class _ClientProjectMixin(object):
132132
passed falls back to the default inferred from the
133133
environment.
134134
135-
:raises: :class:`ValueError` if the project is neither passed in nor
136-
set in the environment.
135+
:raises: :class:`EnvironmentError` if the project is neither passed in nor
136+
set in the environment. :class:`ValueError` if the project value
137+
is invalid.
137138
"""
138139

139140
def __init__(self, project=None):
140-
project = _determine_default_project(project)
141+
project = self._determine_default(project)
141142
if project is None:
142-
raise ValueError('Project was not passed and could not be '
143-
'determined from the environment.')
143+
raise EnvironmentError('Project was not passed and could not be '
144+
'determined from the environment.')
144145
if isinstance(project, six.binary_type):
145146
project = project.decode('utf-8')
146147
if not isinstance(project, six.string_types):
147148
raise ValueError('Project must be a string.')
148149
self.project = project
149150

151+
@staticmethod
152+
def _determine_default(project):
153+
return _determine_default_project(project)
154+
150155

151156
class JSONClient(Client, _ClientProjectMixin):
152157
"""Client to for Google JSON-based API.

gcloud/datastore/client.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from gcloud._helpers import _LocalStack
1919
from gcloud._helpers import _app_engine_id
2020
from gcloud._helpers import _compute_engine_id
21+
from gcloud.client import _ClientProjectMixin
2122
from gcloud.client import Client as _BaseClient
2223
from gcloud.datastore import helpers
2324
from gcloud.datastore.connection import Connection
@@ -155,7 +156,7 @@ def _extended_lookup(connection, project, key_pbs,
155156
return results
156157

157158

158-
class Client(_BaseClient):
159+
class Client(_BaseClient, _ClientProjectMixin):
159160
"""Convenience wrapper for invoking APIs/factories w/ a project.
160161
161162
:type project: string
@@ -180,14 +181,15 @@ class Client(_BaseClient):
180181

181182
def __init__(self, project=None, namespace=None,
182183
credentials=None, http=None):
183-
project = _determine_default_project(project)
184-
if project is None:
185-
raise EnvironmentError('Project could not be inferred.')
186-
self.project = project
184+
_ClientProjectMixin.__init__(self, project=project)
187185
self.namespace = namespace
188186
self._batch_stack = _LocalStack()
189187
super(Client, self).__init__(credentials, http)
190188

189+
@staticmethod
190+
def _determine_default(project):
191+
return _determine_default_project(project)
192+
191193
def _push_batch(self, batch):
192194
"""Push a batch/transaction onto our stack.
193195

gcloud/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def mock_determine_proj(project):
171171
return None
172172

173173
with _Monkey(client, _determine_default_project=mock_determine_proj):
174-
self.assertRaises(ValueError, self._makeOne)
174+
self.assertRaises(EnvironmentError, self._makeOne)
175175

176176
self.assertEqual(FUNC_CALLS, [(None, '_determine_default_project')])
177177

0 commit comments

Comments
 (0)