Content-Length: 734815 | pFad | http://github.com/googleapis/google-cloud-python/commit/d5a9ca0c51f92d24fa9078b393fd7cf0bd728a6d

C6 Isolating speech Connection in _http module. · googleapis/google-cloud-python@d5a9ca0 · GitHub
Skip to content

Commit d5a9ca0

Browse files
committed
Isolating speech Connection in _http module.
1 parent 685269e commit d5a9ca0

File tree

5 files changed

+34
-40
lines changed

5 files changed

+34
-40
lines changed

packages/google-cloud-python-speech/google/cloud/speech/_gax.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class GAPICSpeechAPI(object):
4242
"""Manage calls through GAPIC wrappers to the Speech API."""
4343
def __init__(self, client=None):
4444
self._client = client
45-
credentials = self._client._connection.credentials
45+
credentials = self._client._credentials
4646
channel = make_secure_channel(
4747
credentials, DEFAULT_USER_AGENT,
4848
SpeechClient.SERVICE_ADDRESS)

packages/google-cloud-python-speech/google/cloud/speech/_http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class HTTPSpeechAPI(object):
5757
"""
5858
def __init__(self, client):
5959
self._client = client
60-
self._connection = client._connection
60+
self._connection = Connection(client)
6161

6262
def async_recognize(self, sample, language_code=None,
6363
max_alternatives=None, profanity_filter=None,

packages/google-cloud-python-speech/google/cloud/speech/client.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from google.cloud.environment_vars import DISABLE_GRPC
2121

2222
from google.cloud.speech._gax import GAPICSpeechAPI
23-
from google.cloud.speech._http import Connection
2423
from google.cloud.speech._http import HTTPSpeechAPI
2524
from google.cloud.speech.sample import Sample
2625

@@ -58,8 +57,6 @@ class Client(BaseClient):
5857

5958
def __init__(self, credentials=None, http=None, use_gax=None):
6059
super(Client, self).__init__(credentials=credentials, http=http)
61-
self._connection = Connection(self)
62-
6360
# Save on the actual client class whether we use GAX or not.
6461
if use_gax is None:
6562
self._use_gax = _USE_GAX

packages/google-cloud-python-speech/unit_tests/test__gax.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,16 @@ def test_constructor(self, mocked_stub, mocked_cls, mocked_channel):
4949
from google.cloud.speech import __version__
5050
from google.cloud.speech._gax import OPERATIONS_API_HOST
5151

52+
credentials = _make_credentials()
5253
mock_cnxn = mock.Mock(
53-
credentials=_make_credentials(),
54+
credentials=credentials,
5455
spec=['credentials'],
5556
)
56-
mock_client = mock.Mock(_connection=mock_cnxn, spec=['_connection'])
57+
mock_client = mock.Mock(
58+
_connection=mock_cnxn,
59+
_credentials=credentials,
60+
spec=['_connection', '_credentials'],
61+
)
5762

5863
speech_api = self._make_one(mock_client)
5964
self.assertIs(speech_api._client, mock_client)

packages/google-cloud-python-speech/unit_tests/test_client.py

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,11 @@ def _make_one(self, *args, **kw):
8585
return self._get_target_class()(*args, **kw)
8686

8787
def test_ctor(self):
88-
from google.cloud.speech._http import Connection
89-
9088
creds = _make_credentials()
9189
http = object()
9290
client = self._make_one(credentials=creds, http=http)
93-
self.assertIsInstance(client._connection, Connection)
94-
self.assertTrue(client._connection.credentials is creds)
95-
self.assertTrue(client._connection.http is http)
91+
self.assertTrue(client._credentials is creds)
92+
self.assertTrue(client._http is http)
9693

9794
def test_ctor_use_gax_preset(self):
9895
creds = _make_credentials()
@@ -153,7 +150,9 @@ def test_sync_recognize_content_with_optional_params_no_gax(self):
153150
}
154151
credentials = _make_credentials()
155152
client = self._make_one(credentials=credentials, use_gax=False)
156-
client._connection = _Connection(RETURNED)
153+
speech_api = client.speech_api
154+
connection = _Connection(RETURNED)
155+
speech_api._connection = connection
157156

158157
encoding = speech.Encoding.FLAC
159158

@@ -165,8 +164,8 @@ def test_sync_recognize_content_with_optional_params_no_gax(self):
165164
profanity_filter=True,
166165
speech_context=self.HINTS)
167166

168-
self.assertEqual(len(client._connection._requested), 1)
169-
req = client._connection._requested[0]
167+
self.assertEqual(len(connection._requested), 1)
168+
req = connection._requested[0]
170169
self.assertEqual(len(req), 3)
171170
self.assertEqual(req['data'], REQUEST)
172171
self.assertEqual(req['method'], 'POST')
@@ -199,7 +198,9 @@ def test_sync_recognize_source_uri_without_optional_params_no_gax(self):
199198
}
200199
credentials = _make_credentials()
201200
client = self._make_one(credentials=credentials, use_gax=False)
202-
client._connection = _Connection(RETURNED)
201+
speech_api = client.speech_api
202+
connection = _Connection(RETURNED)
203+
speech_api._connection = connection
203204

204205
encoding = speech.Encoding.FLAC
205206

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

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

211-
self.assertEqual(len(client._connection._requested), 1)
212-
req = client._connection._requested[0]
212+
self.assertEqual(len(connection._requested), 1)
213+
req = connection._requested[0]
213214
self.assertEqual(len(req), 3)
214215
self.assertEqual(req['data'], REQUEST)
215216
self.assertEqual(req['method'], 'POST')
@@ -231,7 +232,8 @@ def test_sync_recognize_with_empty_results_no_gax(self):
231232

232233
credentials = _make_credentials()
233234
client = self._make_one(credentials=credentials, use_gax=False)
234-
client._connection = _Connection(SYNC_RECOGNIZE_EMPTY_RESPONSE)
235+
speech_api = client.speech_api
236+
speech_api._connection = _Connection(SYNC_RECOGNIZE_EMPTY_RESPONSE)
235237

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

249251
credentials = _make_credentials()
250252
client = self._make_one(credentials=credentials, use_gax=True)
251-
client._connection = _Connection()
252-
client._connection.credentials = credentials
253+
client._credentials = credentials
253254

254255
channel_args = []
255256
channel_obj = object()
@@ -291,9 +292,7 @@ def test_sync_recognize_with_gax(self):
291292

292293
creds = _make_credentials()
293294
client = self._make_one(credentials=creds, use_gax=True)
294-
client._connection = _Connection()
295-
client._connection.credentials = creds
296-
client._speech_api = None
295+
client._credentials = creds
297296

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

354353
credentials = _make_credentials()
355-
client = self._make_one(credentials=credentials)
356-
client._connection = _Connection({})
354+
client = self._make_one(credentials=credentials, use_gax=True)
357355

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

371369
credentials = _make_credentials()
372370
client = self._make_one(credentials=credentials, use_gax=False)
373-
client._connection = _Connection(RETURNED)
371+
speech_api = client.speech_api
372+
speech_api._connection = _Connection(RETURNED)
374373

375374
sample = client.sample(source_uri=self.AUDIO_SOURCE_URI,
376375
encoding=speech.Encoding.LINEAR16,
@@ -393,8 +392,7 @@ def test_async_recognize_with_gax(self):
393392
credentials = _make_credentials()
394393
client = self._make_one(credentials=credentials,
395394
use_gax=True)
396-
client._connection = _Connection()
397-
client._connection.credentials = credentials
395+
client._credentials = credentials
398396

399397
channel_args = []
400398
channel_obj = object()
@@ -434,7 +432,6 @@ def test_streaming_depends_on_gax(self):
434432

435433
credentials = _make_credentials()
436434
client = self._make_one(credentials=credentials, use_gax=False)
437-
client.connection = _Connection()
438435
sample = client.sample(content=self.AUDIO_CONTENT,
439436
encoding=speech.Encoding.LINEAR16,
440437
sample_rate=self.SAMPLE_RATE)
@@ -453,8 +450,7 @@ def test_streaming_closed_stream(self):
453450
stream = BytesIO(b'Some audio data...')
454451
credentials = _make_credentials()
455452
client = self._make_one(credentials=credentials)
456-
client.connection = _Connection()
457-
client.connection.credentials = credentials
453+
client._credentials = credentials
458454

459455
channel_args = []
460456
channel_obj = object()
@@ -495,8 +491,7 @@ def test_stream_recognize_interim_results(self):
495491
stream = BytesIO(b'Some audio data...')
496492
credentials = _make_credentials()
497493
client = self._make_one(credentials=credentials)
498-
client.connection = _Connection()
499-
client.connection.credentials = credentials
494+
client._credentials = credentials
500495

501496
alternatives = [{
502497
'transcript': 'testing streaming 1 2 3',
@@ -576,8 +571,7 @@ def test_stream_recognize(self):
576571
stream = BytesIO(b'Some audio data...')
577572
credentials = _make_credentials()
578573
client = self._make_one(credentials=credentials)
579-
client.connection = _Connection()
580-
client.connection.credentials = credentials
574+
client._credentials = credentials
581575

582576
alternatives = [{
583577
'transcript': 'testing streaming 1 2 3',
@@ -633,8 +627,7 @@ def test_stream_recognize_no_results(self):
633627
stream = BytesIO(b'Some audio data...')
634628
credentials = _make_credentials()
635629
client = self._make_one(credentials=credentials)
636-
client.connection = _Connection()
637-
client.connection.credentials = credentials
630+
client._credentials = credentials
638631

639632
responses = [_make_streaming_response()]
640633

@@ -670,8 +663,7 @@ def test_speech_api_with_gax(self):
670663

671664
creds = _make_credentials()
672665
client = self._make_one(credentials=creds, use_gax=True)
673-
client._connection = _Connection()
674-
client._connection.credentials = creds
666+
client._credentials = creds
675667

676668
channel_args = []
677669
channel_obj = object()

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/googleapis/google-cloud-python/commit/d5a9ca0c51f92d24fa9078b393fd7cf0bd728a6d

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy