GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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.

Sign up for GitHub

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

Merged
busunkim96 merged 6 commits into googleapis:master from busunkim96:translate-client-options
Jul 30, 2019

Uh oh!

There was an error while loading. Please reload this page.

Merged

Translate: Add client options to translate_v2. #8737

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
6 commits
Select commit Hold shift + click to select a range
19a3842
Add client options to translate_v2.
busunkim96 Jul 23, 2019
e10e552
Use translation.googleapis.com as default endpoint.
busunkim96 Jul 24, 2019
e634518
Raise coverage.
busunkim96 Jul 24, 2019
98a21ea
Remove unused imports.
busunkim96 Jul 24, 2019
b80ea4b
Set endpoint on instance variable.
busunkim96 Jul 29, 2019
f5cb28c
Update pin for google-cloud-core.
busunkim96 Jul 29, 2019
File filter
Filter by extension


Clear filters
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
Diff view

There are no files selected for viewing

9 changes: 4 additions & 5 deletions 9 translate/google/cloud/translate_v2/_http.py
Show comments
View file
Open in desktop
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
18 changes: 17 additions & 1 deletion 18 translate/google/cloud/translate_v2/client.py
Show comments
View file
Open in desktop
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 2 translate/setup.py
Show comments
View file
Open in desktop
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 16 translate/tests/unit/test__http.py
Show comments
View file
Open in desktop
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 46 translate/tests/unit/test_client.py
Show comments
View file
Open in desktop
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.