def test_synthesize_speech(self): # Setup Expected Response audio_content = b'16' expected_response = {'audio_content': audio_content} expected_response = cloud_tts_pb2.SynthesizeSpeechResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch('google.api_core.grpc_helpers.create_channel') with patch as create_channel: create_channel.return_value = channel client = texttospeech_v1.TextToSpeechClient() # Setup Request input_ = {} voice = {} audio_config = {} response = client.synthesize_speech(input_, voice, audio_config) assert expected_response == response assert len(channel.requests) == 1 expected_request = cloud_tts_pb2.SynthesizeSpeechRequest( input=input_, voice=voice, audio_config=audio_config) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_synthesize_speech(self): # Setup Expected Response audio_content = b'16' expected_response = {'audio_content': audio_content} expected_response = cloud_tts_pb2.SynthesizeSpeechResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = texttospeech_v1.TextToSpeechClient(channel=channel) # Setup Request input_ = {} voice = {} audio_config = {} response = client.synthesize_speech(input_, voice, audio_config) assert expected_response == response assert len(channel.requests) == 1 expected_request = cloud_tts_pb2.SynthesizeSpeechRequest( input=input_, voice=voice, audio_config=audio_config) actual_request = channel.requests[0][1] assert expected_request == actual_request
def synthesize_speech( self, input_, voice, audio_config, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Synthesizes speech synchronously: receive results after all text input has been processed. Example: >>> from google.cloud import texttospeech_v1 >>> >>> client = texttospeech_v1.TextToSpeechClient() >>> >>> # TODO: Initialize `input_`: >>> input_ = {} >>> >>> # TODO: Initialize `voice`: >>> voice = {} >>> >>> # TODO: Initialize `audio_config`: >>> audio_config = {} >>> >>> response = client.synthesize_speech(input_, voice, audio_config) Args: input_ (Union[dict, ~google.cloud.texttospeech_v1.types.SynthesisInput]): Required. The Synthesizer requires either plain text or SSML as input. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.texttospeech_v1.types.SynthesisInput` voice (Union[dict, ~google.cloud.texttospeech_v1.types.VoiceSelectionParams]): Required. The desired voice of the synthesized audio. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.texttospeech_v1.types.VoiceSelectionParams` audio_config (Union[dict, ~google.cloud.texttospeech_v1.types.AudioConfig]): Required. The configuration of the synthesized audio. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.texttospeech_v1.types.AudioConfig` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.texttospeech_v1.types.SynthesizeSpeechResponse` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if "synthesize_speech" not in self._inner_api_calls: self._inner_api_calls[ "synthesize_speech"] = google.api_core.gapic_v1.method.wrap_method( self.transport.synthesize_speech, default_retry=self._method_configs["SynthesizeSpeech"]. retry, default_timeout=self._method_configs["SynthesizeSpeech"]. timeout, client_info=self._client_info, ) request = cloud_tts_pb2.SynthesizeSpeechRequest( input=input_, voice=voice, audio_config=audio_config) return self._inner_api_calls["synthesize_speech"](request, retry=retry, timeout=timeout, metadata=metadata)