Skip to content

Commit aea68fd

Browse files
busunkim96emar-kar
authored andcommitted
Add client_options. (googleapis#9043)
1 parent 05b53bf commit aea68fd

5 files changed

Lines changed: 71 additions & 9 deletions

File tree

resource_manager/google/cloud/resource_manager/_http.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,20 @@ class Connection(_http.JSONConnection):
2828
2929
:type client_info: :class:`~google.api_core.client_info.ClientInfo`
3030
:param client_info: (Optional) instance used to generate user agent.
31+
32+
:type client_options: :class:`~google.api_core.client_options.ClientOptions`
33+
:param client_options (Optional) Client options used to set user options
34+
on the client. API Endpoint should be set through client_options.
3135
"""
3236

33-
def __init__(self, client, client_info=None):
34-
super(Connection, self).__init__(client, client_info)
37+
DEFAULT_API_ENDPOINT = "https://cloudresourcemanager.googleapis.com"
3538

39+
def __init__(self, client, client_info=None, api_endpoint=DEFAULT_API_ENDPOINT):
40+
super(Connection, self).__init__(client, client_info)
41+
self.API_BASE_URL = api_endpoint
3642
self._client_info.gapic_version = __version__
3743
self._client_info.client_library_version = __version__
3844

39-
API_BASE_URL = "https://cloudresourcemanager.googleapis.com"
40-
"""The base of the API call URL."""
41-
4245
API_VERSION = "v1beta1"
4346
"""The version of the API, used in building the API call's URL."""
4447

resource_manager/google/cloud/resource_manager/client.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import six
1818

19+
import google.api_core.client_options
1920
from google.api_core import page_iterator
2021
from google.cloud.client import Client as BaseClient
2122

@@ -56,14 +57,31 @@ class Client(BaseClient):
5657
requests. If ``None``, then default info will be used. Generally,
5758
you only need to set this if you're developing your own library
5859
or partner tool.
60+
:type client_options: :class:`~google.api_core.client_options.ClientOptions`
61+
or :class:`dict`
62+
:param client_options: (Optional) Client options used to set user options
63+
on the client. API Endpoint should be set through client_options.
5964
"""
6065

6166
SCOPE = ("https://www.googleapis.com/auth/cloud-platform",)
6267
"""The scopes required for authenticating as a Resouce Manager consumer."""
6368

64-
def __init__(self, credentials=None, _http=None, client_info=None):
69+
def __init__(
70+
self, credentials=None, _http=None, client_info=None, client_options=None
71+
):
6572
super(Client, self).__init__(credentials=credentials, _http=_http)
66-
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)
6785

6886
def new_project(self, project_id, name=None, labels=None):
6987
"""Create a project bound to the current client.

resource_manager/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
# 'Development Status :: 5 - Production/Stable'
3030
release_status = 'Development Status :: 3 - Alpha'
3131
dependencies = [
32-
"google-cloud-core >= 1.0.0, < 2.0dev",
32+
"google-cloud-core >= 1.0.3, < 2.0dev",
3333
]
3434
extras = {
3535
}

resource_manager/tests/unit/test__http.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ def _make_one(self, *args, **kw):
2929

3030
def test_build_api_url_no_extra_query_params(self):
3131
conn = self._make_one(object())
32-
URI = "/".join([conn.API_BASE_URL, conn.API_VERSION, "foo"])
32+
URI = "/".join([conn.DEFAULT_API_ENDPOINT, conn.API_VERSION, "foo"])
33+
self.assertEqual(conn.build_api_url("/foo"), URI)
34+
35+
def test_build_api_url_w_custom_endpoint(self):
36+
custom_endpoint = "https://foo-cloudresourcemanager.googleapis.com"
37+
conn = self._make_one(object(), api_endpoint=custom_endpoint)
38+
URI = "/".join([custom_endpoint, conn.API_VERSION, "foo"])
3339
self.assertEqual(conn.build_api_url("/foo"), URI)
3440

3541
def test_build_api_url_w_extra_query_params(self):

resource_manager/tests/unit/test_client.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,41 @@ def test_ctor_w_client_info(self):
6060
self.assertIs(client._http_internal, http)
6161
self.assertIs(client._connection._client_info, client_info)
6262

63+
def test_ctor_w_empty_client_options(self):
64+
from google.api_core.client_options import ClientOptions
65+
66+
http = object()
67+
client_options = ClientOptions()
68+
client = self._make_one(_http=http, client_options=client_options)
69+
self.assertEqual(
70+
client._connection.API_BASE_URL, client._connection.DEFAULT_API_ENDPOINT
71+
)
72+
73+
def test_ctor_w_client_options_object(self):
74+
from google.api_core.client_options import ClientOptions
75+
76+
http = object()
77+
client_options = ClientOptions(
78+
api_endpoint="https://foo-cloudresourcemanager.googleapis.com"
79+
)
80+
client = self._make_one(_http=http, client_options=client_options)
81+
self.assertEqual(
82+
client._connection.API_BASE_URL,
83+
"https://foo-cloudresourcemanager.googleapis.com",
84+
)
85+
86+
def test_ctor_w_client_options_dict(self):
87+
http = object()
88+
client_options = {
89+
"api_endpoint": "https://foo-cloudresourcemanager.googleapis.com"
90+
}
91+
92+
client = self._make_one(_http=http, client_options=client_options)
93+
self.assertEqual(
94+
client._connection.API_BASE_URL,
95+
"https://foo-cloudresourcemanager.googleapis.com",
96+
)
97+
6398
def test_new_project_factory(self):
6499
from google.cloud.resource_manager.project import Project
65100

0 commit comments

Comments
 (0)