GeezerVison is showing github.com — Translate: Add client options to translate_v2. by busunkim96 · Pull Request #8737 · googleapis/google-cloud-python
Translate: Add client options to translate_v2.
#8737
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
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.py
(5)
All 1 file type selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
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
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.