Skip to content

Commit

Permalink
Isolating speech Connection in _http module.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Feb 23, 2017
1 parent 1542e97 commit 9ebef22
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 40 deletions.
2 changes: 1 addition & 1 deletion speech/google/cloud/speech/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class GAPICSpeechAPI(object):
"""Manage calls through GAPIC wrappers to the Speech API."""
def __init__(self, client=None):
self._client = client
credentials = self._client._connection.credentials
credentials = self._client._credentials
channel = make_secure_channel(
credentials, DEFAULT_USER_AGENT,
SpeechClient.SERVICE_ADDRESS)
Expand Down
2 changes: 1 addition & 1 deletion speech/google/cloud/speech/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class HTTPSpeechAPI(object):
"""
def __init__(self, client):
self._client = client
self._connection = client._connection
self._connection = Connection(client)

def async_recognize(self, sample, language_code=None,
max_alternatives=None, profanity_filter=None,
Expand Down
3 changes: 0 additions & 3 deletions speech/google/cloud/speech/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from google.cloud.environment_vars import DISABLE_GRPC

from google.cloud.speech._gax import GAPICSpeechAPI
from google.cloud.speech._http import Connection
from google.cloud.speech._http import HTTPSpeechAPI
from google.cloud.speech.sample import Sample

Expand Down Expand Up @@ -58,8 +57,6 @@ class Client(BaseClient):

def __init__(self, credentials=None, http=None, use_gax=None):
super(Client, self).__init__(credentials=credentials, http=http)
self._connection = Connection(self)

# Save on the actual client class whether we use GAX or not.
if use_gax is None:
self._use_gax = _USE_GAX
Expand Down
9 changes: 7 additions & 2 deletions speech/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ def test_constructor(self, mocked_stub, mocked_cls, mocked_channel):
from google.cloud.speech import __version__
from google.cloud.speech._gax import OPERATIONS_API_HOST

credentials = _make_credentials()
mock_cnxn = mock.Mock(
credentials=_make_credentials(),
credentials=credentials,
spec=['credentials'],
)
mock_client = mock.Mock(_connection=mock_cnxn, spec=['_connection'])
mock_client = mock.Mock(
_connection=mock_cnxn,
_credentials=credentials,
spec=['_connection', '_credentials'],
)

speech_api = self._make_one(mock_client)
self.assertIs(speech_api._client, mock_client)
Expand Down
58 changes: 25 additions & 33 deletions speech/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,11 @@ def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_ctor(self):
from google.cloud.speech._http import Connection

creds = _make_credentials()
http = object()
client = self._make_one(credentials=creds, http=http)
self.assertIsInstance(client._connection, Connection)
self.assertTrue(client._connection.credentials is creds)
self.assertTrue(client._connection.http is http)
self.assertTrue(client._credentials is creds)
self.assertTrue(client._http is http)

def test_ctor_use_gax_preset(self):
creds = _make_credentials()
Expand Down Expand Up @@ -153,7 +150,9 @@ def test_sync_recognize_content_with_optional_params_no_gax(self):
}
credentials = _make_credentials()
client = self._make_one(credentials=credentials, use_gax=False)
client._connection = _Connection(RETURNED)
speech_api = client.speech_api
connection = _Connection(RETURNED)
speech_api._connection = connection

encoding = speech.Encoding.FLAC

Expand All @@ -165,8 +164,8 @@ def test_sync_recognize_content_with_optional_params_no_gax(self):
profanity_filter=True,
speech_context=self.HINTS)

self.assertEqual(len(client._connection._requested), 1)
req = client._connection._requested[0]
self.assertEqual(len(connection._requested), 1)
req = connection._requested[0]
self.assertEqual(len(req), 3)
self.assertEqual(req['data'], REQUEST)
self.assertEqual(req['method'], 'POST')
Expand Down Expand Up @@ -199,7 +198,9 @@ def test_sync_recognize_source_uri_without_optional_params_no_gax(self):
}
credentials = _make_credentials()
client = self._make_one(credentials=credentials, use_gax=False)
client._connection = _Connection(RETURNED)
speech_api = client.speech_api
connection = _Connection(RETURNED)
speech_api._connection = connection

encoding = speech.Encoding.FLAC

Expand All @@ -208,8 +209,8 @@ def test_sync_recognize_source_uri_without_optional_params_no_gax(self):

response = [i for i in sample.sync_recognize()]

self.assertEqual(len(client._connection._requested), 1)
req = client._connection._requested[0]
self.assertEqual(len(connection._requested), 1)
req = connection._requested[0]
self.assertEqual(len(req), 3)
self.assertEqual(req['data'], REQUEST)
self.assertEqual(req['method'], 'POST')
Expand All @@ -231,7 +232,8 @@ def test_sync_recognize_with_empty_results_no_gax(self):

credentials = _make_credentials()
client = self._make_one(credentials=credentials, use_gax=False)
client._connection = _Connection(SYNC_RECOGNIZE_EMPTY_RESPONSE)
speech_api = client.speech_api
speech_api._connection = _Connection(SYNC_RECOGNIZE_EMPTY_RESPONSE)

sample = client.sample(source_uri=self.AUDIO_SOURCE_URI,
encoding=speech.Encoding.FLAC,
Expand All @@ -248,8 +250,7 @@ def test_sync_recognize_with_empty_results_gax(self):

credentials = _make_credentials()
client = self._make_one(credentials=credentials, use_gax=True)
client._connection = _Connection()
client._connection.credentials = credentials
client._credentials = credentials

channel_args = []
channel_obj = object()
Expand Down Expand Up @@ -291,9 +292,7 @@ def test_sync_recognize_with_gax(self):

creds = _make_credentials()
client = self._make_one(credentials=creds, use_gax=True)
client._connection = _Connection()
client._connection.credentials = creds
client._speech_api = None
client._credentials = creds

alternatives = [{
'transcript': 'testing 1 2 3',
Expand Down Expand Up @@ -352,8 +351,7 @@ def test_async_supported_encodings(self):
from google.cloud import speech

credentials = _make_credentials()
client = self._make_one(credentials=credentials)
client._connection = _Connection({})
client = self._make_one(credentials=credentials, use_gax=True)

sample = client.sample(source_uri=self.AUDIO_SOURCE_URI,
encoding=speech.Encoding.FLAC,
Expand All @@ -370,7 +368,8 @@ def test_async_recognize_no_gax(self):

credentials = _make_credentials()
client = self._make_one(credentials=credentials, use_gax=False)
client._connection = _Connection(RETURNED)
speech_api = client.speech_api
speech_api._connection = _Connection(RETURNED)

sample = client.sample(source_uri=self.AUDIO_SOURCE_URI,
encoding=speech.Encoding.LINEAR16,
Expand All @@ -393,8 +392,7 @@ def test_async_recognize_with_gax(self):
credentials = _make_credentials()
client = self._make_one(credentials=credentials,
use_gax=True)
client._connection = _Connection()
client._connection.credentials = credentials
client._credentials = credentials

channel_args = []
channel_obj = object()
Expand Down Expand Up @@ -434,7 +432,6 @@ def test_streaming_depends_on_gax(self):

credentials = _make_credentials()
client = self._make_one(credentials=credentials, use_gax=False)
client.connection = _Connection()
sample = client.sample(content=self.AUDIO_CONTENT,
encoding=speech.Encoding.LINEAR16,
sample_rate=self.SAMPLE_RATE)
Expand All @@ -453,8 +450,7 @@ def test_streaming_closed_stream(self):
stream = BytesIO(b'Some audio data...')
credentials = _make_credentials()
client = self._make_one(credentials=credentials)
client.connection = _Connection()
client.connection.credentials = credentials
client._credentials = credentials

channel_args = []
channel_obj = object()
Expand Down Expand Up @@ -495,8 +491,7 @@ def test_stream_recognize_interim_results(self):
stream = BytesIO(b'Some audio data...')
credentials = _make_credentials()
client = self._make_one(credentials=credentials)
client.connection = _Connection()
client.connection.credentials = credentials
client._credentials = credentials

alternatives = [{
'transcript': 'testing streaming 1 2 3',
Expand Down Expand Up @@ -576,8 +571,7 @@ def test_stream_recognize(self):
stream = BytesIO(b'Some audio data...')
credentials = _make_credentials()
client = self._make_one(credentials=credentials)
client.connection = _Connection()
client.connection.credentials = credentials
client._credentials = credentials

alternatives = [{
'transcript': 'testing streaming 1 2 3',
Expand Down Expand Up @@ -633,8 +627,7 @@ def test_stream_recognize_no_results(self):
stream = BytesIO(b'Some audio data...')
credentials = _make_credentials()
client = self._make_one(credentials=credentials)
client.connection = _Connection()
client.connection.credentials = credentials
client._credentials = credentials

responses = [_make_streaming_response()]

Expand Down Expand Up @@ -670,8 +663,7 @@ def test_speech_api_with_gax(self):

creds = _make_credentials()
client = self._make_one(credentials=creds, use_gax=True)
client._connection = _Connection()
client._connection.credentials = creds
client._credentials = creds

channel_args = []
channel_obj = object()
Expand Down

0 comments on commit 9ebef22

Please sign in to comment.