Skip to content

Commit 4bff050

Browse files
authored
Merge pull request #2514 from daspecster/tighten-speech-async-encoding
Tighten Speech Asynchronous encoding support.
2 parents 00d9538 + 12d6eb7 commit 4bff050

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from google.cloud._helpers import _to_bytes
2020
from google.cloud import client as client_module
2121
from google.cloud.speech.connection import Connection
22+
from google.cloud.speech.encoding import Encoding
2223
from google.cloud.speech.operation import Operation
2324
from google.cloud.speech.sample import Sample
2425

@@ -89,6 +90,9 @@ def async_recognize(self, sample, language_code=None,
8990
:rtype: `~google.cloud.speech.operation.Operation`
9091
:returns: ``Operation`` for asynchronous request to Google Speech API.
9192
"""
93+
if sample.encoding is not Encoding.LINEAR16:
94+
raise ValueError('Only LINEAR16 encoding is supported by '
95+
'asynchronous speech requests.')
9296

9397
data = _build_request_data(sample, language_code, max_alternatives,
9498
profanity_filter, speech_context)

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ def test_sync_recognize_with_empty_results(self):
160160
sample_rate=self.SAMPLE_RATE)
161161
client.sync_recognize(sample)
162162

163+
def test_async_supported_encodings(self):
164+
from google.cloud.speech.encoding import Encoding
165+
from google.cloud.speech.sample import Sample
166+
167+
credentials = _Credentials()
168+
client = self._makeOne(credentials=credentials)
169+
client.connection = _Connection({})
170+
171+
sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
172+
encoding=Encoding.FLAC,
173+
sample_rate=self.SAMPLE_RATE)
174+
with self.assertRaises(ValueError):
175+
client.async_recognize(sample)
176+
163177
def test_async_recognize(self):
164178
from unit_tests._fixtures import ASYNC_RECOGNIZE_RESPONSE
165179
from google.cloud.speech.encoding import Encoding
@@ -172,7 +186,7 @@ def test_async_recognize(self):
172186
client.connection = _Connection(RETURNED)
173187

174188
sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
175-
encoding=Encoding.FLAC,
189+
encoding=Encoding.LINEAR16,
176190
sample_rate=self.SAMPLE_RATE)
177191
operation = client.async_recognize(sample)
178192
self.assertIsInstance(operation, Operation)

0 commit comments

Comments
 (0)