# This file was auto-generated by Fern from our API Definition.

import typing
from json.decoder import JSONDecodeError

from ...core.api_error import ApiError
from ...core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ...core.http_response import AsyncHttpResponse, HttpResponse
from ...core.request_options import RequestOptions
from ...core.unchecked_base_model import construct_type
from ..errors.bad_request_error import BadRequestError
from ..errors.content_too_large_error import ContentTooLargeError
from ..errors.internal_server_error import InternalServerError
from ..errors.too_many_requests_error import TooManyRequestsError
from ..errors.unauthorized_error import UnauthorizedError
from ..types.error_response import ErrorResponse
from .types.lightning_speech_to_text_request_age_detection import LightningSpeechToTextRequestAgeDetection
from .types.lightning_speech_to_text_request_emotion_detection import LightningSpeechToTextRequestEmotionDetection
from .types.lightning_speech_to_text_request_gender_detection import LightningSpeechToTextRequestGenderDetection
from .types.lightning_speech_to_text_request_language import LightningSpeechToTextRequestLanguage
from .types.lightning_speech_to_text_request_model import LightningSpeechToTextRequestModel
from .types.lightning_speech_to_text_response import LightningSpeechToTextResponse
from .types.pulse_speech_to_text_request_age_detection import PulseSpeechToTextRequestAgeDetection
from .types.pulse_speech_to_text_request_emotion_detection import PulseSpeechToTextRequestEmotionDetection
from .types.pulse_speech_to_text_request_gender_detection import PulseSpeechToTextRequestGenderDetection
from .types.pulse_speech_to_text_request_language import PulseSpeechToTextRequestLanguage
from .types.pulse_speech_to_text_request_model import PulseSpeechToTextRequestModel
from .types.pulse_speech_to_text_response import PulseSpeechToTextResponse

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class RawSpeechToTextClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def lightning(
        self,
        *,
        model: LightningSpeechToTextRequestModel,
        request: typing.Union[bytes, typing.Iterator[bytes], typing.AsyncIterator[bytes]],
        language: typing.Optional[LightningSpeechToTextRequestLanguage] = None,
        webhook_url: typing.Optional[str] = None,
        webhook_extra: typing.Optional[str] = None,
        word_timestamps: typing.Optional[bool] = None,
        diarize: typing.Optional[bool] = None,
        age_detection: typing.Optional[LightningSpeechToTextRequestAgeDetection] = None,
        gender_detection: typing.Optional[LightningSpeechToTextRequestGenderDetection] = None,
        emotion_detection: typing.Optional[LightningSpeechToTextRequestEmotionDetection] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[LightningSpeechToTextResponse]:
        """
        Convert speech to text using the Lightning ASR model. Supports two input methods - raw audio bytes (application/octet-stream) with query parameters, or audio URL (application/json) with URL in body.

        Parameters
        ----------
        model : LightningSpeechToTextRequestModel
            The ASR model to use for transcription

        request : typing.Union[bytes, typing.Iterator[bytes], typing.AsyncIterator[bytes]]

        language : typing.Optional[LightningSpeechToTextRequestLanguage]
            Language of the audio file. Use `multi` for automatic language detection

        webhook_url : typing.Optional[str]

        webhook_extra : typing.Optional[str]

        word_timestamps : typing.Optional[bool]
            Whether to include word and utterance level timestamps in the response

        diarize : typing.Optional[bool]
            Whether to perform speaker diarization

        age_detection : typing.Optional[LightningSpeechToTextRequestAgeDetection]
            Whether to predict age group of the speaker

        gender_detection : typing.Optional[LightningSpeechToTextRequestGenderDetection]
            Whether to predict the gender of the speaker

        emotion_detection : typing.Optional[LightningSpeechToTextRequestEmotionDetection]
            Whether to predict speaker emotions

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[LightningSpeechToTextResponse]
            Speech transcribed successfully
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/v1/lightning/get_text",
            base_url=self._client_wrapper.get_environment().waves,
            method="POST",
            params={
                "model": model,
                "language": language,
                "webhook_url": webhook_url,
                "webhook_extra": webhook_extra,
                "word_timestamps": word_timestamps,
                "diarize": diarize,
                "age_detection": age_detection,
                "gender_detection": gender_detection,
                "emotion_detection": emotion_detection,
            },
            content=request,
            headers={
                "content-type": "application/octet-stream",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    LightningSpeechToTextResponse,
                    construct_type(
                        type_=LightningSpeechToTextResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 413:
                raise ContentTooLargeError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        construct_type(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 429:
                raise TooManyRequestsError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        construct_type(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def pulse(
        self,
        *,
        model: PulseSpeechToTextRequestModel,
        request: typing.Union[bytes, typing.Iterator[bytes], typing.AsyncIterator[bytes]],
        language: typing.Optional[PulseSpeechToTextRequestLanguage] = None,
        webhook_url: typing.Optional[str] = None,
        webhook_extra: typing.Optional[str] = None,
        word_timestamps: typing.Optional[bool] = None,
        diarize: typing.Optional[bool] = None,
        age_detection: typing.Optional[PulseSpeechToTextRequestAgeDetection] = None,
        gender_detection: typing.Optional[PulseSpeechToTextRequestGenderDetection] = None,
        emotion_detection: typing.Optional[PulseSpeechToTextRequestEmotionDetection] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[PulseSpeechToTextResponse]:
        """
        Convert speech to text using the Pulse ASR model. Supports two input methods - raw audio bytes (application/octet-stream) with query parameters, or audio URL (application/json) with URL in body.

        Parameters
        ----------
        model : PulseSpeechToTextRequestModel
            The ASR model to use for transcription

        request : typing.Union[bytes, typing.Iterator[bytes], typing.AsyncIterator[bytes]]

        language : typing.Optional[PulseSpeechToTextRequestLanguage]
            Language of the audio file. Use `multi` for automatic language detection

        webhook_url : typing.Optional[str]

        webhook_extra : typing.Optional[str]

        word_timestamps : typing.Optional[bool]
            Whether to include word and utterance level timestamps in the response

        diarize : typing.Optional[bool]
            Whether to perform speaker diarization

        age_detection : typing.Optional[PulseSpeechToTextRequestAgeDetection]
            Whether to predict age group of the speaker

        gender_detection : typing.Optional[PulseSpeechToTextRequestGenderDetection]
            Whether to predict the gender of the speaker

        emotion_detection : typing.Optional[PulseSpeechToTextRequestEmotionDetection]
            Whether to predict speaker emotions

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[PulseSpeechToTextResponse]
            Speech transcribed successfully
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/v1/pulse/get_text",
            base_url=self._client_wrapper.get_environment().waves,
            method="POST",
            params={
                "model": model,
                "language": language,
                "webhook_url": webhook_url,
                "webhook_extra": webhook_extra,
                "word_timestamps": word_timestamps,
                "diarize": diarize,
                "age_detection": age_detection,
                "gender_detection": gender_detection,
                "emotion_detection": emotion_detection,
            },
            content=request,
            headers={
                "content-type": "application/octet-stream",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PulseSpeechToTextResponse,
                    construct_type(
                        type_=PulseSpeechToTextResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 413:
                raise ContentTooLargeError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        construct_type(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 429:
                raise TooManyRequestsError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        construct_type(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)


class AsyncRawSpeechToTextClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def lightning(
        self,
        *,
        model: LightningSpeechToTextRequestModel,
        request: typing.Union[bytes, typing.Iterator[bytes], typing.AsyncIterator[bytes]],
        language: typing.Optional[LightningSpeechToTextRequestLanguage] = None,
        webhook_url: typing.Optional[str] = None,
        webhook_extra: typing.Optional[str] = None,
        word_timestamps: typing.Optional[bool] = None,
        diarize: typing.Optional[bool] = None,
        age_detection: typing.Optional[LightningSpeechToTextRequestAgeDetection] = None,
        gender_detection: typing.Optional[LightningSpeechToTextRequestGenderDetection] = None,
        emotion_detection: typing.Optional[LightningSpeechToTextRequestEmotionDetection] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[LightningSpeechToTextResponse]:
        """
        Convert speech to text using the Lightning ASR model. Supports two input methods - raw audio bytes (application/octet-stream) with query parameters, or audio URL (application/json) with URL in body.

        Parameters
        ----------
        model : LightningSpeechToTextRequestModel
            The ASR model to use for transcription

        request : typing.Union[bytes, typing.Iterator[bytes], typing.AsyncIterator[bytes]]

        language : typing.Optional[LightningSpeechToTextRequestLanguage]
            Language of the audio file. Use `multi` for automatic language detection

        webhook_url : typing.Optional[str]

        webhook_extra : typing.Optional[str]

        word_timestamps : typing.Optional[bool]
            Whether to include word and utterance level timestamps in the response

        diarize : typing.Optional[bool]
            Whether to perform speaker diarization

        age_detection : typing.Optional[LightningSpeechToTextRequestAgeDetection]
            Whether to predict age group of the speaker

        gender_detection : typing.Optional[LightningSpeechToTextRequestGenderDetection]
            Whether to predict the gender of the speaker

        emotion_detection : typing.Optional[LightningSpeechToTextRequestEmotionDetection]
            Whether to predict speaker emotions

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[LightningSpeechToTextResponse]
            Speech transcribed successfully
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/v1/lightning/get_text",
            base_url=self._client_wrapper.get_environment().waves,
            method="POST",
            params={
                "model": model,
                "language": language,
                "webhook_url": webhook_url,
                "webhook_extra": webhook_extra,
                "word_timestamps": word_timestamps,
                "diarize": diarize,
                "age_detection": age_detection,
                "gender_detection": gender_detection,
                "emotion_detection": emotion_detection,
            },
            content=request,
            headers={
                "content-type": "application/octet-stream",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    LightningSpeechToTextResponse,
                    construct_type(
                        type_=LightningSpeechToTextResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 413:
                raise ContentTooLargeError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        construct_type(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 429:
                raise TooManyRequestsError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        construct_type(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def pulse(
        self,
        *,
        model: PulseSpeechToTextRequestModel,
        request: typing.Union[bytes, typing.Iterator[bytes], typing.AsyncIterator[bytes]],
        language: typing.Optional[PulseSpeechToTextRequestLanguage] = None,
        webhook_url: typing.Optional[str] = None,
        webhook_extra: typing.Optional[str] = None,
        word_timestamps: typing.Optional[bool] = None,
        diarize: typing.Optional[bool] = None,
        age_detection: typing.Optional[PulseSpeechToTextRequestAgeDetection] = None,
        gender_detection: typing.Optional[PulseSpeechToTextRequestGenderDetection] = None,
        emotion_detection: typing.Optional[PulseSpeechToTextRequestEmotionDetection] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[PulseSpeechToTextResponse]:
        """
        Convert speech to text using the Pulse ASR model. Supports two input methods - raw audio bytes (application/octet-stream) with query parameters, or audio URL (application/json) with URL in body.

        Parameters
        ----------
        model : PulseSpeechToTextRequestModel
            The ASR model to use for transcription

        request : typing.Union[bytes, typing.Iterator[bytes], typing.AsyncIterator[bytes]]

        language : typing.Optional[PulseSpeechToTextRequestLanguage]
            Language of the audio file. Use `multi` for automatic language detection

        webhook_url : typing.Optional[str]

        webhook_extra : typing.Optional[str]

        word_timestamps : typing.Optional[bool]
            Whether to include word and utterance level timestamps in the response

        diarize : typing.Optional[bool]
            Whether to perform speaker diarization

        age_detection : typing.Optional[PulseSpeechToTextRequestAgeDetection]
            Whether to predict age group of the speaker

        gender_detection : typing.Optional[PulseSpeechToTextRequestGenderDetection]
            Whether to predict the gender of the speaker

        emotion_detection : typing.Optional[PulseSpeechToTextRequestEmotionDetection]
            Whether to predict speaker emotions

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[PulseSpeechToTextResponse]
            Speech transcribed successfully
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/v1/pulse/get_text",
            base_url=self._client_wrapper.get_environment().waves,
            method="POST",
            params={
                "model": model,
                "language": language,
                "webhook_url": webhook_url,
                "webhook_extra": webhook_extra,
                "word_timestamps": word_timestamps,
                "diarize": diarize,
                "age_detection": age_detection,
                "gender_detection": gender_detection,
                "emotion_detection": emotion_detection,
            },
            content=request,
            headers={
                "content-type": "application/octet-stream",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    PulseSpeechToTextResponse,
                    construct_type(
                        type_=PulseSpeechToTextResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 401:
                raise UnauthorizedError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 413:
                raise ContentTooLargeError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        construct_type(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 429:
                raise TooManyRequestsError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        ErrorResponse,
                        construct_type(
                            type_=ErrorResponse,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 500:
                raise InternalServerError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
