Skip to content

Commit f8c157f

Browse files
emar-karcrwilcox
authored andcommitted
Datastore: Add client_options to constructors for manual clients. (#9055)
* Add `client_options` to datastore
1 parent b2c61ee commit f8c157f

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

packages/google-cloud-datastore/google/cloud/datastore/client.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import os
1717

18+
import google.api_core.client_options
1819
from google.cloud._helpers import _LocalStack
1920
from google.cloud._helpers import _determine_default_project as _base_default_project
2021
from google.cloud.client import ClientWithProject
@@ -201,6 +202,11 @@ class Client(ClientWithProject):
201202
you only need to set this if you're developing your
202203
own library or partner tool.
203204
205+
:type client_options: :class:`~google.api_core.client_options.ClientOptions`
206+
or :class:`dict`
207+
:param client_options: (Optional) Client options used to set user options on the
208+
client. API Endpoint should be set through client_options.
209+
204210
:type _http: :class:`~requests.Session`
205211
:param _http: (Optional) HTTP object to make requests. Can be any object
206212
that defines ``request()`` with the same interface as
@@ -228,6 +234,7 @@ def __init__(
228234
namespace=None,
229235
credentials=None,
230236
client_info=_CLIENT_INFO,
237+
client_options=None,
231238
_http=None,
232239
_use_grpc=None,
233240
):
@@ -236,6 +243,7 @@ def __init__(
236243
)
237244
self.namespace = namespace
238245
self._client_info = client_info
246+
self._client_options = client_options
239247
self._batch_stack = _LocalStack()
240248
self._datastore_api_internal = None
241249
if _use_grpc is None:
@@ -246,7 +254,15 @@ def __init__(
246254
host = os.environ[GCD_HOST]
247255
self._base_url = "http://" + host
248256
except KeyError:
249-
self._base_url = _DATASTORE_BASE_URL
257+
api_endpoint = _DATASTORE_BASE_URL
258+
if client_options:
259+
if type(client_options) == dict:
260+
client_options = google.api_core.client_options.from_dict(
261+
client_options
262+
)
263+
if client_options.api_endpoint:
264+
api_endpoint = client_options.api_endpoint
265+
self._base_url = api_endpoint
250266

251267
@staticmethod
252268
def _determine_default(project):

packages/google-cloud-datastore/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
release_status = "Development Status :: 5 - Production/Stable"
3131
dependencies = [
3232
"google-api-core[grpc] >= 1.14.0, < 2.0.0dev",
33-
"google-cloud-core >= 1.0.0, < 2.0dev",
33+
"google-cloud-core >= 1.0.3, < 2.0dev",
3434
]
3535
extras = {}
3636

packages/google-cloud-datastore/tests/unit/test_client.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def _make_one(
128128
namespace=None,
129129
credentials=None,
130130
client_info=None,
131+
client_options=None,
131132
_http=None,
132133
_use_grpc=None,
133134
):
@@ -136,6 +137,7 @@ def _make_one(
136137
namespace=namespace,
137138
credentials=credentials,
138139
client_info=client_info,
140+
client_options=client_options,
139141
_http=_http,
140142
_use_grpc=_use_grpc,
141143
)
@@ -172,6 +174,7 @@ def test_constructor_w_implicit_inputs(self):
172174
self.assertIs(client._credentials, creds)
173175
self.assertIs(client._client_info, _CLIENT_INFO)
174176
self.assertIsNone(client._http_internal)
177+
self.assertIsNone(client._client_options)
175178
self.assertEqual(client.base_url, _DATASTORE_BASE_URL)
176179

177180
self.assertIsNone(client.current_batch)
@@ -181,18 +184,20 @@ def test_constructor_w_implicit_inputs(self):
181184
_determine_default_project.assert_called_once_with(None)
182185

183186
def test_constructor_w_explicit_inputs(self):
184-
from google.cloud.datastore.client import _DATASTORE_BASE_URL
187+
from google.api_core.client_options import ClientOptions
185188

186189
other = "other"
187190
namespace = "namespace"
188191
creds = _make_credentials()
189192
client_info = mock.Mock()
193+
client_options = ClientOptions("endpoint")
190194
http = object()
191195
client = self._make_one(
192196
project=other,
193197
namespace=namespace,
194198
credentials=creds,
195199
client_info=client_info,
200+
client_options=client_options,
196201
_http=http,
197202
)
198203
self.assertEqual(client.project, other)
@@ -201,8 +206,8 @@ def test_constructor_w_explicit_inputs(self):
201206
self.assertIs(client._client_info, client_info)
202207
self.assertIs(client._http_internal, http)
203208
self.assertIsNone(client.current_batch)
209+
self.assertIs(client._base_url, "endpoint")
204210
self.assertEqual(list(client._batch_stack), [])
205-
self.assertEqual(client.base_url, _DATASTORE_BASE_URL)
206211

207212
def test_constructor_use_grpc_default(self):
208213
import google.cloud.datastore.client as MUT
@@ -243,12 +248,39 @@ def test_constructor_gcd_host(self):
243248
self.assertEqual(client.base_url, "http://" + host)
244249

245250
def test_base_url_property(self):
251+
from google.cloud.datastore.client import _DATASTORE_BASE_URL
252+
from google.api_core.client_options import ClientOptions
253+
246254
alternate_url = "https://alias.example.com/"
247255
project = "PROJECT"
248256
creds = _make_credentials()
249257
http = object()
258+
client_options = ClientOptions()
250259

251-
client = self._make_one(project=project, credentials=creds, _http=http)
260+
client = self._make_one(
261+
project=project,
262+
credentials=creds,
263+
_http=http,
264+
client_options=client_options,
265+
)
266+
self.assertEqual(client.base_url, _DATASTORE_BASE_URL)
267+
client.base_url = alternate_url
268+
self.assertEqual(client.base_url, alternate_url)
269+
270+
def test_base_url_property_w_client_options(self):
271+
alternate_url = "https://alias.example.com/"
272+
project = "PROJECT"
273+
creds = _make_credentials()
274+
http = object()
275+
client_options = {"api_endpoint": "endpoint"}
276+
277+
client = self._make_one(
278+
project=project,
279+
credentials=creds,
280+
_http=http,
281+
client_options=client_options,
282+
)
283+
self.assertEqual(client.base_url, "endpoint")
252284
client.base_url = alternate_url
253285
self.assertEqual(client.base_url, alternate_url)
254286

0 commit comments

Comments
 (0)