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

import contextlib
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.pydantic_utilities import parse_obj_as
from ....core.request_options import RequestOptions
from ....errors.bad_request_error import BadRequestError
from .types.audio_generate_request_callback_method import AudioGenerateRequestCallbackMethod
from .types.audio_generate_request_container import AudioGenerateRequestContainer
from .types.audio_generate_request_encoding import AudioGenerateRequestEncoding
from .types.audio_generate_request_model import AudioGenerateRequestModel

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


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

    @contextlib.contextmanager
    def generate(
        self,
        *,
        text: str,
        callback: typing.Optional[str] = None,
        callback_method: typing.Optional[AudioGenerateRequestCallbackMethod] = None,
        mip_opt_out: typing.Optional[bool] = None,
        tag: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        bit_rate: typing.Optional[float] = None,
        container: typing.Optional[AudioGenerateRequestContainer] = None,
        encoding: typing.Optional[AudioGenerateRequestEncoding] = None,
        model: typing.Optional[AudioGenerateRequestModel] = None,
        sample_rate: typing.Optional[float] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Iterator[HttpResponse[typing.Iterator[bytes]]]:
        """
        Convert text into natural-sounding speech using Deepgram's TTS REST API

        Parameters
        ----------
        text : str
            The text content to be converted to speech

        callback : typing.Optional[str]
            URL to which we'll make the callback request

        callback_method : typing.Optional[AudioGenerateRequestCallbackMethod]
            HTTP method by which the callback request will be made

        mip_opt_out : typing.Optional[bool]
            Opts out requests from the Deepgram Model Improvement Program. Refer to our Docs for pricing impacts before setting this to true. https://dpgr.am/deepgram-mip

        tag : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Label your requests for the purpose of identification during usage reporting

        bit_rate : typing.Optional[float]
            The bitrate of the audio in bits per second. Choose from predefined ranges or specific values based on the encoding type.

        container : typing.Optional[AudioGenerateRequestContainer]
            Container specifies the file format wrapper for the output audio. The available options depend on the encoding type.

        encoding : typing.Optional[AudioGenerateRequestEncoding]
            Encoding allows you to specify the expected encoding of your audio output

        model : typing.Optional[AudioGenerateRequestModel]
            AI model used to process submitted text

        sample_rate : typing.Optional[float]
            Sample Rate specifies the sample rate for the output audio. Based on the encoding, different sample rates are supported. For some encodings, the sample rate is not configurable

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration. You can pass in configuration such as `chunk_size`, and more to customize the request and response.

        Returns
        -------
        typing.Iterator[HttpResponse[typing.Iterator[bytes]]]
            Successful text-to-speech transformation
        """
        with self._client_wrapper.httpx_client.stream(
            "v1/speak",
            base_url=self._client_wrapper.get_environment().base,
            method="POST",
            params={
                "callback": callback,
                "callback_method": callback_method,
                "mip_opt_out": mip_opt_out,
                "tag": tag,
                "bit_rate": bit_rate,
                "container": container,
                "encoding": encoding,
                "model": model,
                "sample_rate": sample_rate,
            },
            json={
                "text": text,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        ) as _response:

            def _stream() -> HttpResponse[typing.Iterator[bytes]]:
                try:
                    if 200 <= _response.status_code < 300:
                        _chunk_size = request_options.get("chunk_size", None) if request_options is not None else None
                        return HttpResponse(
                            response=_response, data=(_chunk for _chunk in _response.iter_bytes(chunk_size=_chunk_size))
                        )
                    _response.read()
                    if _response.status_code == 400:
                        raise BadRequestError(
                            headers=dict(_response.headers),
                            body=typing.cast(
                                typing.Any,
                                parse_obj_as(
                                    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)

            yield _stream()


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

    @contextlib.asynccontextmanager
    async def generate(
        self,
        *,
        text: str,
        callback: typing.Optional[str] = None,
        callback_method: typing.Optional[AudioGenerateRequestCallbackMethod] = None,
        mip_opt_out: typing.Optional[bool] = None,
        tag: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        bit_rate: typing.Optional[float] = None,
        container: typing.Optional[AudioGenerateRequestContainer] = None,
        encoding: typing.Optional[AudioGenerateRequestEncoding] = None,
        model: typing.Optional[AudioGenerateRequestModel] = None,
        sample_rate: typing.Optional[float] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.AsyncIterator[AsyncHttpResponse[typing.AsyncIterator[bytes]]]:
        """
        Convert text into natural-sounding speech using Deepgram's TTS REST API

        Parameters
        ----------
        text : str
            The text content to be converted to speech

        callback : typing.Optional[str]
            URL to which we'll make the callback request

        callback_method : typing.Optional[AudioGenerateRequestCallbackMethod]
            HTTP method by which the callback request will be made

        mip_opt_out : typing.Optional[bool]
            Opts out requests from the Deepgram Model Improvement Program. Refer to our Docs for pricing impacts before setting this to true. https://dpgr.am/deepgram-mip

        tag : typing.Optional[typing.Union[str, typing.Sequence[str]]]
            Label your requests for the purpose of identification during usage reporting

        bit_rate : typing.Optional[float]
            The bitrate of the audio in bits per second. Choose from predefined ranges or specific values based on the encoding type.

        container : typing.Optional[AudioGenerateRequestContainer]
            Container specifies the file format wrapper for the output audio. The available options depend on the encoding type.

        encoding : typing.Optional[AudioGenerateRequestEncoding]
            Encoding allows you to specify the expected encoding of your audio output

        model : typing.Optional[AudioGenerateRequestModel]
            AI model used to process submitted text

        sample_rate : typing.Optional[float]
            Sample Rate specifies the sample rate for the output audio. Based on the encoding, different sample rates are supported. For some encodings, the sample rate is not configurable

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration. You can pass in configuration such as `chunk_size`, and more to customize the request and response.

        Returns
        -------
        typing.AsyncIterator[AsyncHttpResponse[typing.AsyncIterator[bytes]]]
            Successful text-to-speech transformation
        """
        async with self._client_wrapper.httpx_client.stream(
            "v1/speak",
            base_url=self._client_wrapper.get_environment().base,
            method="POST",
            params={
                "callback": callback,
                "callback_method": callback_method,
                "mip_opt_out": mip_opt_out,
                "tag": tag,
                "bit_rate": bit_rate,
                "container": container,
                "encoding": encoding,
                "model": model,
                "sample_rate": sample_rate,
            },
            json={
                "text": text,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        ) as _response:

            async def _stream() -> AsyncHttpResponse[typing.AsyncIterator[bytes]]:
                try:
                    if 200 <= _response.status_code < 300:
                        _chunk_size = request_options.get("chunk_size", None) if request_options is not None else None
                        return AsyncHttpResponse(
                            response=_response,
                            data=(_chunk async for _chunk in _response.aiter_bytes(chunk_size=_chunk_size)),
                        )
                    await _response.aread()
                    if _response.status_code == 400:
                        raise BadRequestError(
                            headers=dict(_response.headers),
                            body=typing.cast(
                                typing.Any,
                                parse_obj_as(
                                    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)

            yield await _stream()
