Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate: Add client options to translate_v2. #8737

Merged
merged 6 commits into from Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions translate/google/cloud/translate_v2/_http.py
Expand Up @@ -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."""

Expand Down
18 changes: 17 additions & 1 deletion translate/google/cloud/translate_v2/client.py
Expand Up @@ -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
Expand Down Expand Up @@ -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",)
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion translate/setup.py
Expand Up @@ -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 = {}

Expand Down
16 changes: 15 additions & 1 deletion translate/tests/unit/test__http.py
Expand Up @@ -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)

Expand Down
46 changes: 45 additions & 1 deletion translate/tests/unit/test_client.py
Expand Up @@ -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
Expand Down