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

Update translation #2731

Merged
merged 5 commits into from
Nov 14, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions translate/google/cloud/translate/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Client for interacting with the Google Cloud Translate API."""


import httplib2
import six

from google.cloud._helpers import _to_bytes
Expand Down Expand Up @@ -55,6 +56,12 @@ def __init__(self, target_language=ENGLISH_ISO_639, api_key=None,
credentials=None, http=None):
self.api_key = api_key
self.target_language = target_language

if api_key is not None:
# If API key auth is desired, make it so that no credentials

This comment was marked as spam.

# will be auto-detected by the base class constructor.
if http is None:
http = httplib2.Http()
super(Client, self).__init__(credentials=credentials, http=http)

This comment was marked as spam.

This comment was marked as spam.


def get_languages(self, target_language=None):
Expand Down
1 change: 1 addition & 0 deletions translate/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ envlist =
localdeps =
pip install --quiet --upgrade {toxinidir}/../core
deps =
mock
pytest
covercmd =
py.test --quiet \
Expand Down
20 changes: 18 additions & 2 deletions translate/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _get_target_class():
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_ctor(self):
def test_constructor(self):
from google.cloud.translate.connection import Connection
from google.cloud.translate.client import ENGLISH_ISO_639

Expand All @@ -39,7 +39,7 @@ def test_ctor(self):
self.assertIsNone(client.api_key)
self.assertEqual(client.target_language, ENGLISH_ISO_639)

def test_ctor_non_default(self):
def test_constructor_non_default(self):
from google.cloud.translate.connection import Connection

http = object()
Expand All @@ -52,6 +52,22 @@ def test_ctor_non_default(self):
self.assertEqual(self.KEY, client.api_key)
self.assertEqual(client.target_language, target)

def test_constructor_api_key_override(self):
import mock
from google.cloud.translate.connection import Connection

target = 'ru'
with mock.patch('httplib2.Http') as http_ctor:
client = self._make_one(
target_language=target, api_key=self.KEY)

http_ctor.assert_called_once_with()
self.assertIsInstance(client._connection, Connection)
self.assertIsNone(client._connection.credentials)
self.assertIs(client._connection.http, http_ctor.return_value)
self.assertEqual(self.KEY, client.api_key)
self.assertEqual(client.target_language, target)

def test_get_languages(self):
from google.cloud.translate.client import ENGLISH_ISO_639

Expand Down