From 4ddbcec03a5b22ef82cf91449bd5e6850d78742f Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Tue, 6 Dec 2016 09:43:02 -0800 Subject: [PATCH] Removing support for API_KEY auth in translate. Fixes #2734. --- .../google/cloud/translate/client.py | 20 +------- .../unit_tests/test_client.py | 48 +++++-------------- 2 files changed, 12 insertions(+), 56 deletions(-) diff --git a/packages/google-cloud-translate/google/cloud/translate/client.py b/packages/google-cloud-translate/google/cloud/translate/client.py index 47c692833abf..ea5359007bb6 100644 --- a/packages/google-cloud-translate/google/cloud/translate/client.py +++ b/packages/google-cloud-translate/google/cloud/translate/client.py @@ -15,7 +15,6 @@ """Client for interacting with the Google Cloud Translate API.""" -import httplib2 import six from google.cloud._helpers import _to_bytes @@ -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 @@ -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): @@ -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: @@ -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( @@ -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) diff --git a/packages/google-cloud-translate/unit_tests/test_client.py b/packages/google-cloud-translate/unit_tests/test_client.py index 2e9a23d6995e..c4eeba7573e1 100644 --- a/packages/google-cloud-translate/unit_tests/test_client.py +++ b/packages/google-cloud-translate/unit_tests/test_client.py @@ -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 @@ -36,7 +34,6 @@ 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): @@ -44,34 +41,16 @@ def test_constructor_non_default(self): 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'}, @@ -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( @@ -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'}, @@ -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({}) @@ -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, @@ -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) @@ -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, @@ -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({}) @@ -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) @@ -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', @@ -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' @@ -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_),