Skip to content

Commit

Permalink
Removing support for API_KEY auth in translate.
Browse files Browse the repository at this point in the history
Fixes #2734.
  • Loading branch information
dhermes committed Dec 6, 2016
1 parent 49eeecf commit 4ddbcec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 56 deletions.
20 changes: 1 addition & 19 deletions packages/google-cloud-translate/google/cloud/translate/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"""Client for interacting with the Google Cloud Translate API."""


import httplib2
import six

from google.cloud._helpers import _to_bytes
Expand All @@ -41,10 +40,6 @@ class Client(BaseClient):
translations and language names. (Defaults to
:data:`ENGLISH_ISO_639`.)
:type api_key: str
:param api_key: (Optional) The key used to send with requests as a
query parameter.
:type credentials: :class:`oauth2client.client.OAuth2Credentials`
:param credentials: (Optional) The OAuth2 Credentials to use for the
connection owned by this client. If not passed (and
Expand All @@ -58,16 +53,9 @@ class Client(BaseClient):

_connection_class = Connection

def __init__(self, target_language=ENGLISH_ISO_639, api_key=None,
def __init__(self, target_language=ENGLISH_ISO_639,
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
# will be auto-detected by the base class constructor.
if http is None:
http = httplib2.Http()
super(Client, self).__init__(credentials=credentials, http=http)

def get_languages(self, target_language=None):
Expand All @@ -91,8 +79,6 @@ def get_languages(self, target_language=None):
language (localized to the target language).
"""
query_params = {}
if self.api_key is not None:
query_params['key'] = self.api_key
if target_language is None:
target_language = self.target_language
if target_language is not None:
Expand Down Expand Up @@ -137,8 +123,6 @@ def detect_language(self, values):
values = [values]

query_params = []
if self.api_key is not None:
query_params.append(('key', self.api_key))
query_params.extend(('q', _to_bytes(value, 'utf-8'))
for value in values)
response = self._connection.api_request(
Expand Down Expand Up @@ -230,8 +214,6 @@ def translate(self, values, target_language=None, format_=None,
customization_ids = [customization_ids]

query_params = [('target', target_language)]
if self.api_key is not None:
query_params.append(('key', self.api_key))
query_params.extend(('q', _to_bytes(value, 'utf-8'))
for value in values)
query_params.extend(('cid', cid) for cid in customization_ids)
Expand Down
48 changes: 11 additions & 37 deletions packages/google-cloud-translate/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

class TestClient(unittest.TestCase):

KEY = 'abc-123-my-key'

@staticmethod
def _get_target_class():
from google.cloud.translate.client import Client
Expand All @@ -36,42 +34,23 @@ def test_constructor(self):
self.assertIsInstance(client._connection, Connection)
self.assertIsNone(client._connection.credentials)
self.assertIs(client._connection.http, http)
self.assertIsNone(client.api_key)
self.assertEqual(client.target_language, ENGLISH_ISO_639)

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

http = object()
target = 'es'
client = self._make_one(
target_language=target, api_key=self.KEY, http=http)
client = self._make_one(target_language=target, http=http)
self.assertIsInstance(client._connection, Connection)
self.assertIsNone(client._connection.credentials)
self.assertIs(client._connection.http, http)
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

client = self._make_one(api_key=self.KEY, http=object())
client = self._make_one(http=object())
supported = [
{'language': 'en', 'name': 'English'},
{'language': 'af', 'name': 'Afrikaans'},
Expand All @@ -93,7 +72,7 @@ def test_get_languages(self):
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/languages')
self.assertEqual(req['query_params'],
{'key': self.KEY, 'target': ENGLISH_ISO_639})
{'target': ENGLISH_ISO_639})

def test_get_languages_no_target(self):
client = self._make_one(
Expand Down Expand Up @@ -122,7 +101,7 @@ def test_get_languages_no_target(self):
self.assertEqual(req['query_params'], {})

def test_get_languages_explicit_target(self):
client = self._make_one(api_key=self.KEY, http=object())
client = self._make_one(http=object())
target_language = 'en'
supported = [
{'language': 'en', 'name': 'Spanish'},
Expand All @@ -145,10 +124,10 @@ def test_get_languages_explicit_target(self):
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/languages')
self.assertEqual(req['query_params'],
{'key': self.KEY, 'target': target_language})
{'target': target_language})

def test_detect_language_bad_result(self):
client = self._make_one(api_key=self.KEY, http=object())
client = self._make_one(http=object())
value = 'takoy'
conn = client._connection = _Connection({})

Expand All @@ -161,13 +140,12 @@ def test_detect_language_bad_result(self):
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/detect')
query_params = [
('key', self.KEY),
('q', value.encode('utf-8')),
]
self.assertEqual(req['query_params'], query_params)

def test_detect_language_single_value(self):
client = self._make_one(api_key=self.KEY, http=object())
client = self._make_one(http=object())
value = 'takoy'
detection = {
'confidence': 1.0,
Expand All @@ -191,7 +169,6 @@ def test_detect_language_single_value(self):
self.assertEqual(req['method'], 'GET')
self.assertEqual(req['path'], '/detect')
query_params = [
('key', self.KEY),
('q', value.encode('utf-8')),
]
self.assertEqual(req['query_params'], query_params)
Expand Down Expand Up @@ -237,7 +214,7 @@ def test_detect_language_multiple_values(self):
self.assertEqual(req['query_params'], query_params)

def test_detect_language_multiple_results(self):
client = self._make_one(api_key=self.KEY, http=object())
client = self._make_one(http=object())
value = 'soy'
detection1 = {
'confidence': 0.81496066,
Expand All @@ -262,7 +239,7 @@ def test_detect_language_multiple_results(self):
client.detect_language(value)

def test_translate_bad_result(self):
client = self._make_one(api_key=self.KEY, http=object())
client = self._make_one(http=object())
value = 'hvala ti'
conn = client._connection = _Connection({})

Expand All @@ -276,7 +253,6 @@ def test_translate_bad_result(self):
self.assertEqual(req['path'], '')
query_params = [
('target', 'en'),
('key', self.KEY),
('q', value.encode('utf-8')),
]
self.assertEqual(req['query_params'], query_params)
Expand Down Expand Up @@ -311,7 +287,7 @@ def test_translate_defaults(self):
self.assertEqual(req['query_params'], query_params)

def test_translate_multiple(self):
client = self._make_one(api_key=self.KEY, http=object())
client = self._make_one(http=object())
value1 = 'hvala ti'
translation1 = {
'detectedSourceLanguage': 'hr',
Expand Down Expand Up @@ -341,14 +317,13 @@ def test_translate_multiple(self):
self.assertEqual(req['path'], '')
query_params = [
('target', 'en'),
('key', self.KEY),
('q', value1.encode('utf-8')),
('q', value2.encode('utf-8')),
]
self.assertEqual(req['query_params'], query_params)

def test_translate_explicit(self):
client = self._make_one(api_key=self.KEY, http=object())
client = self._make_one(http=object())
value = 'thank you'
target_language = 'eo'
source_language = 'en'
Expand Down Expand Up @@ -379,7 +354,6 @@ def test_translate_explicit(self):
self.assertEqual(req['path'], '')
query_params = [
('target', target_language),
('key', self.KEY),
('q', value.encode('utf-8')),
('cid', cid),
('format', format_),
Expand Down

0 comments on commit 4ddbcec

Please sign in to comment.