From 6974c2dc16aaf93b60fe5056e96bb6a7d1c04d79 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Date: Wed, 28 Aug 2019 09:33:58 -0700 Subject: [PATCH] Add client_options. (#9043) --- .../google/cloud/resource_manager/_http.py | 13 ++++--- .../google/cloud/resource_manager/client.py | 22 ++++++++++-- resource_manager/setup.py | 2 +- resource_manager/tests/unit/test__http.py | 8 ++++- resource_manager/tests/unit/test_client.py | 35 +++++++++++++++++++ 5 files changed, 71 insertions(+), 9 deletions(-) diff --git a/resource_manager/google/cloud/resource_manager/_http.py b/resource_manager/google/cloud/resource_manager/_http.py index 147b5649f9f7..dcf1aeb0b9ea 100644 --- a/resource_manager/google/cloud/resource_manager/_http.py +++ b/resource_manager/google/cloud/resource_manager/_http.py @@ -28,17 +28,20 @@ class Connection(_http.JSONConnection): :type client_info: :class:`~google.api_core.client_info.ClientInfo` :param client_info: (Optional) instance used to generate user agent. + + :type client_options: :class:`~google.api_core.client_options.ClientOptions` + :param client_options (Optional) Client options used to set user options + on the client. API Endpoint should be set through client_options. """ - def __init__(self, client, client_info=None): - super(Connection, self).__init__(client, client_info) + DEFAULT_API_ENDPOINT = "https://cloudresourcemanager.googleapis.com" + def __init__(self, client, client_info=None, api_endpoint=DEFAULT_API_ENDPOINT): + super(Connection, self).__init__(client, client_info) + self.API_BASE_URL = api_endpoint self._client_info.gapic_version = __version__ self._client_info.client_library_version = __version__ - API_BASE_URL = "https://cloudresourcemanager.googleapis.com" - """The base of the API call URL.""" - API_VERSION = "v1beta1" """The version of the API, used in building the API call's URL.""" diff --git a/resource_manager/google/cloud/resource_manager/client.py b/resource_manager/google/cloud/resource_manager/client.py index f9e180e29b18..70d15b311261 100644 --- a/resource_manager/google/cloud/resource_manager/client.py +++ b/resource_manager/google/cloud/resource_manager/client.py @@ -16,6 +16,7 @@ import six +import google.api_core.client_options from google.api_core import page_iterator from google.cloud.client import Client as BaseClient @@ -56,14 +57,31 @@ class Client(BaseClient): 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. + :type client_options: :class:`~google.api_core.client_options.ClientOptions` + or :class:`dict` + :param client_options: (Optional) Client options used to set user options + on the client. API Endpoint should be set through client_options. """ SCOPE = ("https://www.googleapis.com/auth/cloud-platform",) """The scopes required for authenticating as a Resouce Manager consumer.""" - def __init__(self, credentials=None, _http=None, client_info=None): + def __init__( + self, credentials=None, _http=None, client_info=None, client_options=None + ): super(Client, self).__init__(credentials=credentials, _http=_http) - self._connection = Connection(self, client_info=client_info) + + kw_args = {"client_info": client_info} + if client_options: + if type(client_options) == dict: + client_options = google.api_core.client_options.from_dict( + client_options + ) + if client_options.api_endpoint: + api_endpoint = client_options.api_endpoint + kw_args["api_endpoint"] = api_endpoint + + self._connection = Connection(self, **kw_args) def new_project(self, project_id, name=None, labels=None): """Create a project bound to the current client. diff --git a/resource_manager/setup.py b/resource_manager/setup.py index 554770069d90..f2f28c680f29 100644 --- a/resource_manager/setup.py +++ b/resource_manager/setup.py @@ -29,7 +29,7 @@ # 'Development Status :: 5 - Production/Stable' release_status = 'Development Status :: 3 - Alpha' dependencies = [ - "google-cloud-core >= 1.0.0, < 2.0dev", + "google-cloud-core >= 1.0.3, < 2.0dev", ] extras = { } diff --git a/resource_manager/tests/unit/test__http.py b/resource_manager/tests/unit/test__http.py index f2d665874954..619845cb3126 100644 --- a/resource_manager/tests/unit/test__http.py +++ b/resource_manager/tests/unit/test__http.py @@ -29,7 +29,13 @@ def _make_one(self, *args, **kw): def test_build_api_url_no_extra_query_params(self): conn = self._make_one(object()) - URI = "/".join([conn.API_BASE_URL, conn.API_VERSION, "foo"]) + URI = "/".join([conn.DEFAULT_API_ENDPOINT, conn.API_VERSION, "foo"]) + self.assertEqual(conn.build_api_url("/foo"), URI) + + def test_build_api_url_w_custom_endpoint(self): + custom_endpoint = "https://foo-cloudresourcemanager.googleapis.com" + conn = self._make_one(object(), api_endpoint=custom_endpoint) + URI = "/".join([custom_endpoint, conn.API_VERSION, "foo"]) self.assertEqual(conn.build_api_url("/foo"), URI) def test_build_api_url_w_extra_query_params(self): diff --git a/resource_manager/tests/unit/test_client.py b/resource_manager/tests/unit/test_client.py index 57d0085d15e7..7ecd814b5066 100644 --- a/resource_manager/tests/unit/test_client.py +++ b/resource_manager/tests/unit/test_client.py @@ -60,6 +60,41 @@ def test_ctor_w_client_info(self): self.assertIs(client._http_internal, http) self.assertIs(client._connection._client_info, client_info) + def test_ctor_w_empty_client_options(self): + from google.api_core.client_options import ClientOptions + + http = object() + client_options = ClientOptions() + client = self._make_one(_http=http, client_options=client_options) + self.assertEqual( + client._connection.API_BASE_URL, client._connection.DEFAULT_API_ENDPOINT + ) + + def test_ctor_w_client_options_object(self): + from google.api_core.client_options import ClientOptions + + http = object() + client_options = ClientOptions( + api_endpoint="https://foo-cloudresourcemanager.googleapis.com" + ) + client = self._make_one(_http=http, client_options=client_options) + self.assertEqual( + client._connection.API_BASE_URL, + "https://foo-cloudresourcemanager.googleapis.com", + ) + + def test_ctor_w_client_options_dict(self): + http = object() + client_options = { + "api_endpoint": "https://foo-cloudresourcemanager.googleapis.com" + } + + client = self._make_one(_http=http, client_options=client_options) + self.assertEqual( + client._connection.API_BASE_URL, + "https://foo-cloudresourcemanager.googleapis.com", + ) + def test_new_project_factory(self): from google.cloud.resource_manager.project import Project