Skip to content

Commit

Permalink
Add client options to translate_v2. (#8737)
Browse files Browse the repository at this point in the history
  • Loading branch information
busunkim96 committed Jul 30, 2019
1 parent e0f543d commit ad886bb
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 9 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 packages/google-cloud-translate/setup.py
Original file line number Diff line number Diff line change
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 packages/google-cloud-translate/tests/unit/test__http.py
Original file line number Diff line number Diff line change
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 packages/google-cloud-translate/tests/unit/test_client.py
Original file line number Diff line number Diff line change
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

0 comments on commit ad886bb

Please sign in to comment.