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

import typing
from ..core.client_wrapper import SyncClientWrapper
from ..core.request_options import RequestOptions
from .types.duration_response import DurationResponse
from ..core.pydantic_utilities import parse_obj_as
from ..transcribe.errors.authentication_error import AuthenticationError
from ..transcribe.errors.rate_limit_error import RateLimitError
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper

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


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

    def get_duration(
        self,
        *,
        audio_url: typing.Optional[str] = OMIT,
        audio_file: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> DurationResponse:
        """
        Returns the audio duration in seconds for a provided URL or base64-encoded file.

        Provide either `audio_url` or `audio_file` (base64). If both are provided, the request is invalid.

        Parameters
        ----------
        audio_url : typing.Optional[str]
            Direct URL to a downloadable audio file.

        audio_file : typing.Optional[str]
            Base64-encoded audio file content. Do not include a data URI prefix.

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

        Returns
        -------
        DurationResponse

        Examples
        --------
        from soferai import SoferAI

        client = SoferAI(
            api_key="YOUR_API_KEY",
        )
        client.utils.get_duration()
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/utils/duration",
            method="POST",
            json={
                "audio_url": audio_url,
                "audio_file": audio_file,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    DurationResponse,
                    parse_obj_as(
                        type_=DurationResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 401:
                raise AuthenticationError()
            if _response.status_code == 429:
                raise RateLimitError()
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)


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

    async def get_duration(
        self,
        *,
        audio_url: typing.Optional[str] = OMIT,
        audio_file: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> DurationResponse:
        """
        Returns the audio duration in seconds for a provided URL or base64-encoded file.

        Provide either `audio_url` or `audio_file` (base64). If both are provided, the request is invalid.

        Parameters
        ----------
        audio_url : typing.Optional[str]
            Direct URL to a downloadable audio file.

        audio_file : typing.Optional[str]
            Base64-encoded audio file content. Do not include a data URI prefix.

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

        Returns
        -------
        DurationResponse

        Examples
        --------
        import asyncio

        from soferai import AsyncSoferAI

        client = AsyncSoferAI(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.utils.get_duration()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/utils/duration",
            method="POST",
            json={
                "audio_url": audio_url,
                "audio_file": audio_file,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    DurationResponse,
                    parse_obj_as(
                        type_=DurationResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 401:
                raise AuthenticationError()
            if _response.status_code == 429:
                raise RateLimitError()
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)
