# 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.jsonable_encoder import jsonable_encoder
from ..core.pydantic_utilities import pydantic_v1
from ..core.request_options import RequestOptions
from ..types.file_upload import FileUpload

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


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

    def get(self, id: int, *, request_options: typing.Optional[RequestOptions] = None) -> FileUpload:
        """
        Retrieve details about a specific uploaded file. To get the file upload ID, use [Get files list](list).

        Parameters
        ----------
        id : int
            A unique integer value identifying this file upload.

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

        Returns
        -------
        FileUpload


        Examples
        --------
        from label_studio_sdk.client import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.files.get(
            id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/import/file-upload/{jsonable_encoder(id)}", method="GET", request_options=request_options
        )
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(FileUpload, _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 delete(self, id: int, *, request_options: typing.Optional[RequestOptions] = None) -> None:
        """
        Delete a specific uploaded file. To get the file upload ID, use [Get files list](list).

        Parameters
        ----------
        id : int
            A unique integer value identifying this file upload.

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

        Returns
        -------
        None

        Examples
        --------
        from label_studio_sdk.client import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.files.delete(
            id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/import/file-upload/{jsonable_encoder(id)}", method="DELETE", request_options=request_options
        )
        if 200 <= _response.status_code < 300:
            return
        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 update(
        self, id: int, *, request: FileUpload, request_options: typing.Optional[RequestOptions] = None
    ) -> FileUpload:
        """
        Update a specific uploaded file. To get the file upload ID, use [Get files list](list).

        You will need to include the file data in the request body. For example:

        ```bash
        curl -H 'Authorization: Token abc123' \ -X POST 'https://localhost:8080/api/import/file-upload/245' -F ‘file=@path/to/my_file.csv’
        ```

        Parameters
        ----------
        id : int
            A unique integer value identifying this file upload.

        request : FileUpload

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

        Returns
        -------
        FileUpload


        Examples
        --------
        from label_studio_sdk import FileUpload
        from label_studio_sdk.client import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.files.update(
            id=1,
            request=FileUpload(),
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/import/file-upload/{jsonable_encoder(id)}",
            method="PATCH",
            json=request,
            request_options=request_options,
            omit=OMIT,
        )
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(FileUpload, _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 list(
        self,
        id: int,
        *,
        all_: typing.Optional[bool] = None,
        ids: typing.Optional[typing.Union[int, typing.Sequence[int]]] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.List[FileUpload]:
        """
        Retrieve the list of uploaded files used to create labeling tasks for a specific project. These are files that have been uploaded directly to Label Studio.

        You must provide a project ID. The project ID can be found in the URL when viewing the project in Label Studio, or you can retrieve all project IDs using [List all projects](../list).

        Parameters
        ----------
        id : int
            A unique integer value identifying this file upload.

        all_ : typing.Optional[bool]
            Set to "true" if you want to retrieve all file uploads

        ids : typing.Optional[typing.Union[int, typing.Sequence[int]]]
            Specify the list of file upload IDs to retrieve, e.g. ids=[1,2,3]

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

        Returns
        -------
        typing.List[FileUpload]


        Examples
        --------
        from label_studio_sdk.client import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.files.list(
            id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/projects/{jsonable_encoder(id)}/file-uploads",
            method="GET",
            params={"all": all_, "ids": ids},
            request_options=request_options,
        )
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(typing.List[FileUpload], _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 delete_many(self, id: int, *, request_options: typing.Optional[RequestOptions] = None) -> None:
        """
        Delete uploaded files for a specific project. These are files that have been uploaded directly to Label Studio.

        You must provide a project ID. The project ID can be found in the URL when viewing the project in Label Studio, or you can retrieve all project IDs using [List all projects](../list).

        Parameters
        ----------
        id : int
            A unique integer value identifying this file upload.

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

        Returns
        -------
        None

        Examples
        --------
        from label_studio_sdk.client import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.files.delete_many(
            id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/projects/{jsonable_encoder(id)}/file-uploads", method="DELETE", request_options=request_options
        )
        if 200 <= _response.status_code < 300:
            return
        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 download(self, filename: str, *, request_options: typing.Optional[RequestOptions] = None) -> None:
        """
        Download a specific uploaded file. If you aren't sure of the file name, try [Get files list](list) first.

        Parameters
        ----------
        filename : str

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

        Returns
        -------
        None

        Examples
        --------
        from label_studio_sdk.client import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.files.download(
            filename="filename",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"data/upload/{jsonable_encoder(filename)}", method="GET", request_options=request_options
        )
        if 200 <= _response.status_code < 300:
            return
        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 AsyncFilesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def get(self, id: int, *, request_options: typing.Optional[RequestOptions] = None) -> FileUpload:
        """
        Retrieve details about a specific uploaded file. To get the file upload ID, use [Get files list](list).

        Parameters
        ----------
        id : int
            A unique integer value identifying this file upload.

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

        Returns
        -------
        FileUpload


        Examples
        --------
        from label_studio_sdk.client import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )
        await client.files.get(
            id=1,
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/import/file-upload/{jsonable_encoder(id)}", method="GET", request_options=request_options
        )
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(FileUpload, _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 delete(self, id: int, *, request_options: typing.Optional[RequestOptions] = None) -> None:
        """
        Delete a specific uploaded file. To get the file upload ID, use [Get files list](list).

        Parameters
        ----------
        id : int
            A unique integer value identifying this file upload.

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

        Returns
        -------
        None

        Examples
        --------
        from label_studio_sdk.client import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )
        await client.files.delete(
            id=1,
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/import/file-upload/{jsonable_encoder(id)}", method="DELETE", request_options=request_options
        )
        if 200 <= _response.status_code < 300:
            return
        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 update(
        self, id: int, *, request: FileUpload, request_options: typing.Optional[RequestOptions] = None
    ) -> FileUpload:
        """
        Update a specific uploaded file. To get the file upload ID, use [Get files list](list).

        You will need to include the file data in the request body. For example:

        ```bash
        curl -H 'Authorization: Token abc123' \ -X POST 'https://localhost:8080/api/import/file-upload/245' -F ‘file=@path/to/my_file.csv’
        ```

        Parameters
        ----------
        id : int
            A unique integer value identifying this file upload.

        request : FileUpload

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

        Returns
        -------
        FileUpload


        Examples
        --------
        from label_studio_sdk import FileUpload
        from label_studio_sdk.client import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )
        await client.files.update(
            id=1,
            request=FileUpload(),
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/import/file-upload/{jsonable_encoder(id)}",
            method="PATCH",
            json=request,
            request_options=request_options,
            omit=OMIT,
        )
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(FileUpload, _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 list(
        self,
        id: int,
        *,
        all_: typing.Optional[bool] = None,
        ids: typing.Optional[typing.Union[int, typing.Sequence[int]]] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.List[FileUpload]:
        """
        Retrieve the list of uploaded files used to create labeling tasks for a specific project. These are files that have been uploaded directly to Label Studio.

        You must provide a project ID. The project ID can be found in the URL when viewing the project in Label Studio, or you can retrieve all project IDs using [List all projects](../list).

        Parameters
        ----------
        id : int
            A unique integer value identifying this file upload.

        all_ : typing.Optional[bool]
            Set to "true" if you want to retrieve all file uploads

        ids : typing.Optional[typing.Union[int, typing.Sequence[int]]]
            Specify the list of file upload IDs to retrieve, e.g. ids=[1,2,3]

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

        Returns
        -------
        typing.List[FileUpload]


        Examples
        --------
        from label_studio_sdk.client import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )
        await client.files.list(
            id=1,
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/projects/{jsonable_encoder(id)}/file-uploads",
            method="GET",
            params={"all": all_, "ids": ids},
            request_options=request_options,
        )
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(typing.List[FileUpload], _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 delete_many(self, id: int, *, request_options: typing.Optional[RequestOptions] = None) -> None:
        """
        Delete uploaded files for a specific project. These are files that have been uploaded directly to Label Studio.

        You must provide a project ID. The project ID can be found in the URL when viewing the project in Label Studio, or you can retrieve all project IDs using [List all projects](../list).

        Parameters
        ----------
        id : int
            A unique integer value identifying this file upload.

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

        Returns
        -------
        None

        Examples
        --------
        from label_studio_sdk.client import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )
        await client.files.delete_many(
            id=1,
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/projects/{jsonable_encoder(id)}/file-uploads", method="DELETE", request_options=request_options
        )
        if 200 <= _response.status_code < 300:
            return
        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 download(self, filename: str, *, request_options: typing.Optional[RequestOptions] = None) -> None:
        """
        Download a specific uploaded file. If you aren't sure of the file name, try [Get files list](list) first.

        Parameters
        ----------
        filename : str

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

        Returns
        -------
        None

        Examples
        --------
        from label_studio_sdk.client import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )
        await client.files.download(
            filename="filename",
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"data/upload/{jsonable_encoder(filename)}", method="GET", request_options=request_options
        )
        if 200 <= _response.status_code < 300:
            return
        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)
