# 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.datetime_utils import serialize_datetime
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 .....errors.bad_request_error import BadRequestError
from .....types.get_project_request_v1response import GetProjectRequestV1Response
from .....types.list_project_requests_v1response import ListProjectRequestsV1Response
from .types.requests_list_request_deployment import RequestsListRequestDeployment
from .types.requests_list_request_endpoint import RequestsListRequestEndpoint
from .types.requests_list_request_method import RequestsListRequestMethod
from .types.requests_list_request_status import RequestsListRequestStatus


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

    def list(
        self,
        project_id: str,
        *,
        start: typing.Optional[dt.datetime] = None,
        end: typing.Optional[dt.datetime] = None,
        limit: typing.Optional[float] = None,
        page: typing.Optional[float] = None,
        accessor: typing.Optional[str] = None,
        request_id: typing.Optional[str] = None,
        deployment: typing.Optional[RequestsListRequestDeployment] = None,
        endpoint: typing.Optional[RequestsListRequestEndpoint] = None,
        method: typing.Optional[RequestsListRequestMethod] = None,
        status: typing.Optional[RequestsListRequestStatus] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[ListProjectRequestsV1Response]:
        """
        Generates a list of requests for a specific project

        Parameters
        ----------
        project_id : str
            The unique identifier of the project

        start : typing.Optional[dt.datetime]
            Start date of the requested date range. Formats accepted are YYYY-MM-DD, YYYY-MM-DDTHH:MM:SS, or YYYY-MM-DDTHH:MM:SS+HH:MM

        end : typing.Optional[dt.datetime]
            End date of the requested date range. Formats accepted are YYYY-MM-DD, YYYY-MM-DDTHH:MM:SS, or YYYY-MM-DDTHH:MM:SS+HH:MM

        limit : typing.Optional[float]
            Number of results to return per page. Default 10. Range [1,1000]

        page : typing.Optional[float]
            Navigate and return the results to retrieve specific portions of information of the response

        accessor : typing.Optional[str]
            Filter for requests where a specific accessor was used

        request_id : typing.Optional[str]
            Filter for a specific request id

        deployment : typing.Optional[RequestsListRequestDeployment]
            Filter for requests where a specific deployment was used

        endpoint : typing.Optional[RequestsListRequestEndpoint]
            Filter for requests where a specific endpoint was used

        method : typing.Optional[RequestsListRequestMethod]
            Filter for requests where a specific method was used

        status : typing.Optional[RequestsListRequestStatus]
            Filter for requests that succeeded (status code < 300) or failed (status code >=400)

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

        Returns
        -------
        HttpResponse[ListProjectRequestsV1Response]
            A list of requests for a specific project
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/projects/{jsonable_encoder(project_id)}/requests",
            base_url=self._client_wrapper.get_environment().base,
            method="GET",
            params={
                "start": serialize_datetime(start) if start is not None else None,
                "end": serialize_datetime(end) if end is not None else None,
                "limit": limit,
                "page": page,
                "accessor": accessor,
                "request_id": request_id,
                "deployment": deployment,
                "endpoint": endpoint,
                "method": method,
                "status": status,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ListProjectRequestsV1Response,
                    parse_obj_as(
                        type_=ListProjectRequestsV1Response,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            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)

    def get(
        self, project_id: str, request_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[GetProjectRequestV1Response]:
        """
        Retrieves a specific request for a specific project

        Parameters
        ----------
        project_id : str
            The unique identifier of the project

        request_id : str
            The unique identifier of the request

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

        Returns
        -------
        HttpResponse[GetProjectRequestV1Response]
            A specific request for a specific project
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/projects/{jsonable_encoder(project_id)}/requests/{jsonable_encoder(request_id)}",
            base_url=self._client_wrapper.get_environment().base,
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetProjectRequestV1Response,
                    parse_obj_as(
                        type_=GetProjectRequestV1Response,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            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)


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

    async def list(
        self,
        project_id: str,
        *,
        start: typing.Optional[dt.datetime] = None,
        end: typing.Optional[dt.datetime] = None,
        limit: typing.Optional[float] = None,
        page: typing.Optional[float] = None,
        accessor: typing.Optional[str] = None,
        request_id: typing.Optional[str] = None,
        deployment: typing.Optional[RequestsListRequestDeployment] = None,
        endpoint: typing.Optional[RequestsListRequestEndpoint] = None,
        method: typing.Optional[RequestsListRequestMethod] = None,
        status: typing.Optional[RequestsListRequestStatus] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[ListProjectRequestsV1Response]:
        """
        Generates a list of requests for a specific project

        Parameters
        ----------
        project_id : str
            The unique identifier of the project

        start : typing.Optional[dt.datetime]
            Start date of the requested date range. Formats accepted are YYYY-MM-DD, YYYY-MM-DDTHH:MM:SS, or YYYY-MM-DDTHH:MM:SS+HH:MM

        end : typing.Optional[dt.datetime]
            End date of the requested date range. Formats accepted are YYYY-MM-DD, YYYY-MM-DDTHH:MM:SS, or YYYY-MM-DDTHH:MM:SS+HH:MM

        limit : typing.Optional[float]
            Number of results to return per page. Default 10. Range [1,1000]

        page : typing.Optional[float]
            Navigate and return the results to retrieve specific portions of information of the response

        accessor : typing.Optional[str]
            Filter for requests where a specific accessor was used

        request_id : typing.Optional[str]
            Filter for a specific request id

        deployment : typing.Optional[RequestsListRequestDeployment]
            Filter for requests where a specific deployment was used

        endpoint : typing.Optional[RequestsListRequestEndpoint]
            Filter for requests where a specific endpoint was used

        method : typing.Optional[RequestsListRequestMethod]
            Filter for requests where a specific method was used

        status : typing.Optional[RequestsListRequestStatus]
            Filter for requests that succeeded (status code < 300) or failed (status code >=400)

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

        Returns
        -------
        AsyncHttpResponse[ListProjectRequestsV1Response]
            A list of requests for a specific project
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/projects/{jsonable_encoder(project_id)}/requests",
            base_url=self._client_wrapper.get_environment().base,
            method="GET",
            params={
                "start": serialize_datetime(start) if start is not None else None,
                "end": serialize_datetime(end) if end is not None else None,
                "limit": limit,
                "page": page,
                "accessor": accessor,
                "request_id": request_id,
                "deployment": deployment,
                "endpoint": endpoint,
                "method": method,
                "status": status,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ListProjectRequestsV1Response,
                    parse_obj_as(
                        type_=ListProjectRequestsV1Response,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            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)

    async def get(
        self, project_id: str, request_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[GetProjectRequestV1Response]:
        """
        Retrieves a specific request for a specific project

        Parameters
        ----------
        project_id : str
            The unique identifier of the project

        request_id : str
            The unique identifier of the request

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

        Returns
        -------
        AsyncHttpResponse[GetProjectRequestV1Response]
            A specific request for a specific project
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/projects/{jsonable_encoder(project_id)}/requests/{jsonable_encoder(request_id)}",
            base_url=self._client_wrapper.get_environment().base,
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    GetProjectRequestV1Response,
                    parse_obj_as(
                        type_=GetProjectRequestV1Response,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            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)
