Skip to content

Commit 3979e8b

Browse files
authored
Add client options to translate_v2. (#8737)
1 parent 3375c81 commit 3979e8b

File tree

5 files changed

+82
-9
lines changed

5 files changed

+82
-9
lines changed

translate/google/cloud/translate_v2/_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://translation.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://translation.googleapis.com"
39-
"""The base of the API call URL."""
40-
4140
API_VERSION = "v2"
4241
"""The version of the API, used in building the API call's URL."""
4342

translate/google/cloud/translate_v2/client.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import six
1919

20+
import google.api_core.client_options
2021
from google.cloud.client import Client as BaseClient
2122

2223
from google.cloud.translate_v2._http import Connection
@@ -61,6 +62,9 @@ class Client(BaseClient):
6162
requests. If ``None``, then default info will be used. Generally,
6263
you only need to set this if you're developing your own library
6364
or partner tool.
65+
:type client_options: :class:`~google.api_core.client_options.ClientOptions` or :class:`dict`
66+
:param client_options: (Optional) Client options used to set user options on the client.
67+
API Endpoint should be set through client_options.
6468
"""
6569

6670
SCOPE = ("https://www.googleapis.com/auth/cloud-platform",)
@@ -72,10 +76,22 @@ def __init__(
7276
credentials=None,
7377
_http=None,
7478
client_info=None,
79+
client_options=None,
7580
):
7681
self.target_language = target_language
7782
super(Client, self).__init__(credentials=credentials, _http=_http)
78-
self._connection = Connection(self, client_info=client_info)
83+
84+
kw_args = {"client_info": client_info}
85+
if client_options:
86+
if type(client_options) == dict:
87+
client_options = google.api_core.client_options.from_dict(
88+
client_options
89+
)
90+
if client_options.api_endpoint:
91+
api_endpoint = client_options.api_endpoint
92+
kw_args["api_endpoint"] = api_endpoint
93+
94+
self._connection = Connection(self, **kw_args)
7995

8096
def get_languages(self, target_language=None):
8197
"""Get list of supported languages for translation.

translate/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
release_status = "Development Status :: 5 - Production/Stable"
3131
dependencies = [
3232
"google-api-core[grpc] >= 1.14.0, < 2.0.0dev",
33-
"google-cloud-core >= 1.0.0, < 2.0dev",
33+
"google-cloud-core >= 1.0.3, < 2.0dev",
3434
]
3535
extras = {}
3636

translate/tests/unit/test__http.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,21 @@ def _make_one(self, *args, **kw):
3030
def test_build_api_url_no_extra_query_params(self):
3131
conn = self._make_one(object())
3232
URI = "/".join(
33-
[conn.API_BASE_URL, "language", "translate", conn.API_VERSION, "foo"]
33+
[
34+
conn.DEFAULT_API_ENDPOINT,
35+
"language",
36+
"translate",
37+
conn.API_VERSION,
38+
"foo",
39+
]
40+
)
41+
self.assertEqual(conn.build_api_url("/foo"), URI)
42+
43+
def test_build_api_url_w_custom_endpoint(self):
44+
custom_endpoint = "https://foo-translation.googleapis.com"
45+
conn = self._make_one(object(), api_endpoint=custom_endpoint)
46+
URI = "/".join(
47+
[custom_endpoint, "language", "translate", conn.API_VERSION, "foo"]
3448
)
3549
self.assertEqual(conn.build_api_url("/foo"), URI)
3650

translate/tests/unit/test_client.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,57 @@ def test_constructor_explicit(self):
4646
target = "es"
4747
client_info = ClientInfo()
4848
client = self._make_one(
49-
target_language=target, _http=http, client_info=client_info
49+
target_language=target,
50+
_http=http,
51+
client_info=client_info,
52+
client_options={"api_endpoint": "https://foo-translation.googleapis.com"},
5053
)
5154
self.assertIsInstance(client._connection, Connection)
5255
self.assertIsNone(client._connection.credentials)
5356
self.assertIs(client._connection.http, http)
5457
self.assertEqual(client.target_language, target)
5558
self.assertIs(client._connection._client_info, client_info)
59+
self.assertEqual(
60+
client._connection.API_BASE_URL, "https://foo-translation.googleapis.com"
61+
)
62+
63+
def test_constructor_w_empty_client_options(self):
64+
from google.cloud._http import ClientInfo
65+
from google.api_core.client_options import ClientOptions
66+
67+
http = object()
68+
target = "es"
69+
client_info = ClientInfo()
70+
client_options = ClientOptions()
71+
client = self._make_one(
72+
target_language=target,
73+
_http=http,
74+
client_info=client_info,
75+
client_options=client_options,
76+
)
77+
self.assertEqual(
78+
client._connection.API_BASE_URL, client._connection.DEFAULT_API_ENDPOINT
79+
)
80+
81+
def test_constructor_w_client_options_object(self):
82+
from google.cloud._http import ClientInfo
83+
from google.api_core.client_options import ClientOptions
84+
85+
http = object()
86+
target = "es"
87+
client_info = ClientInfo()
88+
client_options = ClientOptions(
89+
api_endpoint="https://foo-translation.googleapis.com"
90+
)
91+
client = self._make_one(
92+
target_language=target,
93+
_http=http,
94+
client_info=client_info,
95+
client_options=client_options,
96+
)
97+
self.assertEqual(
98+
client._connection.API_BASE_URL, "https://foo-translation.googleapis.com"
99+
)
56100

57101
def test_get_languages(self):
58102
from google.cloud.translate_v2.client import ENGLISH_ISO_639

0 commit comments

Comments
 (0)