From 19a3842e1bb6debb268039d17747878e6e3d3798 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Tue, 23 Jul 2019 16:07:49 -0700 Subject: [PATCH 1/6] Add client options to translate_v2. --- translate/google/cloud/translate_v2/_http.py | 8 ++++---- translate/google/cloud/translate_v2/client.py | 19 ++++++++++++++++++- translate/tests/unit/test__http.py | 8 ++++++++ translate/tests/unit/test_client.py | 8 +++++++- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/translate/google/cloud/translate_v2/_http.py b/translate/google/cloud/translate_v2/_http.py index b73f5f595450..1db01164e772 100644 --- a/translate/google/cloud/translate_v2/_http.py +++ b/translate/google/cloud/translate_v2/_http.py @@ -29,15 +29,15 @@ class Connection(_http.JSONConnection): :param client_info: (Optional) instance used to generate user agent. """ - def __init__(self, client, client_info=None): + DEFAULT_API_ENDPOINT = "https://translation.googleapis.com" + + def __init__(self, client, client_info=None, api_endpoint=DEFAULT_API_ENDPOINT): + Connection.API_BASE_URL = api_endpoint super(Connection, self).__init__(client, client_info) self._client_info.gapic_version = __version__ self._client_info.client_library_version = __version__ - API_BASE_URL = "https://translation.googleapis.com" - """The base of the API call URL.""" - API_VERSION = "v2" """The version of the API, used in building the API call's URL.""" diff --git a/translate/google/cloud/translate_v2/client.py b/translate/google/cloud/translate_v2/client.py index 9e7e4d351478..fedbe97cd4ce 100644 --- a/translate/google/cloud/translate_v2/client.py +++ b/translate/google/cloud/translate_v2/client.py @@ -17,6 +17,7 @@ import six +import google.api_core.client_options from google.cloud.client import Client as BaseClient from google.cloud.translate_v2._http import Connection @@ -61,6 +62,9 @@ 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",) @@ -72,10 +76,23 @@ def __init__( credentials=None, _http=None, client_info=None, + client_options=None, ): self.target_language = target_language super(Client, self).__init__(credentials=credentials, _http=_http) - self._connection = Connection(self, client_info=client_info) + + api_endpoint = None + 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 + + self._connection = Connection( + self, client_info=client_info, api_endpoint=api_endpoint + ) def get_languages(self, target_language=None): """Get list of supported languages for translation. diff --git a/translate/tests/unit/test__http.py b/translate/tests/unit/test__http.py index d179ce330653..a2dbbee9a35a 100644 --- a/translate/tests/unit/test__http.py +++ b/translate/tests/unit/test__http.py @@ -34,6 +34,14 @@ def test_build_api_url_no_extra_query_params(self): ) self.assertEqual(conn.build_api_url("/foo"), URI) + def test_build_api_url_w_custom_endpoint(self): + custom_endpoint = "https://foo-translation.googleapis.com" + conn = self._make_one(object(), api_endpoint=custom_endpoint) + URI = "/".join( + [custom_endpoint, "language", "translate", conn.API_VERSION, "foo"] + ) + self.assertEqual(conn.build_api_url("/foo"), URI) + def test_build_api_url_w_extra_query_params(self): from six.moves.urllib.parse import parse_qsl from six.moves.urllib.parse import urlsplit diff --git a/translate/tests/unit/test_client.py b/translate/tests/unit/test_client.py index e014657bc882..a5a0d05d5e83 100644 --- a/translate/tests/unit/test_client.py +++ b/translate/tests/unit/test_client.py @@ -46,13 +46,19 @@ def test_constructor_explicit(self): target = "es" client_info = ClientInfo() client = self._make_one( - target_language=target, _http=http, client_info=client_info + target_language=target, + _http=http, + client_info=client_info, + client_options={"api_endpoint": "https://foo-translation.googleapis.com"}, ) self.assertIsInstance(client._connection, Connection) self.assertIsNone(client._connection.credentials) self.assertIs(client._connection.http, http) self.assertEqual(client.target_language, target) self.assertIs(client._connection._client_info, client_info) + self.assertEqual( + client._connection.API_BASE_URL, "https://foo-translation.googleapis.com" + ) def test_get_languages(self): from google.cloud.translate_v2.client import ENGLISH_ISO_639 From e10e552b463c1a8b8dc9eaee76847c56e99dbfa2 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Tue, 23 Jul 2019 17:40:01 -0700 Subject: [PATCH 2/6] Use translation.googleapis.com as default endpoint. --- translate/google/cloud/translate_v2/_http.py | 4 +++- translate/tests/unit/test__http.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/translate/google/cloud/translate_v2/_http.py b/translate/google/cloud/translate_v2/_http.py index 1db01164e772..67dd554bae4f 100644 --- a/translate/google/cloud/translate_v2/_http.py +++ b/translate/google/cloud/translate_v2/_http.py @@ -31,7 +31,9 @@ class Connection(_http.JSONConnection): DEFAULT_API_ENDPOINT = "https://translation.googleapis.com" - def __init__(self, client, client_info=None, api_endpoint=DEFAULT_API_ENDPOINT): + def __init__(self, client, client_info=None, api_endpoint=None): + if api_endpoint is None: + api_endpoint = self.DEFAULT_API_ENDPOINT Connection.API_BASE_URL = api_endpoint super(Connection, self).__init__(client, client_info) diff --git a/translate/tests/unit/test__http.py b/translate/tests/unit/test__http.py index a2dbbee9a35a..4e9510928f2d 100644 --- a/translate/tests/unit/test__http.py +++ b/translate/tests/unit/test__http.py @@ -30,7 +30,7 @@ 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, "language", "translate", conn.API_VERSION, "foo"] + [conn.DEFAULT_API_ENDPOINT, "language", "translate", conn.API_VERSION, "foo"] ) self.assertEqual(conn.build_api_url("/foo"), URI) From e6345182cc8568955774b0d45f25baa7e0caa740 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Wed, 24 Jul 2019 10:16:33 -0700 Subject: [PATCH 3/6] Raise coverage. --- translate/google/cloud/translate_v2/_http.py | 4 +- translate/google/cloud/translate_v2/client.py | 7 ++-- translate/tests/unit/test__http.py | 8 +++- translate/tests/unit/test_client.py | 40 +++++++++++++++++++ 4 files changed, 51 insertions(+), 8 deletions(-) diff --git a/translate/google/cloud/translate_v2/_http.py b/translate/google/cloud/translate_v2/_http.py index 67dd554bae4f..1db01164e772 100644 --- a/translate/google/cloud/translate_v2/_http.py +++ b/translate/google/cloud/translate_v2/_http.py @@ -31,9 +31,7 @@ class Connection(_http.JSONConnection): DEFAULT_API_ENDPOINT = "https://translation.googleapis.com" - def __init__(self, client, client_info=None, api_endpoint=None): - if api_endpoint is None: - api_endpoint = self.DEFAULT_API_ENDPOINT + def __init__(self, client, client_info=None, api_endpoint=DEFAULT_API_ENDPOINT): Connection.API_BASE_URL = api_endpoint super(Connection, self).__init__(client, client_info) diff --git a/translate/google/cloud/translate_v2/client.py b/translate/google/cloud/translate_v2/client.py index fedbe97cd4ce..6c561675d25e 100644 --- a/translate/google/cloud/translate_v2/client.py +++ b/translate/google/cloud/translate_v2/client.py @@ -81,7 +81,7 @@ def __init__( self.target_language = target_language super(Client, self).__init__(credentials=credentials, _http=_http) - api_endpoint = None + kw_args = {"client_info": client_info} if client_options: if type(client_options) == dict: client_options = google.api_core.client_options.from_dict( @@ -89,10 +89,9 @@ def __init__( ) if client_options.api_endpoint: api_endpoint = client_options.api_endpoint + kw_args["api_endpoint"] = api_endpoint - self._connection = Connection( - self, client_info=client_info, api_endpoint=api_endpoint - ) + self._connection = Connection(self, **kw_args) def get_languages(self, target_language=None): """Get list of supported languages for translation. diff --git a/translate/tests/unit/test__http.py b/translate/tests/unit/test__http.py index 4e9510928f2d..208f207c8380 100644 --- a/translate/tests/unit/test__http.py +++ b/translate/tests/unit/test__http.py @@ -30,7 +30,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.DEFAULT_API_ENDPOINT, "language", "translate", conn.API_VERSION, "foo"] + [ + conn.DEFAULT_API_ENDPOINT, + "language", + "translate", + conn.API_VERSION, + "foo", + ] ) self.assertEqual(conn.build_api_url("/foo"), URI) diff --git a/translate/tests/unit/test_client.py b/translate/tests/unit/test_client.py index a5a0d05d5e83..33c1bca5e986 100644 --- a/translate/tests/unit/test_client.py +++ b/translate/tests/unit/test_client.py @@ -60,6 +60,46 @@ def test_constructor_explicit(self): client._connection.API_BASE_URL, "https://foo-translation.googleapis.com" ) + def test_constructor_w_empty_client_options(self): + from google.cloud._http import ClientInfo + from google.api_core.client_options import ClientOptions + from google.cloud.translate_v2._http import Connection + + http = object() + target = "es" + client_info = ClientInfo() + client_options = ClientOptions() + client = self._make_one( + target_language=target, + _http=http, + client_info=client_info, + client_options=client_options, + ) + self.assertEqual( + client._connection.API_BASE_URL, client._connection.DEFAULT_API_ENDPOINT + ) + + def test_constructor_w_client_options_object(self): + from google.cloud._http import ClientInfo + from google.api_core.client_options import ClientOptions + from google.cloud.translate_v2._http import Connection + + http = object() + target = "es" + client_info = ClientInfo() + client_options = ClientOptions( + api_endpoint="https://foo-translation.googleapis.com" + ) + client = self._make_one( + target_language=target, + _http=http, + client_info=client_info, + client_options=client_options, + ) + self.assertEqual( + client._connection.API_BASE_URL, "https://foo-translation.googleapis.com" + ) + def test_get_languages(self): from google.cloud.translate_v2.client import ENGLISH_ISO_639 From 98a21ea8a12835919f8dc460cb163fd63e05b7eb Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Wed, 24 Jul 2019 10:44:38 -0700 Subject: [PATCH 4/6] Remove unused imports. --- translate/tests/unit/test_client.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/translate/tests/unit/test_client.py b/translate/tests/unit/test_client.py index 33c1bca5e986..dfbc5affaa73 100644 --- a/translate/tests/unit/test_client.py +++ b/translate/tests/unit/test_client.py @@ -63,7 +63,6 @@ def test_constructor_explicit(self): def test_constructor_w_empty_client_options(self): from google.cloud._http import ClientInfo from google.api_core.client_options import ClientOptions - from google.cloud.translate_v2._http import Connection http = object() target = "es" @@ -82,7 +81,6 @@ def test_constructor_w_empty_client_options(self): def test_constructor_w_client_options_object(self): from google.cloud._http import ClientInfo from google.api_core.client_options import ClientOptions - from google.cloud.translate_v2._http import Connection http = object() target = "es" From b80ea4b9a97b0b50ffd52430f77d818c7bb8e05c Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Mon, 29 Jul 2019 10:22:23 -0700 Subject: [PATCH 5/6] Set endpoint on instance variable. --- translate/google/cloud/translate_v2/_http.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/translate/google/cloud/translate_v2/_http.py b/translate/google/cloud/translate_v2/_http.py index 1db01164e772..d33647c757ab 100644 --- a/translate/google/cloud/translate_v2/_http.py +++ b/translate/google/cloud/translate_v2/_http.py @@ -32,9 +32,8 @@ class Connection(_http.JSONConnection): DEFAULT_API_ENDPOINT = "https://translation.googleapis.com" def __init__(self, client, client_info=None, api_endpoint=DEFAULT_API_ENDPOINT): - Connection.API_BASE_URL = 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__ From f5cb28cb311a03ef024bc53663f0eea688519573 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Mon, 29 Jul 2019 10:24:32 -0700 Subject: [PATCH 6/6] Update pin for google-cloud-core. --- translate/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/translate/setup.py b/translate/setup.py index 4d0e9c3921b3..5bacdb534155 100644 --- a/translate/setup.py +++ b/translate/setup.py @@ -30,7 +30,7 @@ release_status = "Development Status :: 5 - Production/Stable" dependencies = [ "google-api-core[grpc] >= 1.14.0, < 2.0.0dev", - "google-cloud-core >= 1.0.0, < 2.0dev", + "google-cloud-core >= 1.0.3, < 2.0dev", ] extras = {}