# 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.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..core.serialization import convert_and_respect_annotation_metadata
from ..errors.bad_request_error import BadRequestError
from ..errors.conflict_error import ConflictError
from ..errors.not_found_error import NotFoundError
from ..requests.extraction_field import ExtractionFieldParams
from .types.extraction_schemas_create_response import ExtractionSchemasCreateResponse
from .types.extraction_schemas_delete_response import ExtractionSchemasDeleteResponse
from .types.extraction_schemas_get_response import ExtractionSchemasGetResponse
from .types.extraction_schemas_list_response import ExtractionSchemasListResponse
from .types.extraction_schemas_update_response import ExtractionSchemasUpdateResponse

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


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

    def list(
        self, *, project: typing.Optional[str] = None, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[ExtractionSchemasListResponse]:
        """
        Returns all extraction schemas in a project.

        Parameters
        ----------
        project : typing.Optional[str]
            The name of the project to list extraction schemas for.

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

        Returns
        -------
        HttpResponse[ExtractionSchemasListResponse]
            Success response
        """
        _response = self._client_wrapper.httpx_client.request(
            "extraction_schemas",
            base_url=self._client_wrapper.get_environment().base,
            method="GET",
            params={
                "project": project,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ExtractionSchemasListResponse,
                    parse_obj_as(
                        type_=ExtractionSchemasListResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[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 create(
        self,
        *,
        name: str,
        prompt: str,
        fields: typing.Sequence[ExtractionFieldParams],
        project: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[ExtractionSchemasCreateResponse]:
        """
        Creates a new extraction schema in a project.

        Parameters
        ----------
        name : str
            A name for the extraction schema.

        prompt : str
            Instructions for how to extract data from conversations.

        fields : typing.Sequence[ExtractionFieldParams]
            Array of field definitions.

        project : typing.Optional[str]
            The name of the project to create the extraction schema in.

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

        Returns
        -------
        HttpResponse[ExtractionSchemasCreateResponse]
            Success response
        """
        _response = self._client_wrapper.httpx_client.request(
            "extraction_schemas",
            base_url=self._client_wrapper.get_environment().base,
            method="POST",
            params={
                "project": project,
            },
            json={
                "name": name,
                "prompt": prompt,
                "fields": convert_and_respect_annotation_metadata(
                    object_=fields, annotation=typing.Sequence[ExtractionFieldParams], direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ExtractionSchemasCreateResponse,
                    parse_obj_as(
                        type_=ExtractionSchemasCreateResponse,  # 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.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 409:
                raise ConflictError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[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,
        name_or_id: str,
        *,
        project: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[ExtractionSchemasGetResponse]:
        """
        Returns an extraction schema by name or ID.

        Parameters
        ----------
        name_or_id : str
            The name or the ID of the extraction schema to get.

        project : typing.Optional[str]
            The name of the project containing the extraction schema. Only used when `nameOrId` is a name.

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

        Returns
        -------
        HttpResponse[ExtractionSchemasGetResponse]
            Success response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"extraction_schemas/{jsonable_encoder(name_or_id)}",
            base_url=self._client_wrapper.get_environment().base,
            method="GET",
            params={
                "project": project,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ExtractionSchemasGetResponse,
                    parse_obj_as(
                        type_=ExtractionSchemasGetResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[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 delete(
        self,
        name_or_id: str,
        *,
        project: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[ExtractionSchemasDeleteResponse]:
        """
        Deletes an extraction schema by name or ID.

        Parameters
        ----------
        name_or_id : str
            The name or the ID of the extraction schema to delete.

        project : typing.Optional[str]
            The name of the project containing the extraction schema. Only used when `nameOrId` is a name.

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

        Returns
        -------
        HttpResponse[ExtractionSchemasDeleteResponse]
            Success response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"extraction_schemas/{jsonable_encoder(name_or_id)}",
            base_url=self._client_wrapper.get_environment().base,
            method="DELETE",
            params={
                "project": project,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ExtractionSchemasDeleteResponse,
                    parse_obj_as(
                        type_=ExtractionSchemasDeleteResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[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 update(
        self,
        name_or_id: str,
        *,
        project: typing.Optional[str] = None,
        name: typing.Optional[str] = OMIT,
        prompt: typing.Optional[str] = OMIT,
        fields: typing.Optional[typing.Sequence[ExtractionFieldParams]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[ExtractionSchemasUpdateResponse]:
        """
        Updates an extraction schema by name or ID.

        Parameters
        ----------
        name_or_id : str
            The name or the ID of the extraction schema to update.

        project : typing.Optional[str]
            The name of the project containing the extraction schema. Only used when `nameOrId` is a name.

        name : typing.Optional[str]
            A name for the extraction schema.

        prompt : typing.Optional[str]
            Instructions for how to extract data from conversations.

        fields : typing.Optional[typing.Sequence[ExtractionFieldParams]]
            Array of field definitions.

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

        Returns
        -------
        HttpResponse[ExtractionSchemasUpdateResponse]
            Success response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"extraction_schemas/{jsonable_encoder(name_or_id)}",
            base_url=self._client_wrapper.get_environment().base,
            method="PATCH",
            params={
                "project": project,
            },
            json={
                "name": name,
                "prompt": prompt,
                "fields": convert_and_respect_annotation_metadata(
                    object_=fields, annotation=typing.Sequence[ExtractionFieldParams], direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ExtractionSchemasUpdateResponse,
                    parse_obj_as(
                        type_=ExtractionSchemasUpdateResponse,  # 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.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 409:
                raise ConflictError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[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 AsyncRawExtractionSchemasClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list(
        self, *, project: typing.Optional[str] = None, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[ExtractionSchemasListResponse]:
        """
        Returns all extraction schemas in a project.

        Parameters
        ----------
        project : typing.Optional[str]
            The name of the project to list extraction schemas for.

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

        Returns
        -------
        AsyncHttpResponse[ExtractionSchemasListResponse]
            Success response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "extraction_schemas",
            base_url=self._client_wrapper.get_environment().base,
            method="GET",
            params={
                "project": project,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ExtractionSchemasListResponse,
                    parse_obj_as(
                        type_=ExtractionSchemasListResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[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 create(
        self,
        *,
        name: str,
        prompt: str,
        fields: typing.Sequence[ExtractionFieldParams],
        project: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[ExtractionSchemasCreateResponse]:
        """
        Creates a new extraction schema in a project.

        Parameters
        ----------
        name : str
            A name for the extraction schema.

        prompt : str
            Instructions for how to extract data from conversations.

        fields : typing.Sequence[ExtractionFieldParams]
            Array of field definitions.

        project : typing.Optional[str]
            The name of the project to create the extraction schema in.

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

        Returns
        -------
        AsyncHttpResponse[ExtractionSchemasCreateResponse]
            Success response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "extraction_schemas",
            base_url=self._client_wrapper.get_environment().base,
            method="POST",
            params={
                "project": project,
            },
            json={
                "name": name,
                "prompt": prompt,
                "fields": convert_and_respect_annotation_metadata(
                    object_=fields, annotation=typing.Sequence[ExtractionFieldParams], direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ExtractionSchemasCreateResponse,
                    parse_obj_as(
                        type_=ExtractionSchemasCreateResponse,  # 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.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 409:
                raise ConflictError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[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,
        name_or_id: str,
        *,
        project: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[ExtractionSchemasGetResponse]:
        """
        Returns an extraction schema by name or ID.

        Parameters
        ----------
        name_or_id : str
            The name or the ID of the extraction schema to get.

        project : typing.Optional[str]
            The name of the project containing the extraction schema. Only used when `nameOrId` is a name.

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

        Returns
        -------
        AsyncHttpResponse[ExtractionSchemasGetResponse]
            Success response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"extraction_schemas/{jsonable_encoder(name_or_id)}",
            base_url=self._client_wrapper.get_environment().base,
            method="GET",
            params={
                "project": project,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ExtractionSchemasGetResponse,
                    parse_obj_as(
                        type_=ExtractionSchemasGetResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[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 delete(
        self,
        name_or_id: str,
        *,
        project: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[ExtractionSchemasDeleteResponse]:
        """
        Deletes an extraction schema by name or ID.

        Parameters
        ----------
        name_or_id : str
            The name or the ID of the extraction schema to delete.

        project : typing.Optional[str]
            The name of the project containing the extraction schema. Only used when `nameOrId` is a name.

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

        Returns
        -------
        AsyncHttpResponse[ExtractionSchemasDeleteResponse]
            Success response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"extraction_schemas/{jsonable_encoder(name_or_id)}",
            base_url=self._client_wrapper.get_environment().base,
            method="DELETE",
            params={
                "project": project,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ExtractionSchemasDeleteResponse,
                    parse_obj_as(
                        type_=ExtractionSchemasDeleteResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[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 update(
        self,
        name_or_id: str,
        *,
        project: typing.Optional[str] = None,
        name: typing.Optional[str] = OMIT,
        prompt: typing.Optional[str] = OMIT,
        fields: typing.Optional[typing.Sequence[ExtractionFieldParams]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[ExtractionSchemasUpdateResponse]:
        """
        Updates an extraction schema by name or ID.

        Parameters
        ----------
        name_or_id : str
            The name or the ID of the extraction schema to update.

        project : typing.Optional[str]
            The name of the project containing the extraction schema. Only used when `nameOrId` is a name.

        name : typing.Optional[str]
            A name for the extraction schema.

        prompt : typing.Optional[str]
            Instructions for how to extract data from conversations.

        fields : typing.Optional[typing.Sequence[ExtractionFieldParams]]
            Array of field definitions.

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

        Returns
        -------
        AsyncHttpResponse[ExtractionSchemasUpdateResponse]
            Success response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"extraction_schemas/{jsonable_encoder(name_or_id)}",
            base_url=self._client_wrapper.get_environment().base,
            method="PATCH",
            params={
                "project": project,
            },
            json={
                "name": name,
                "prompt": prompt,
                "fields": convert_and_respect_annotation_metadata(
                    object_=fields, annotation=typing.Sequence[ExtractionFieldParams], direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    ExtractionSchemasUpdateResponse,
                    parse_obj_as(
                        type_=ExtractionSchemasUpdateResponse,  # 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.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[typing.Any],  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            if _response.status_code == 409:
                raise ConflictError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Optional[typing.Any],
                        parse_obj_as(
                            type_=typing.Optional[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)
