Content-Length: 618327 | pFad | http://github.com/googleapis/google-cloud-python/commit/98639f8b79f3f50d1feb98466084aab843c0ea8f

95 feat(dns): add 'client_options' argument to client ctor (#9516) · googleapis/google-cloud-python@98639f8 · GitHub
Skip to content

Commit 98639f8

Browse files
authored
feat(dns): add 'client_options' argument to client ctor (#9516)
Toward #8475
1 parent c6ecf19 commit 98639f8

File tree

4 files changed

+87
-8
lines changed

4 files changed

+87
-8
lines changed

dns/google/cloud/dns/_http.py

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

32-
def __init__(self, client, client_info=None):
33-
super(Connection, self).__init__(client, client_info)
32+
DEFAULT_API_ENDPOINT = "https://dns.googleapis.com"
3433

34+
def __init__(self, client, client_info=None, api_endpoint=DEFAULT_API_ENDPOINT):
35+
super(Connection, self).__init__(client, client_info)
36+
self.API_BASE_URL = api_endpoint
3537
self._client_info.gapic_version = __version__
3638
self._client_info.client_library_version = __version__
3739

38-
API_BASE_URL = "https://dns.googleapis.com"
39-
"""The base of the API call URL."""
40-
4140
API_VERSION = "v1"
4241
"""The version of the API, used in building the API call's URL."""
4342

dns/google/cloud/dns/client.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Client for interacting with the Google Cloud DNS API."""
1616

1717
from google.api_core import page_iterator
18+
from google.api_core import client_options as client_options_mod
1819
from google.cloud.client import ClientWithProject
1920

2021
from google.cloud.dns._http import Connection
@@ -50,16 +51,37 @@ class Client(ClientWithProject):
5051
requests. If ``None``, then default info will be used. Generally,
5152
you only need to set this if you're developing your own library
5253
or partner tool.
54+
55+
:type client_options: :class:`~google.api_core.client_options.ClientOptions`
56+
or :class:`dict`
57+
:param client_options: (Optional) Client options used to set user options
58+
on the client. API Endpoint should be set through client_options.
5359
"""
5460

5561
SCOPE = ("https://www.googleapis.com/auth/ndev.clouddns.readwrite",)
5662
"""The scopes required for authenticating as a Cloud DNS consumer."""
5763

58-
def __init__(self, project=None, credentials=None, _http=None, client_info=None):
64+
def __init__(
65+
self,
66+
project=None,
67+
credentials=None,
68+
_http=None,
69+
client_info=None,
70+
client_options=None,
71+
):
5972
super(Client, self).__init__(
6073
project=project, credentials=credentials, _http=_http
6174
)
62-
self._connection = Connection(self, client_info=client_info)
75+
76+
kwargs = {"client_info": client_info}
77+
if client_options:
78+
if isinstance(client_options, dict):
79+
client_options = client_options_mod.from_dict(client_options)
80+
81+
if client_options.api_endpoint:
82+
kwargs["api_endpoint"] = client_options.api_endpoint
83+
84+
self._connection = Connection(self, **kwargs)
6385

6486
def quotas(self):
6587
"""Return DNS quotas for the project associated with this client.

dns/tests/unit/test__http.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ def test_build_api_url_w_extra_query_params(self):
4444
parms = dict(parse_qsl(qs))
4545
self.assertEqual(parms["bar"], "baz")
4646

47+
def test_build_api_url_w_custom_endpoint(self):
48+
custom_endpoint = "https://foo-dns.googleapis.com"
49+
conn = self._make_one(object(), api_endpoint=custom_endpoint)
50+
URI = "/".join([custom_endpoint, "dns", conn.API_VERSION, "foo"])
51+
self.assertEqual(conn.build_api_url("/foo"), URI)
52+
4753
def test_extra_headers(self):
4854
import requests
4955
from google.cloud import _http as base_http

dns/tests/unit/test_client.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def _get_target_class():
3737
def _make_one(self, *args, **kw):
3838
return self._get_target_class()(*args, **kw)
3939

40-
def test_ctor(self):
40+
def test_ctor_defaults(self):
4141
from google.api_core.client_info import ClientInfo
4242
from google.cloud.dns._http import Connection
4343

@@ -48,6 +48,9 @@ def test_ctor(self):
4848
self.assertIs(client._connection.credentials, creds)
4949
self.assertIs(client._connection.http, http)
5050
self.assertIsInstance(client._connection._client_info, ClientInfo)
51+
self.assertEqual(
52+
client._connection.API_BASE_URL, client._connection.DEFAULT_API_ENDPOINT
53+
)
5154

5255
def test_ctor_w_client_info(self):
5356
from google.api_core.client_info import ClientInfo
@@ -65,6 +68,55 @@ def test_ctor_w_client_info(self):
6568
self.assertIs(client._connection.http, http)
6669
self.assertIs(client._connection._client_info, client_info)
6770

71+
def test_ctor_w_empty_client_options_object(self):
72+
from google.api_core.client_info import ClientInfo
73+
from google.api_core.client_options import ClientOptions
74+
from google.cloud.dns._http import Connection
75+
76+
creds = _make_credentials()
77+
http = object()
78+
client = self._make_one(
79+
project=self.PROJECT,
80+
credentials=creds,
81+
_http=http,
82+
client_options=ClientOptions(),
83+
)
84+
self.assertIsInstance(client._connection, Connection)
85+
self.assertIs(client._connection.credentials, creds)
86+
self.assertIs(client._connection.http, http)
87+
self.assertIsInstance(client._connection._client_info, ClientInfo)
88+
self.assertEqual(
89+
client._connection.API_BASE_URL, client._connection.DEFAULT_API_ENDPOINT
90+
)
91+
92+
def test_ctor_w_client_options_object(self):
93+
from google.api_core.client_options import ClientOptions
94+
95+
api_endpoint = "https://foo-dns.googleapis.com"
96+
creds = _make_credentials()
97+
http = object()
98+
client_options = ClientOptions(api_endpoint=api_endpoint)
99+
client = self._make_one(
100+
project=self.PROJECT,
101+
credentials=creds,
102+
_http=http,
103+
client_options=client_options,
104+
)
105+
self.assertEqual(client._connection.API_BASE_URL, api_endpoint)
106+
107+
def test_ctor_w_client_options_dict(self):
108+
api_endpoint = "https://foo-dns.googleapis.com"
109+
creds = _make_credentials()
110+
http = object()
111+
client_options = {"api_endpoint": api_endpoint}
112+
client = self._make_one(
113+
project=self.PROJECT,
114+
credentials=creds,
115+
_http=http,
116+
client_options=client_options,
117+
)
118+
self.assertEqual(client._connection.API_BASE_URL, api_endpoint)
119+
68120
def test_quotas_defaults(self):
69121
PATH = "projects/%s" % (self.PROJECT,)
70122
MANAGED_ZONES = 1234

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/googleapis/google-cloud-python/commit/98639f8b79f3f50d1feb98466084aab843c0ea8f

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy