Skip to content

Commit

Permalink
Merge pull request #2703 from dhermes/connection-non-public
Browse files Browse the repository at this point in the history
Making base connection module non-public and making connection attribute non-public
  • Loading branch information
dhermes authored Nov 11, 2016
2 parents b39c0ba + 0bc8caf commit edd9178
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self, api_key, http=None, target_language=ENGLISH_ISO_639):
self.api_key = api_key
if http is None:
http = httplib2.Http()
self.connection = Connection(http=http)
self._connection = Connection(http=http)
self.target_language = target_language

def get_languages(self, target_language=None):
Expand Down Expand Up @@ -75,7 +75,7 @@ def get_languages(self, target_language=None):
target_language = self.target_language
if target_language is not None:
query_params['target'] = target_language
response = self.connection.api_request(
response = self._connection.api_request(
method='GET', path='/languages', query_params=query_params)
return response.get('data', {}).get('languages', ())

Expand Down Expand Up @@ -117,7 +117,7 @@ def detect_language(self, values):
query_params = [('key', self.api_key)]
query_params.extend(('q', _to_bytes(value, 'utf-8'))
for value in values)
response = self.connection.api_request(
response = self._connection.api_request(
method='GET', path='/detect', query_params=query_params)
detections = response.get('data', {}).get('detections', ())

Expand Down Expand Up @@ -208,7 +208,7 @@ def translate(self, values, target_language=None, format_=None,
if source_language is not None:
query_params.append(('source', source_language))

response = self.connection.api_request(
response = self._connection.api_request(
method='GET', path='', query_params=query_params)

translations = response.get('data', {}).get('translations', ())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

"""Create / interact with Google Cloud Translate connections."""

from google.cloud import connection as base_connection
from google.cloud import _http


class Connection(base_connection.JSONConnection):
class Connection(_http.JSONConnection):
"""A connection to Google Cloud Translate via the JSON REST API."""

API_BASE_URL = 'https://www.googleapis.com'
Expand Down
34 changes: 17 additions & 17 deletions packages/google-cloud-translate/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ def test_ctor(self):

http = object()
client = self._make_one(self.KEY, http=http)
self.assertIsInstance(client.connection, Connection)
self.assertIsNone(client.connection.credentials)
self.assertIs(client.connection.http, http)
self.assertIsInstance(client._connection, Connection)
self.assertIsNone(client._connection.credentials)
self.assertIs(client._connection.http, http)
self.assertEqual(client.target_language, ENGLISH_ISO_639)

def test_ctor_non_default(self):
Expand All @@ -44,9 +44,9 @@ def test_ctor_non_default(self):
http = object()
target = 'es'
client = self._make_one(self.KEY, http=http, target_language=target)
self.assertIsInstance(client.connection, Connection)
self.assertIsNone(client.connection.credentials)
self.assertIs(client.connection.http, http)
self.assertIsInstance(client._connection, Connection)
self.assertIsNone(client._connection.credentials)
self.assertIs(client._connection.http, http)
self.assertEqual(client.target_language, target)

def test_get_languages(self):
Expand All @@ -63,7 +63,7 @@ def test_get_languages(self):
'languages': supported,
},
}
conn = client.connection = _Connection(data)
conn = client._connection = _Connection(data)

result = client.get_languages()
self.assertEqual(result, supported)
Expand All @@ -88,7 +88,7 @@ def test_get_languages_no_target(self):
'languages': supported,
},
}
conn = client.connection = _Connection(data)
conn = client._connection = _Connection(data)

result = client.get_languages()
self.assertEqual(result, supported)
Expand All @@ -113,7 +113,7 @@ def test_get_languages_explicit_target(self):
'languages': supported,
},
}
conn = client.connection = _Connection(data)
conn = client._connection = _Connection(data)

result = client.get_languages(target_language)
self.assertEqual(result, supported)
Expand All @@ -129,7 +129,7 @@ def test_get_languages_explicit_target(self):
def test_detect_language_bad_result(self):
client = self._make_one(self.KEY)
value = 'takoy'
conn = client.connection = _Connection({})
conn = client._connection = _Connection({})

with self.assertRaises(ValueError):
client.detect_language(value)
Expand Down Expand Up @@ -159,7 +159,7 @@ def test_detect_language_single_value(self):
'detections': [[detection]],
},
}
conn = client.connection = _Connection(data)
conn = client._connection = _Connection(data)

result = client.detect_language(value)
self.assertEqual(result, detection)
Expand Down Expand Up @@ -199,7 +199,7 @@ def test_detect_language_multiple_values(self):
],
},
}
conn = client.connection = _Connection(data)
conn = client._connection = _Connection(data)

result = client.detect_language([value1, value2])
self.assertEqual(result, [detection1, detection2])
Expand Down Expand Up @@ -236,15 +236,15 @@ def test_detect_language_multiple_results(self):
'detections': [[detection1, detection2]],
},
}
client.connection = _Connection(data)
client._connection = _Connection(data)

with self.assertRaises(ValueError):
client.detect_language(value)

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

with self.assertRaises(ValueError):
client.translate(value)
Expand Down Expand Up @@ -274,7 +274,7 @@ def test_translate_defaults(self):
'translations': [translation],
},
}
conn = client.connection = _Connection(data)
conn = client._connection = _Connection(data)

result = client.translate(value)
self.assertEqual(result, translation)
Expand Down Expand Up @@ -310,7 +310,7 @@ def test_translate_multiple(self):
'translations': [translation1, translation2],
},
}
conn = client.connection = _Connection(data)
conn = client._connection = _Connection(data)

result = client.translate([value1, value2])
self.assertEqual(result, [translation1, translation2])
Expand Down Expand Up @@ -342,7 +342,7 @@ def test_translate_explicit(self):
'translations': [translation],
},
}
conn = client.connection = _Connection(data)
conn = client._connection = _Connection(data)

cid = '123'
format_ = 'text'
Expand Down

0 comments on commit edd9178

Please sign in to comment.