Skip to content

Commit fa1cd00

Browse files
authored
Add client options. (#9045)
1 parent fc507ec commit fa1cd00

File tree

5 files changed

+66
-12
lines changed

5 files changed

+66
-12
lines changed

packages/google-cloud-runtimeconfig/docs/conf.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,7 @@
340340
intersphinx_mapping = {
341341
"python": ("http://python.readthedocs.org/en/latest/", None),
342342
"google-auth": ("https://google-auth.readthedocs.io/en/stable", None),
343-
"google.api_core": (
344-
"https://googleapis.github.io/google-cloud-python/latest",
345-
None,
346-
),
343+
"google.api_core": ("https://googleapis.dev/python/google-api-core/latest", None),
347344
"grpc": ("https://grpc.io/grpc/python/", None),
348345
"requests": ("https://2.python-requests.org/en/master/", None),
349346
}

packages/google-cloud-runtimeconfig/google/cloud/runtimeconfig/_http.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@ class Connection(_http.JSONConnection):
3131
:param client_info: (Optional) instance used to generate user agent.
3232
"""
3333

34-
def __init__(self, client, client_info=None):
35-
super(Connection, self).__init__(client, client_info)
34+
DEFAULT_API_ENDPOINT = "https://runtimeconfig.googleapis.com"
3635

36+
def __init__(self, client, client_info=None, api_endpoint=DEFAULT_API_ENDPOINT):
37+
super(Connection, self).__init__(client, client_info)
38+
self.API_BASE_URL = api_endpoint
3739
self._client_info.gapic_version = __version__
3840
self._client_info.client_library_version = __version__
3941

40-
API_BASE_URL = "https://runtimeconfig.googleapis.com"
41-
"""The base of the API call URL."""
42-
4342
API_VERSION = "v1beta1"
4443
"""The version of the API, used in building the API call's URL."""
4544

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
"""Client for interacting with the Google Cloud RuntimeConfig API."""
1616

17-
17+
import google.api_core.client_options
1818
from google.cloud.client import ClientWithProject
1919

2020
from google.cloud.runtimeconfig._http import Connection
@@ -50,16 +50,38 @@ class Client(ClientWithProject):
5050
requests. If ``None``, then default info will be used. Generally,
5151
you only need to set this if you're developing your own library
5252
or partner tool.
53+
:type client_options: :class:`~google.api_core.client_options.ClientOptions`
54+
or :class:`dict`
55+
:param client_options: (Optional) Client options used to set user options
56+
on the client. API Endpoint should be set through client_options.
5357
"""
5458

5559
SCOPE = ("https://www.googleapis.com/auth/cloudruntimeconfig",)
5660
"""The scopes required for authenticating as a RuntimeConfig consumer."""
5761

58-
def __init__(self, project=None, credentials=None, _http=None, client_info=None):
62+
def __init__(
63+
self,
64+
project=None,
65+
credentials=None,
66+
_http=None,
67+
client_info=None,
68+
client_options=None,
69+
):
5970
super(Client, self).__init__(
6071
project=project, credentials=credentials, _http=_http
6172
)
62-
self._connection = Connection(self, client_info=client_info)
73+
74+
kw_args = {"client_info": client_info}
75+
if client_options:
76+
if type(client_options) == dict:
77+
client_options = google.api_core.client_options.from_dict(
78+
client_options
79+
)
80+
if client_options.api_endpoint:
81+
api_endpoint = client_options.api_endpoint
82+
kw_args["api_endpoint"] = api_endpoint
83+
84+
self._connection = Connection(self, **kw_args)
6385

6486
def config(self, config_name):
6587
"""Factory constructor for config object.

packages/google-cloud-runtimeconfig/tests/unit/test__http.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ def test_default_url(self):
3232
conn = self._make_one(client)
3333
self.assertIs(conn._client, client)
3434

35+
def test_build_api_url_w_custom_endpoint(self):
36+
custom_endpoint = "https://foo-runtimeconfig.googleapis.com"
37+
conn = self._make_one(object(), api_endpoint=custom_endpoint)
38+
URI = "/".join([custom_endpoint, conn.API_VERSION, "foo"])
39+
self.assertEqual(conn.build_api_url("/foo"), URI)
40+
3541
def test_extra_headers(self):
3642
import requests
3743
from google.cloud import _http as base_http

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,36 @@ def test_ctor_w_client_info(self):
6464
self.assertIs(client._http_internal, http)
6565
self.assertIs(client._connection._client_info, client_info)
6666

67+
def test_ctor_w_empty_client_options(self):
68+
from google.api_core.client_options import ClientOptions
69+
70+
http = object()
71+
client_options = ClientOptions()
72+
client = self._make_one(_http=http, client_options=client_options)
73+
self.assertEqual(
74+
client._connection.API_BASE_URL, client._connection.DEFAULT_API_ENDPOINT
75+
)
76+
77+
def test_constructor_w_client_options_object(self):
78+
from google.api_core.client_options import ClientOptions
79+
80+
http = object()
81+
client_options = ClientOptions(
82+
api_endpoint="https://foo-runtimeconfig.googleapis.com"
83+
)
84+
client = self._make_one(_http=http, client_options=client_options)
85+
self.assertEqual(
86+
client._connection.API_BASE_URL, "https://foo-runtimeconfig.googleapis.com"
87+
)
88+
89+
def test_constructor_w_client_options_dict(self):
90+
http = object()
91+
client_options = {"api_endpoint": "https://foo-runtimeconfig.googleapis.com"}
92+
client = self._make_one(_http=http, client_options=client_options)
93+
self.assertEqual(
94+
client._connection.API_BASE_URL, "https://foo-runtimeconfig.googleapis.com"
95+
)
96+
6797
def test_config(self):
6898
PROJECT = "PROJECT"
6999
CONFIG_NAME = "config_name"

0 commit comments

Comments
 (0)