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

import datetime as dt
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.jsonable_encoder import jsonable_encoder
from ..core.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..core.serialization import convert_and_respect_annotation_metadata
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.config_stream import ConfigStream
from ..types.create_stream_out import CreateStreamOut
from ..types.get_probe_stream_out import GetProbeStreamOut
from ..types.http_validation_error import HttpValidationError
from ..types.languages import Languages
from ..types.source_stream import SourceStream
from ..types.target_stream import TargetStream

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


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

    def create_stream(
        self,
        *,
        voices: typing.Sequence[int],
        dictionaries: typing.Sequence[int],
        config: ConfigStream,
        source_stream: SourceStream,
        target_streams: typing.Sequence[TargetStream],
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        initial_delay: typing.Optional[int] = OMIT,
        timeout_in_mins: typing.Optional[int] = OMIT,
        start_time: typing.Optional[dt.datetime] = OMIT,
        end_time: typing.Optional[dt.datetime] = OMIT,
        timezone: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[CreateStreamOut]:
        """
        Parameters
        ----------
        voices : typing.Sequence[int]
            List of voice identifiers to be used in the stream.

        dictionaries : typing.Sequence[int]
            List of dictionary identifiers to be used in the stream.

        config : ConfigStream
            The shared configuration for the streaming pipeline.

        source_stream : SourceStream
            The source stream configuration details.

        target_streams : typing.Sequence[TargetStream]
            List of target stream configurations.

        name : typing.Optional[str]
            The name of the stream.

        description : typing.Optional[str]
            The description of the stream.

        initial_delay : typing.Optional[int]
            The initial delay in seconds before starting the stream creation process.

        timeout_in_mins : typing.Optional[int]
            The maximum duration in minutes for the stream creation process before timing out.

        start_time : typing.Optional[dt.datetime]

        end_time : typing.Optional[dt.datetime]

        timezone : typing.Optional[str]

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

        Returns
        -------
        HttpResponse[CreateStreamOut]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "stream",
            method="POST",
            json={
                "name": name,
                "description": description,
                "initial_delay": initial_delay,
                "timeout_in_mins": timeout_in_mins,
                "voices": voices,
                "dictionaries": dictionaries,
                "config": convert_and_respect_annotation_metadata(
                    object_=config, annotation=ConfigStream, direction="write"
                ),
                "source_stream": convert_and_respect_annotation_metadata(
                    object_=source_stream, annotation=SourceStream, direction="write"
                ),
                "target_streams": convert_and_respect_annotation_metadata(
                    object_=target_streams, annotation=typing.Sequence[TargetStream], direction="write"
                ),
                "start_time": start_time,
                "end_time": end_time,
                "timezone": timezone,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateStreamOut,
                    parse_obj_as(
                        type_=CreateStreamOut,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 get_stream_result(
        self,
        stream_id: int,
        *,
        run_id: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.Dict[str, typing.Any]]:
        """
        Parameters
        ----------
        stream_id : int

        run_id : typing.Optional[int]

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

        Returns
        -------
        HttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"stream/{jsonable_encoder(stream_id)}",
            method="GET",
            params={
                "run_id": run_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 destroy_stream(
        self,
        stream_id: int,
        *,
        run_id: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.Dict[str, typing.Any]]:
        """
        Parameters
        ----------
        stream_id : int

        run_id : typing.Optional[int]

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

        Returns
        -------
        HttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"stream/{jsonable_encoder(stream_id)}",
            method="DELETE",
            params={
                "run_id": run_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 patch_stream_data(
        self,
        stream_id: int,
        *,
        run_id: typing.Optional[int] = None,
        start_time: typing.Optional[dt.datetime] = OMIT,
        end_time: typing.Optional[dt.datetime] = OMIT,
        payload: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        target_languages: typing.Optional[typing.Sequence[Languages]] = OMIT,
        timezone: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.Dict[str, typing.Any]]:
        """
        Parameters
        ----------
        stream_id : int

        run_id : typing.Optional[int]

        start_time : typing.Optional[dt.datetime]

        end_time : typing.Optional[dt.datetime]

        payload : typing.Optional[typing.Dict[str, typing.Any]]

        target_languages : typing.Optional[typing.Sequence[Languages]]

        timezone : typing.Optional[str]

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

        Returns
        -------
        HttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"stream/{jsonable_encoder(stream_id)}",
            method="PATCH",
            params={
                "run_id": run_id,
            },
            json={
                "start_time": start_time,
                "end_time": end_time,
                "payload": payload,
                "target_languages": target_languages,
                "timezone": timezone,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 get_probe_stream(
        self,
        *,
        url: str,
        run_id: typing.Optional[int] = None,
        passphrase: typing.Optional[str] = OMIT,
        stream_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[GetProbeStreamOut]:
        """
        Parameters
        ----------
        url : str

        run_id : typing.Optional[int]

        passphrase : typing.Optional[str]

        stream_id : typing.Optional[str]

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

        Returns
        -------
        HttpResponse[GetProbeStreamOut]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "stream/probe",
            method="POST",
            params={
                "run_id": run_id,
            },
            json={
                "url": url,
                "passphrase": passphrase,
                "stream_id": stream_id,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetProbeStreamOut,
                    parse_obj_as(
                        type_=GetProbeStreamOut,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 AsyncRawStreamingClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def create_stream(
        self,
        *,
        voices: typing.Sequence[int],
        dictionaries: typing.Sequence[int],
        config: ConfigStream,
        source_stream: SourceStream,
        target_streams: typing.Sequence[TargetStream],
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        initial_delay: typing.Optional[int] = OMIT,
        timeout_in_mins: typing.Optional[int] = OMIT,
        start_time: typing.Optional[dt.datetime] = OMIT,
        end_time: typing.Optional[dt.datetime] = OMIT,
        timezone: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[CreateStreamOut]:
        """
        Parameters
        ----------
        voices : typing.Sequence[int]
            List of voice identifiers to be used in the stream.

        dictionaries : typing.Sequence[int]
            List of dictionary identifiers to be used in the stream.

        config : ConfigStream
            The shared configuration for the streaming pipeline.

        source_stream : SourceStream
            The source stream configuration details.

        target_streams : typing.Sequence[TargetStream]
            List of target stream configurations.

        name : typing.Optional[str]
            The name of the stream.

        description : typing.Optional[str]
            The description of the stream.

        initial_delay : typing.Optional[int]
            The initial delay in seconds before starting the stream creation process.

        timeout_in_mins : typing.Optional[int]
            The maximum duration in minutes for the stream creation process before timing out.

        start_time : typing.Optional[dt.datetime]

        end_time : typing.Optional[dt.datetime]

        timezone : typing.Optional[str]

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

        Returns
        -------
        AsyncHttpResponse[CreateStreamOut]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "stream",
            method="POST",
            json={
                "name": name,
                "description": description,
                "initial_delay": initial_delay,
                "timeout_in_mins": timeout_in_mins,
                "voices": voices,
                "dictionaries": dictionaries,
                "config": convert_and_respect_annotation_metadata(
                    object_=config, annotation=ConfigStream, direction="write"
                ),
                "source_stream": convert_and_respect_annotation_metadata(
                    object_=source_stream, annotation=SourceStream, direction="write"
                ),
                "target_streams": convert_and_respect_annotation_metadata(
                    object_=target_streams, annotation=typing.Sequence[TargetStream], direction="write"
                ),
                "start_time": start_time,
                "end_time": end_time,
                "timezone": timezone,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    CreateStreamOut,
                    parse_obj_as(
                        type_=CreateStreamOut,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 get_stream_result(
        self,
        stream_id: int,
        *,
        run_id: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.Dict[str, typing.Any]]:
        """
        Parameters
        ----------
        stream_id : int

        run_id : typing.Optional[int]

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

        Returns
        -------
        AsyncHttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"stream/{jsonable_encoder(stream_id)}",
            method="GET",
            params={
                "run_id": run_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 destroy_stream(
        self,
        stream_id: int,
        *,
        run_id: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.Dict[str, typing.Any]]:
        """
        Parameters
        ----------
        stream_id : int

        run_id : typing.Optional[int]

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

        Returns
        -------
        AsyncHttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"stream/{jsonable_encoder(stream_id)}",
            method="DELETE",
            params={
                "run_id": run_id,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 patch_stream_data(
        self,
        stream_id: int,
        *,
        run_id: typing.Optional[int] = None,
        start_time: typing.Optional[dt.datetime] = OMIT,
        end_time: typing.Optional[dt.datetime] = OMIT,
        payload: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        target_languages: typing.Optional[typing.Sequence[Languages]] = OMIT,
        timezone: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.Dict[str, typing.Any]]:
        """
        Parameters
        ----------
        stream_id : int

        run_id : typing.Optional[int]

        start_time : typing.Optional[dt.datetime]

        end_time : typing.Optional[dt.datetime]

        payload : typing.Optional[typing.Dict[str, typing.Any]]

        target_languages : typing.Optional[typing.Sequence[Languages]]

        timezone : typing.Optional[str]

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

        Returns
        -------
        AsyncHttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"stream/{jsonable_encoder(stream_id)}",
            method="PATCH",
            params={
                "run_id": run_id,
            },
            json={
                "start_time": start_time,
                "end_time": end_time,
                "payload": payload,
                "target_languages": target_languages,
                "timezone": timezone,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 get_probe_stream(
        self,
        *,
        url: str,
        run_id: typing.Optional[int] = None,
        passphrase: typing.Optional[str] = OMIT,
        stream_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[GetProbeStreamOut]:
        """
        Parameters
        ----------
        url : str

        run_id : typing.Optional[int]

        passphrase : typing.Optional[str]

        stream_id : typing.Optional[str]

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

        Returns
        -------
        AsyncHttpResponse[GetProbeStreamOut]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "stream/probe",
            method="POST",
            params={
                "run_id": run_id,
            },
            json={
                "url": url,
                "passphrase": passphrase,
                "stream_id": stream_id,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetProbeStreamOut,
                    parse_obj_as(
                        type_=GetProbeStreamOut,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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)
