Skip to content

Commit

Permalink
Firestore: Add client_options to v1. (googleapis#9048)
Browse files Browse the repository at this point in the history
  • Loading branch information
busunkim96 authored and emar-kar committed Sep 18, 2019
1 parent c3c7c4a commit 4b67624
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
18 changes: 16 additions & 2 deletions firestore/google/cloud/firestore_v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"""
import os

import google.api_core.client_options
from google.api_core.gapic_v1 import client_info
from google.cloud.client import ClientWithProject

Expand Down Expand Up @@ -79,6 +80,9 @@ class Client(ClientWithProject):
requests. If ``None``, then default info will be used. Generally,
you only need to set this if you're developing your own library
or partner tool.
client_options (Union[dict, google.api_core.client_options.ClientOptions]):
Client options used to set user options on the client. API Endpoint
should be set through client_options.
"""

SCOPE = (
Expand All @@ -97,6 +101,7 @@ def __init__(
credentials=None,
database=DEFAULT_DATABASE,
client_info=_CLIENT_INFO,
client_options=None,
):
# NOTE: This API has no use for the _http argument, but sending it
# will have no impact since the _http() @property only lazily
Expand All @@ -105,6 +110,13 @@ def __init__(
project=project, credentials=credentials, _http=None
)
self._client_info = client_info
if client_options:
if type(client_options) == dict:
client_options = google.api_core.client_options.from_dict(
client_options
)
self._client_options = client_options

self._database = database
self._emulator_host = os.getenv(_FIRESTORE_EMULATOR_HOST)

Expand Down Expand Up @@ -150,8 +162,10 @@ def _target(self):
"""
if self._emulator_host is not None:
return self._emulator_host

return firestore_client.FirestoreClient.SERVICE_ADDRESS
elif self._client_options and self._client_options.api_endpoint:
return self._client_options.api_endpoint
else:
return firestore_client.FirestoreClient.SERVICE_ADDRESS

@property
def _database_string(self):
Expand Down
12 changes: 12 additions & 0 deletions firestore/tests/unit/v1/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,28 @@ def test_constructor_explicit(self):
credentials = _make_credentials()
database = "now-db"
client_info = mock.Mock()
client_options = mock.Mock()
client = self._make_one(
project=self.PROJECT,
credentials=credentials,
database=database,
client_info=client_info,
client_options=client_options,
)
self.assertEqual(client.project, self.PROJECT)
self.assertEqual(client._credentials, credentials)
self.assertEqual(client._database, database)
self.assertIs(client._client_info, client_info)
self.assertIs(client._client_options, client_options)

def test_constructor_w_client_options(self):
credentials = _make_credentials()
client = self._make_one(
project=self.PROJECT,
credentials=credentials,
client_options={"api_endpoint": "foo-firestore.googleapis.com"},
)
self.assertEqual(client._target, "foo-firestore.googleapis.com")

@mock.patch(
"google.cloud.firestore_v1.gapic.firestore_client.FirestoreClient",
Expand Down

0 comments on commit 4b67624

Please sign in to comment.