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

import typing
import urllib.parse
from json.decoder import JSONDecodeError

from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.datetime_utils import serialize_datetime
from ..core.jsonable_encoder import jsonable_encoder
from ..core.pydantic_utilities import pydantic_v1
from ..core.remove_none_from_dict import remove_none_from_dict
from ..core.request_options import RequestOptions
from ..errors.errors.object_not_found_error import ObjectNotFoundError
from ..errors.types.object_not_found_body import ObjectNotFoundBody
from ..pagination.errors.pagination_error import PaginationError
from ..pagination.types.created_gt import CreatedGt
from ..pagination.types.created_lt import CreatedLt
from ..pagination.types.limit import Limit
from ..pagination.types.next_token import NextToken
from ..pagination.types.sort import Sort
from ..pagination.types.updated_gt import UpdatedGt
from ..pagination.types.updated_lt import UpdatedLt
from .types.paginated_plots import PaginatedPlots
from .types.plot import Plot


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

    def get_plot(self, plot_id: str, *, request_options: typing.Optional[RequestOptions] = None) -> Plot:
        """
        Parameters
        ----------
        plot_id : str

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

        Returns
        -------
        Plot

        Examples
        --------
        from sphinxbio.client import Sphinxbio

        client = Sphinxbio(
            token="YOUR_TOKEN",
        )
        client.plots.get_plot(
            plot_id="plot_12345",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            method="GET",
            url=urllib.parse.urljoin(
                f"{self._client_wrapper.get_base_url()}/", f"v1/plots/{jsonable_encoder(plot_id)}"
            ),
            params=jsonable_encoder(
                request_options.get("additional_query_parameters") if request_options is not None else None
            ),
            headers=jsonable_encoder(
                remove_none_from_dict(
                    {
                        **self._client_wrapper.get_headers(),
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
                    }
                )
            ),
            timeout=request_options.get("timeout_in_seconds")
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
            else self._client_wrapper.get_timeout(),
            retries=0,
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
        )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(Plot, _response_json)  # type: ignore
        if "error" in _response_json:
            if _response_json["error"] == "ObjectNotFoundError":
                raise ObjectNotFoundError(
                    pydantic_v1.parse_obj_as(ObjectNotFoundBody, _response_json["content"])  # type: ignore
                )
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def list_plots(
        self,
        *,
        limit: typing.Optional[Limit] = None,
        sort: typing.Optional[Sort] = None,
        created_at_gt: typing.Optional[CreatedGt] = None,
        created_at_lt: typing.Optional[CreatedLt] = None,
        updated_at_gt: typing.Optional[UpdatedGt] = None,
        updated_at_lt: typing.Optional[UpdatedLt] = None,
        next_token: typing.Optional[NextToken] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PaginatedPlots:
        """
        Parameters
        ----------
        limit : typing.Optional[Limit]

        sort : typing.Optional[Sort]

        created_at_gt : typing.Optional[CreatedGt]

        created_at_lt : typing.Optional[CreatedLt]

        updated_at_gt : typing.Optional[UpdatedGt]

        updated_at_lt : typing.Optional[UpdatedLt]

        next_token : typing.Optional[NextToken]

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

        Returns
        -------
        PaginatedPlots

        Examples
        --------
        from sphinxbio.client import Sphinxbio

        client = Sphinxbio(
            token="YOUR_TOKEN",
        )
        client.plots.list_plots()
        """
        _response = self._client_wrapper.httpx_client.request(
            method="GET",
            url=urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "v1/plots"),
            params=jsonable_encoder(
                remove_none_from_dict(
                    {
                        "limit": limit,
                        "sort": sort,
                        "createdAt.gt": serialize_datetime(created_at_gt) if created_at_gt is not None else None,
                        "createdAt.lt": serialize_datetime(created_at_lt) if created_at_lt is not None else None,
                        "updatedAt.gt": serialize_datetime(updated_at_gt) if updated_at_gt is not None else None,
                        "updatedAt.lt": serialize_datetime(updated_at_lt) if updated_at_lt is not None else None,
                        "nextToken": next_token,
                        **(
                            request_options.get("additional_query_parameters", {})
                            if request_options is not None
                            else {}
                        ),
                    }
                )
            ),
            headers=jsonable_encoder(
                remove_none_from_dict(
                    {
                        **self._client_wrapper.get_headers(),
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
                    }
                )
            ),
            timeout=request_options.get("timeout_in_seconds")
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
            else self._client_wrapper.get_timeout(),
            retries=0,
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
        )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(PaginatedPlots, _response_json)  # type: ignore
        if "error" in _response_json:
            if _response_json["error"] == "PaginationError":
                raise PaginationError(pydantic_v1.parse_obj_as(str, _response_json["content"]))  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)


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

    async def get_plot(self, plot_id: str, *, request_options: typing.Optional[RequestOptions] = None) -> Plot:
        """
        Parameters
        ----------
        plot_id : str

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

        Returns
        -------
        Plot

        Examples
        --------
        from sphinxbio.client import AsyncSphinxbio

        client = AsyncSphinxbio(
            token="YOUR_TOKEN",
        )
        await client.plots.get_plot(
            plot_id="plot_12345",
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            method="GET",
            url=urllib.parse.urljoin(
                f"{self._client_wrapper.get_base_url()}/", f"v1/plots/{jsonable_encoder(plot_id)}"
            ),
            params=jsonable_encoder(
                request_options.get("additional_query_parameters") if request_options is not None else None
            ),
            headers=jsonable_encoder(
                remove_none_from_dict(
                    {
                        **self._client_wrapper.get_headers(),
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
                    }
                )
            ),
            timeout=request_options.get("timeout_in_seconds")
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
            else self._client_wrapper.get_timeout(),
            retries=0,
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
        )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(Plot, _response_json)  # type: ignore
        if "error" in _response_json:
            if _response_json["error"] == "ObjectNotFoundError":
                raise ObjectNotFoundError(
                    pydantic_v1.parse_obj_as(ObjectNotFoundBody, _response_json["content"])  # type: ignore
                )
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def list_plots(
        self,
        *,
        limit: typing.Optional[Limit] = None,
        sort: typing.Optional[Sort] = None,
        created_at_gt: typing.Optional[CreatedGt] = None,
        created_at_lt: typing.Optional[CreatedLt] = None,
        updated_at_gt: typing.Optional[UpdatedGt] = None,
        updated_at_lt: typing.Optional[UpdatedLt] = None,
        next_token: typing.Optional[NextToken] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PaginatedPlots:
        """
        Parameters
        ----------
        limit : typing.Optional[Limit]

        sort : typing.Optional[Sort]

        created_at_gt : typing.Optional[CreatedGt]

        created_at_lt : typing.Optional[CreatedLt]

        updated_at_gt : typing.Optional[UpdatedGt]

        updated_at_lt : typing.Optional[UpdatedLt]

        next_token : typing.Optional[NextToken]

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

        Returns
        -------
        PaginatedPlots

        Examples
        --------
        from sphinxbio.client import AsyncSphinxbio

        client = AsyncSphinxbio(
            token="YOUR_TOKEN",
        )
        await client.plots.list_plots()
        """
        _response = await self._client_wrapper.httpx_client.request(
            method="GET",
            url=urllib.parse.urljoin(f"{self._client_wrapper.get_base_url()}/", "v1/plots"),
            params=jsonable_encoder(
                remove_none_from_dict(
                    {
                        "limit": limit,
                        "sort": sort,
                        "createdAt.gt": serialize_datetime(created_at_gt) if created_at_gt is not None else None,
                        "createdAt.lt": serialize_datetime(created_at_lt) if created_at_lt is not None else None,
                        "updatedAt.gt": serialize_datetime(updated_at_gt) if updated_at_gt is not None else None,
                        "updatedAt.lt": serialize_datetime(updated_at_lt) if updated_at_lt is not None else None,
                        "nextToken": next_token,
                        **(
                            request_options.get("additional_query_parameters", {})
                            if request_options is not None
                            else {}
                        ),
                    }
                )
            ),
            headers=jsonable_encoder(
                remove_none_from_dict(
                    {
                        **self._client_wrapper.get_headers(),
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
                    }
                )
            ),
            timeout=request_options.get("timeout_in_seconds")
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
            else self._client_wrapper.get_timeout(),
            retries=0,
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
        )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(PaginatedPlots, _response_json)  # type: ignore
        if "error" in _response_json:
            if _response_json["error"] == "PaginationError":
                raise PaginationError(pydantic_v1.parse_obj_as(str, _response_json["content"]))  # type: ignore
        raise ApiError(status_code=_response.status_code, body=_response_json)
