# 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.jsonable_encoder import jsonable_encoder
from ..core.pagination import AsyncPager, SyncPager
from ..core.request_options import RequestOptions
from ..core.unchecked_base_model import construct_type
from ..types.bulk_retrieve_channels_response import BulkRetrieveChannelsResponse
from ..types.channel import Channel
from ..types.channel_status import ChannelStatus
from ..types.list_channels_response import ListChannelsResponse
from ..types.reference_type import ReferenceType
from ..types.retrieve_channel_response import RetrieveChannelResponse

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


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

    def list(
        self,
        *,
        reference_type: typing.Optional[ReferenceType] = None,
        reference_id: typing.Optional[str] = None,
        status: typing.Optional[ChannelStatus] = None,
        cursor: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SyncPager[Channel, ListChannelsResponse]:
        """


        Parameters
        ----------
        reference_type : typing.Optional[ReferenceType]
            Type of reference associated to channel

        reference_id : typing.Optional[str]
            id of reference associated to channel

        status : typing.Optional[ChannelStatus]
            Status of channel

        cursor : typing.Optional[str]
            Cursor to fetch the next result

        limit : typing.Optional[int]
            Maximum number of results to return.
            When not provided the returned results will be cap at 100 channels.

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

        Returns
        -------
        SyncPager[Channel, ListChannelsResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/channels",
            method="GET",
            params={
                "reference_type": reference_type,
                "reference_id": reference_id,
                "status": status,
                "cursor": cursor,
                "limit": limit,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _parsed_response = typing.cast(
                    ListChannelsResponse,
                    construct_type(
                        type_=ListChannelsResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                _items = _parsed_response.channels
                _parsed_next = _parsed_response.cursor
                _has_next = _parsed_next is not None and _parsed_next != ""
                _get_next = lambda: self.list(
                    reference_type=reference_type,
                    reference_id=reference_id,
                    status=status,
                    cursor=_parsed_next,
                    limit=limit,
                    request_options=request_options,
                )
                return SyncPager(has_next=_has_next, items=_items, get_next=_get_next, response=_parsed_response)
            _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 bulk_retrieve(
        self, *, channel_ids: typing.Sequence[str], request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[BulkRetrieveChannelsResponse]:
        """


        Parameters
        ----------
        channel_ids : typing.Sequence[str]

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

        Returns
        -------
        HttpResponse[BulkRetrieveChannelsResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/channels/bulk-retrieve",
            method="POST",
            json={
                "channel_ids": channel_ids,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    BulkRetrieveChannelsResponse,
                    construct_type(
                        type_=BulkRetrieveChannelsResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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(
        self, channel_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[RetrieveChannelResponse]:
        """


        Parameters
        ----------
        channel_id : str
            A channel id

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

        Returns
        -------
        HttpResponse[RetrieveChannelResponse]
            Success
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v2/channels/{jsonable_encoder(channel_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RetrieveChannelResponse,
                    construct_type(
                        type_=RetrieveChannelResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            _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 AsyncRawChannelsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list(
        self,
        *,
        reference_type: typing.Optional[ReferenceType] = None,
        reference_id: typing.Optional[str] = None,
        status: typing.Optional[ChannelStatus] = None,
        cursor: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncPager[Channel, ListChannelsResponse]:
        """


        Parameters
        ----------
        reference_type : typing.Optional[ReferenceType]
            Type of reference associated to channel

        reference_id : typing.Optional[str]
            id of reference associated to channel

        status : typing.Optional[ChannelStatus]
            Status of channel

        cursor : typing.Optional[str]
            Cursor to fetch the next result

        limit : typing.Optional[int]
            Maximum number of results to return.
            When not provided the returned results will be cap at 100 channels.

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

        Returns
        -------
        AsyncPager[Channel, ListChannelsResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/channels",
            method="GET",
            params={
                "reference_type": reference_type,
                "reference_id": reference_id,
                "status": status,
                "cursor": cursor,
                "limit": limit,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _parsed_response = typing.cast(
                    ListChannelsResponse,
                    construct_type(
                        type_=ListChannelsResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                _items = _parsed_response.channels
                _parsed_next = _parsed_response.cursor
                _has_next = _parsed_next is not None and _parsed_next != ""

                async def _get_next():
                    return await self.list(
                        reference_type=reference_type,
                        reference_id=reference_id,
                        status=status,
                        cursor=_parsed_next,
                        limit=limit,
                        request_options=request_options,
                    )

                return AsyncPager(has_next=_has_next, items=_items, get_next=_get_next, response=_parsed_response)
            _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 bulk_retrieve(
        self, *, channel_ids: typing.Sequence[str], request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[BulkRetrieveChannelsResponse]:
        """


        Parameters
        ----------
        channel_ids : typing.Sequence[str]

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

        Returns
        -------
        AsyncHttpResponse[BulkRetrieveChannelsResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/channels/bulk-retrieve",
            method="POST",
            json={
                "channel_ids": channel_ids,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    BulkRetrieveChannelsResponse,
                    construct_type(
                        type_=BulkRetrieveChannelsResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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(
        self, channel_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[RetrieveChannelResponse]:
        """


        Parameters
        ----------
        channel_id : str
            A channel id

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

        Returns
        -------
        AsyncHttpResponse[RetrieveChannelResponse]
            Success
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v2/channels/{jsonable_encoder(channel_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RetrieveChannelResponse,
                    construct_type(
                        type_=RetrieveChannelResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            _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)
