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

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

import httpx
import pydantic

from ...core.api_error import ApiError
from ...core.jsonable_encoder import jsonable_encoder
from ...core.remove_none_from_headers import remove_none_from_headers
from ...environment import VellumEnvironment
from ...types.paginated_slim_document_list import PaginatedSlimDocumentList
from ...types.upload_document_response import UploadDocumentResponse


class DocumentsClient:
    def __init__(self, *, environment: VellumEnvironment = VellumEnvironment.PRODUCTION, api_key: str):
        self._environment = environment
        self.api_key = api_key

    def list(
        self,
        *,
        document_index_id: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        offset: typing.Optional[int] = None,
        ordering: typing.Optional[str] = None,
    ) -> PaginatedSlimDocumentList:
        _response = httpx.request(
            "GET",
            urllib.parse.urljoin(f"{self._environment.default}/", "v1/documents"),
            params={"document_index_id": document_index_id, "limit": limit, "offset": offset, "ordering": ordering},
            headers=remove_none_from_headers({"X_API_KEY": self.api_key}),
            timeout=None,
        )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(PaginatedSlimDocumentList, _response.json())  # type: ignore
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def upload(
        self,
        *,
        add_to_index_names: typing.Optional[typing.List[str]] = None,
        external_id: typing.Optional[str] = None,
        label: str,
        contents: typing.IO,
        keywords: typing.Optional[typing.List[str]] = None,
    ) -> UploadDocumentResponse:
        _response = httpx.request(
            "POST",
            urllib.parse.urljoin(f"{self._environment.documents}/", "v1/upload-document"),
            data=jsonable_encoder(
                {
                    "add_to_index_names": add_to_index_names,
                    "external_id": external_id,
                    "label": label,
                    "keywords": keywords,
                }
            ),
            files={"contents": contents},
            headers=remove_none_from_headers({"X_API_KEY": self.api_key}),
            timeout=None,
        )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(UploadDocumentResponse, _response.json())  # type: ignore
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)


class AsyncDocumentsClient:
    def __init__(self, *, environment: VellumEnvironment = VellumEnvironment.PRODUCTION, api_key: str):
        self._environment = environment
        self.api_key = api_key

    async def list(
        self,
        *,
        document_index_id: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        offset: typing.Optional[int] = None,
        ordering: typing.Optional[str] = None,
    ) -> PaginatedSlimDocumentList:
        async with httpx.AsyncClient() as _client:
            _response = await _client.request(
                "GET",
                urllib.parse.urljoin(f"{self._environment.default}/", "v1/documents"),
                params={"document_index_id": document_index_id, "limit": limit, "offset": offset, "ordering": ordering},
                headers=remove_none_from_headers({"X_API_KEY": self.api_key}),
                timeout=None,
            )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(PaginatedSlimDocumentList, _response.json())  # type: ignore
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def upload(
        self,
        *,
        add_to_index_names: typing.Optional[typing.List[str]] = None,
        external_id: typing.Optional[str] = None,
        label: str,
        contents: typing.IO,
        keywords: typing.Optional[typing.List[str]] = None,
    ) -> UploadDocumentResponse:
        async with httpx.AsyncClient() as _client:
            _response = await _client.request(
                "POST",
                urllib.parse.urljoin(f"{self._environment.documents}/", "v1/upload-document"),
                data=jsonable_encoder(
                    {
                        "add_to_index_names": add_to_index_names,
                        "external_id": external_id,
                        "label": label,
                        "keywords": keywords,
                    }
                ),
                files={"contents": contents},
                headers=remove_none_from_headers({"X_API_KEY": self.api_key}),
                timeout=None,
            )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(UploadDocumentResponse, _response.json())  # type: ignore
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)
