async def test_synthesize_speech_async( transport: str = "grpc_asyncio", request_type=cloud_tts.SynthesizeSpeechRequest): client = TextToSpeechAsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client.transport.synthesize_speech), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( cloud_tts.SynthesizeSpeechResponse( audio_content=b"audio_content_blob", )) response = await client.synthesize_speech(request) # Establish that the underlying gRPC stub method was called. assert len(call.mock_calls) _, args, _ = call.mock_calls[0] assert args[0] == cloud_tts.SynthesizeSpeechRequest() # Establish that the response is the type that we expect. assert isinstance(response, cloud_tts.SynthesizeSpeechResponse) assert response.audio_content == b"audio_content_blob"
def test_synthesize_speech_flattened_error(): client = TextToSpeechClient(credentials=credentials.AnonymousCredentials()) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): client.synthesize_speech( cloud_tts.SynthesizeSpeechRequest(), input=cloud_tts.SynthesisInput(text="text_value"), voice=cloud_tts.VoiceSelectionParams( language_code="language_code_value"), audio_config=cloud_tts.AudioConfig( audio_encoding=cloud_tts.AudioEncoding.LINEAR16), )
def test_synthesize_speech_empty_call(): # This test is a coverage failsafe to make sure that totally empty calls, # i.e. request == None and no flattened fields passed, work. client = TextToSpeechClient( credentials=credentials.AnonymousCredentials(), transport="grpc", ) # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client.transport.synthesize_speech), "__call__") as call: client.synthesize_speech() call.assert_called() _, args, _ = call.mock_calls[0] assert args[0] == cloud_tts.SynthesizeSpeechRequest()
async def synthesize_speech( self, request: cloud_tts.SynthesizeSpeechRequest = None, *, input: cloud_tts.SynthesisInput = None, voice: cloud_tts.VoiceSelectionParams = None, audio_config: cloud_tts.AudioConfig = None, retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), ) -> cloud_tts.SynthesizeSpeechResponse: r"""Synthesizes speech synchronously: receive results after all text input has been processed. Args: request (:class:`~.cloud_tts.SynthesizeSpeechRequest`): The request object. The top-level message sent by the client for the `SynthesizeSpeech` method. input (:class:`~.cloud_tts.SynthesisInput`): Required. The Synthesizer requires either plain text or SSML as input. This corresponds to the ``input`` field on the ``request`` instance; if ``request`` is provided, this should not be set. voice (:class:`~.cloud_tts.VoiceSelectionParams`): Required. The desired voice of the synthesized audio. This corresponds to the ``voice`` field on the ``request`` instance; if ``request`` is provided, this should not be set. audio_config (:class:`~.cloud_tts.AudioConfig`): Required. The configuration of the synthesized audio. This corresponds to the ``audio_config`` field on the ``request`` instance; if ``request`` is provided, this should not be set. retry (google.api_core.retry.Retry): Designation of what errors, if any, should be retried. timeout (float): The timeout for this request. metadata (Sequence[Tuple[str, str]]): Strings which should be sent along with the request as metadata. Returns: ~.cloud_tts.SynthesizeSpeechResponse: The message returned to the client by the ``SynthesizeSpeech`` method. """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. if request is not None and any([input, voice, audio_config]): raise ValueError("If the `request` argument is set, then none of " "the individual field arguments should be set.") request = cloud_tts.SynthesizeSpeechRequest(request) # If we have keyword arguments corresponding to fields on the # request, apply these. if input is not None: request.input = input if voice is not None: request.voice = voice if audio_config is not None: request.audio_config = audio_config # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. rpc = gapic_v1.method_async.wrap_method( self._client._transport.synthesize_speech, default_timeout=None, client_info=_client_info, ) # Send the request. response = await rpc( request, retry=retry, timeout=timeout, metadata=metadata, ) # Done; return the response. return response
def synthesize_speech( self, request: cloud_tts.SynthesizeSpeechRequest = None, *, input: cloud_tts.SynthesisInput = None, voice: cloud_tts.VoiceSelectionParams = None, audio_config: cloud_tts.AudioConfig = None, retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), ) -> cloud_tts.SynthesizeSpeechResponse: r"""Synthesizes speech synchronously: receive results after all text input has been processed. Args: request (google.cloud.texttospeech_v1beta1.types.SynthesizeSpeechRequest): The request object. The top-level message sent by the client for the `SynthesizeSpeech` method. input (google.cloud.texttospeech_v1beta1.types.SynthesisInput): Required. The Synthesizer requires either plain text or SSML as input. This corresponds to the ``input`` field on the ``request`` instance; if ``request`` is provided, this should not be set. voice (google.cloud.texttospeech_v1beta1.types.VoiceSelectionParams): Required. The desired voice of the synthesized audio. This corresponds to the ``voice`` field on the ``request`` instance; if ``request`` is provided, this should not be set. audio_config (google.cloud.texttospeech_v1beta1.types.AudioConfig): Required. The configuration of the synthesized audio. This corresponds to the ``audio_config`` field on the ``request`` instance; if ``request`` is provided, this should not be set. retry (google.api_core.retry.Retry): Designation of what errors, if any, should be retried. timeout (float): The timeout for this request. metadata (Sequence[Tuple[str, str]]): Strings which should be sent along with the request as metadata. Returns: google.cloud.texttospeech_v1beta1.types.SynthesizeSpeechResponse: The message returned to the client by the SynthesizeSpeech method. """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. has_flattened_params = any([input, voice, audio_config]) if request is not None and has_flattened_params: raise ValueError( "If the `request` argument is set, then none of " "the individual field arguments should be set." ) # Minor optimization to avoid making a copy if the user passes # in a cloud_tts.SynthesizeSpeechRequest. # There's no risk of modifying the input as we've already verified # there are no flattened fields. if not isinstance(request, cloud_tts.SynthesizeSpeechRequest): request = cloud_tts.SynthesizeSpeechRequest(request) # If we have keyword arguments corresponding to fields on the # request, apply these. if input is not None: request.input = input if voice is not None: request.voice = voice if audio_config is not None: request.audio_config = audio_config # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. rpc = self._transport._wrapped_methods[self._transport.synthesize_speech] # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) # Done; return the response. return response