diff --git a/translate/google/cloud/translate_v2/_http.py b/translate/google/cloud/translate_v2/_http.py index b73f5f595450..d33647c757ab 100644 --- a/translate/google/cloud/translate_v2/_http.py +++ b/translate/google/cloud/translate_v2/_http.py @@ -29,15 +29,14 @@ class Connection(_http.JSONConnection): :param client_info: (Optional) instance used to generate user agent. """ - def __init__(self, client, client_info=None): - super(Connection, self).__init__(client, client_info) + DEFAULT_API_ENDPOINT = "https://translation.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://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..6c561675d25e 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,22 @@ 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) + + 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 get_languages(self, target_language=None): """Get list of supported languages for translation. 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 = {} diff --git a/translate/tests/unit/test__http.py b/translate/tests/unit/test__http.py index d179ce330653..208f207c8380 100644 --- a/translate/tests/unit/test__http.py +++ b/translate/tests/unit/test__http.py @@ -30,7 +30,21 @@ 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) + + 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) diff --git a/translate/tests/unit/test_client.py b/translate/tests/unit/test_client.py index e014657bc882..dfbc5affaa73 100644 --- a/translate/tests/unit/test_client.py +++ b/translate/tests/unit/test_client.py @@ -46,13 +46,57 @@ 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_constructor_w_empty_client_options(self): + from google.cloud._http import ClientInfo + from google.api_core.client_options import ClientOptions + + 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 + + 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