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

import typing

from .. import core
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.chunking_strategy import ChunkingStrategy
from ..types.corpus_key import CorpusKey
from ..types.document import Document
from ..types.table_extraction_config import TableExtractionConfig
from .raw_client import AsyncRawUploadClient, RawUploadClient

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


class UploadClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawUploadClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> RawUploadClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawUploadClient
        """
        return self._raw_client

    def file(
        self,
        corpus_key: CorpusKey,
        *,
        file: core.File,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        metadata: typing.Optional[typing.Dict[str, typing.Optional[typing.Any]]] = OMIT,
        chunking_strategy: typing.Optional[ChunkingStrategy] = OMIT,
        table_extraction_config: typing.Optional[TableExtractionConfig] = OMIT,
        filename: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Document:
        """
        Upload a file, such as a PDF or Word document, to the specified corpus for automatic text extraction and metadata parsing.

        This endpoint expects a `multipart/form-data` request with the following fields:

        - **metadata**: An optional JSON object containing additional metadata to associate with the document.
          Example: `metadata={"key": "value"}`
        - **chunking_strategy**: An optional JSON object that sets the chunking method for text extraction.
          - By default, the platform uses sentence-based chunking (one chunk per sentence).
          - Example for explicit sentence chunking: `chunking_strategy={"type":"sentence_chunking_strategy"}`
          - Example for max chars chunking: `chunking_strategy={"type":"max_chars_chunking_strategy","max_chars_per_chunk":512}`
        - **table_extraction_config**: An optional JSON object to control table extraction from supported file types (e.g., PDF).
          Example: `table_extraction_config={"extract_tables": true}`
        - **file**: The file to upload. Attach your file as the value for this field.
        - **filename**: The desired name for the uploaded file. Specify as part of the file field in your request.

        Parameters
        ----------
        corpus_key : CorpusKey
            The unique key identifying the corpus of which to upload the file.

        file : core.File
            See core.File for more documentation

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

        metadata : typing.Optional[typing.Dict[str, typing.Optional[typing.Any]]]
            Arbitrary object that will be attached as document metadata to the extracted document.

        chunking_strategy : typing.Optional[ChunkingStrategy]

        table_extraction_config : typing.Optional[TableExtractionConfig]

        filename : typing.Optional[str]
            Optional multipart section to override the filename.

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

        Returns
        -------
        Document
            The extracted document has been parsed and added to the corpus.

        Examples
        --------
        from vectara import Vectara

        client = Vectara(
            api_key="YOUR_API_KEY",
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )
        client.upload.file(
            corpus_key="my-corpus",
        )
        """
        _response = self._raw_client.file(
            corpus_key,
            file=file,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            metadata=metadata,
            chunking_strategy=chunking_strategy,
            table_extraction_config=table_extraction_config,
            filename=filename,
            request_options=request_options,
        )
        return _response.data


class AsyncUploadClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawUploadClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawUploadClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawUploadClient
        """
        return self._raw_client

    async def file(
        self,
        corpus_key: CorpusKey,
        *,
        file: core.File,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        metadata: typing.Optional[typing.Dict[str, typing.Optional[typing.Any]]] = OMIT,
        chunking_strategy: typing.Optional[ChunkingStrategy] = OMIT,
        table_extraction_config: typing.Optional[TableExtractionConfig] = OMIT,
        filename: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Document:
        """
        Upload a file, such as a PDF or Word document, to the specified corpus for automatic text extraction and metadata parsing.

        This endpoint expects a `multipart/form-data` request with the following fields:

        - **metadata**: An optional JSON object containing additional metadata to associate with the document.
          Example: `metadata={"key": "value"}`
        - **chunking_strategy**: An optional JSON object that sets the chunking method for text extraction.
          - By default, the platform uses sentence-based chunking (one chunk per sentence).
          - Example for explicit sentence chunking: `chunking_strategy={"type":"sentence_chunking_strategy"}`
          - Example for max chars chunking: `chunking_strategy={"type":"max_chars_chunking_strategy","max_chars_per_chunk":512}`
        - **table_extraction_config**: An optional JSON object to control table extraction from supported file types (e.g., PDF).
          Example: `table_extraction_config={"extract_tables": true}`
        - **file**: The file to upload. Attach your file as the value for this field.
        - **filename**: The desired name for the uploaded file. Specify as part of the file field in your request.

        Parameters
        ----------
        corpus_key : CorpusKey
            The unique key identifying the corpus of which to upload the file.

        file : core.File
            See core.File for more documentation

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

        metadata : typing.Optional[typing.Dict[str, typing.Optional[typing.Any]]]
            Arbitrary object that will be attached as document metadata to the extracted document.

        chunking_strategy : typing.Optional[ChunkingStrategy]

        table_extraction_config : typing.Optional[TableExtractionConfig]

        filename : typing.Optional[str]
            Optional multipart section to override the filename.

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

        Returns
        -------
        Document
            The extracted document has been parsed and added to the corpus.

        Examples
        --------
        import asyncio

        from vectara import AsyncVectara

        client = AsyncVectara(
            api_key="YOUR_API_KEY",
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )


        async def main() -> None:
            await client.upload.file(
                corpus_key="my-corpus",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.file(
            corpus_key,
            file=file,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            metadata=metadata,
            chunking_strategy=chunking_strategy,
            table_extraction_config=table_extraction_config,
            filename=filename,
            request_options=request_options,
        )
        return _response.data
