コード例 #1
0
    def test_detect_language(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = translation_service_pb2.DetectLanguageResponse(
            **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()

        response = client.detect_language()
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = translation_service_pb2.DetectLanguageRequest()
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
コード例 #2
0
    def detect_language(
        self,
        parent=None,
        model=None,
        content=None,
        mime_type=None,
        retry=google.api_core.gapic_v1.method.DEFAULT,
        timeout=google.api_core.gapic_v1.method.DEFAULT,
        metadata=None,
    ):
        """
        Detects the language of text within a request.

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

        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 (has same location-id) can be used.
                Otherwise an INVALID\_ARGUMENT (400) error is returned.
            model (str): Optional. The language detection model to be used.

                Format:
                ``projects/{project-id}/locations/{location-id}/models/language-detection/{model-id}``

                Only one language detection model is currently supported:
                ``projects/{project-id}/locations/{location-id}/models/language-detection/default``.

                If not specified, the default model is used.
            content (str): The content of the input stored as a string.
            mime_type (str): Optional. The format of the source text, for example, "text/html",
                "text/plain". If left blank, the MIME type defaults to "text/html".
            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.DetectLanguageResponse` 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 "detect_language" not in self._inner_api_calls:
            self._inner_api_calls[
                "detect_language"] = google.api_core.gapic_v1.method.wrap_method(
                    self.transport.detect_language,
                    default_retry=self._method_configs["DetectLanguage"].retry,
                    default_timeout=self._method_configs["DetectLanguage"].
                    timeout,
                    client_info=self._client_info,
                )

        # Sanity check: We have some fields which are mutually exclusive;
        # raise ValueError if more than one is sent.
        google.api_core.protobuf_helpers.check_oneof(content=content)

        request = translation_service_pb2.DetectLanguageRequest(
            parent=parent, model=model, content=content, mime_type=mime_type)
        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["detect_language"](request,
                                                        retry=retry,
                                                        timeout=timeout,
                                                        metadata=metadata)