Exemplo n.º 1
0
    def test_get_supported_languages(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = translation_service_pb2.SupportedLanguages(
            **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 = translate_v3beta1.TranslationServiceClient()

        # Setup Request
        parent = client.location_path("[PROJECT]", "[LOCATION]")

        response = client.get_supported_languages(parent)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = translation_service_pb2.GetSupportedLanguagesRequest(
            parent=parent
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 2
0
    def get_supported_languages(
        self,
        parent=None,
        display_language_code=None,
        model=None,
        retry=google.api_core.gapic_v1.method.DEFAULT,
        timeout=google.api_core.gapic_v1.method.DEFAULT,
        metadata=None,
    ):
        """
        Returns a list of supported languages for translation.

        Example:
            >>> from google.cloud import translate_v3beta1
            >>>
            >>> client = translate_v3beta1.TranslationServiceClient()
            >>>
            >>> response = client.get_supported_languages()

        Args:
            parent (str): Required. Location to make a regional or global call.

                Format: ``projects/{project-id}/locations/{location-id}``.

                For global calls, use ``projects/{project-id}/locations/global``.

                Only models within the same region (have same location-id) can be used,
                otherwise an INVALID\_ARGUMENT (400) error is returned.
            display_language_code (str): Optional. The language to use to return localized, human readable names
                of supported languages. If missing, then display names are not returned
                in a response.
            model (str): Optional. Get supported languages of this model.

                The format depends on model type:

                -  AutoML Translation models:
                   ``projects/{project-id}/locations/{location-id}/models/{model-id}``

                -  General (built-in) models:
                   ``projects/{project-id}/locations/{location-id}/models/general/nmt``,
                   ``projects/{project-id}/locations/{location-id}/models/general/base``

                Returns languages supported by the specified model. If missing, we get
                supported languages of Google general base (PBMT) model.
            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.translate_v3beta1.types.SupportedLanguages` 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 "get_supported_languages" not in self._inner_api_calls:
            self._inner_api_calls[
                "get_supported_languages"] = google.api_core.gapic_v1.method.wrap_method(
                    self.transport.get_supported_languages,
                    default_retry=self.
                    _method_configs["GetSupportedLanguages"].retry,
                    default_timeout=self.
                    _method_configs["GetSupportedLanguages"].timeout,
                    client_info=self._client_info,
                )

        request = translation_service_pb2.GetSupportedLanguagesRequest(
            parent=parent,
            display_language_code=display_language_code,
            model=model)
        if metadata is None:
            metadata = []
        metadata = list(metadata)
        try:
            routing_header = [("parent", parent)]
        except AttributeError:
            pass
        else:
            routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
                routing_header)
            metadata.append(routing_metadata)

        return self._inner_api_calls["get_supported_languages"](
            request, retry=retry, timeout=timeout, metadata=metadata)
    def get_supported_languages(
        self,
        parent=None,
        display_language_code=None,
        model=None,
        retry=google.api_core.gapic_v1.method.DEFAULT,
        timeout=google.api_core.gapic_v1.method.DEFAULT,
        metadata=None,
    ):
        """
        Returns a list of supported languages for translation.

        Example:
            >>> from google.cloud import translate_v3beta1
            >>>
            >>> client = translate_v3beta1.TranslationServiceClient()
            >>>
            >>> response = client.get_supported_languages()

        Args:
            parent (str): Optional. Used for making regionalized calls.
                Format: projects/{project-id}/locations/{location-id}.
                For global calls, use projects/{project-id}/locations/global.
                If missing, the call is treated as a global call.

                Only custom model within the same location-id can be used.
                Otherwise 400 is returned.
            display_language_code (str): Optional. The language to use to return localized, human readable names
                of supported languages. If missing, default language is ENGLISH.
            model (str): Optional. Get supported languages of this model.
                The format depends on model type:
                1. Custom models:
                projects/{project-id}/locations/{location-id}/models/{model-id}.
                2. General (built-in) models:
                projects/{project-id}/locations/{location-id}/models/general/nmt
                projects/{project-id}/locations/{location-id}/models/general/base
                Returns languages supported by the specified model.
                If missing, we get supported languages of Google general NMT model.
            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.translate_v3beta1.types.SupportedLanguages` 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 "get_supported_languages" not in self._inner_api_calls:
            self._inner_api_calls[
                "get_supported_languages"] = google.api_core.gapic_v1.method.wrap_method(
                    self.transport.get_supported_languages,
                    default_retry=self.
                    _method_configs["GetSupportedLanguages"].retry,
                    default_timeout=self.
                    _method_configs["GetSupportedLanguages"].timeout,
                    client_info=self._client_info,
                )

        request = translation_service_pb2.GetSupportedLanguagesRequest(
            parent=parent,
            display_language_code=display_language_code,
            model=model)
        return self._inner_api_calls["get_supported_languages"](
            request, retry=retry, timeout=timeout, metadata=metadata)