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

import typing
from ...core.client_wrapper import SyncClientWrapper
from ...core.request_options import RequestOptions
from ...types.s3s_export_storage import S3SExportStorage
from ...core.pydantic_utilities import parse_obj_as
from json.decoder import JSONDecodeError
from ...core.api_error import ApiError
from ...core.jsonable_encoder import jsonable_encoder
from ...core.client_wrapper import AsyncClientWrapper

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


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

    def list(
        self, *, project: typing.Optional[int] = None, request_options: typing.Optional[RequestOptions] = None
    ) -> typing.List[S3SExportStorage]:
        """

        You can connect your S3 bucket to Label Studio as a source storage or target storage. Use this API request to get a list of all S3 export (target) storage connections for a specific project.

        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](../projects/list).

        For more information about working with external storage, see [Sync data from external storage](https://labelstud.io/guide/storage).

        Parameters
        ----------
        project : typing.Optional[int]
            Project ID

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

        Returns
        -------
        typing.List[S3SExportStorage]


        Examples
        --------
        from label_studio_sdk import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.export_storage.s3s.list()
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/storages/export/s3s",
            method="GET",
            params={
                "project": project,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[S3SExportStorage],
                    parse_obj_as(
                        type_=typing.List[S3SExportStorage],  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _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 create(
        self,
        *,
        can_delete_objects: typing.Optional[bool] = OMIT,
        title: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        project: typing.Optional[int] = OMIT,
        bucket: typing.Optional[str] = OMIT,
        prefix: typing.Optional[str] = OMIT,
        external_id: typing.Optional[str] = OMIT,
        role_arn: typing.Optional[str] = OMIT,
        region_name: typing.Optional[str] = OMIT,
        s3endpoint: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> S3SExportStorage:
        """

        Create a new target storage connection to a S3 bucket with IAM role access.

        For information about the required fields and prerequisites, see [Amazon S3](https://docs.humansignal.com/guide/storage#Set-up-an-S3-connection-with-IAM-role-access) in the Label Studio documentation.

        Parameters
        ----------
        can_delete_objects : typing.Optional[bool]
            Deletion from storage enabled.

        title : typing.Optional[str]
            Storage title

        description : typing.Optional[str]
            Storage description

        project : typing.Optional[int]
            Project ID

        bucket : typing.Optional[str]
            S3 bucket name

        prefix : typing.Optional[str]
            S3 bucket prefix

        external_id : typing.Optional[str]
            AWS External ID

        role_arn : typing.Optional[str]
            AWS Role ARN

        region_name : typing.Optional[str]
            AWS Region

        s3endpoint : typing.Optional[str]
            S3 Endpoint

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

        Returns
        -------
        S3SExportStorage


        Examples
        --------
        from label_studio_sdk import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.export_storage.s3s.create()
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/storages/export/s3s",
            method="POST",
            json={
                "can_delete_objects": can_delete_objects,
                "title": title,
                "description": description,
                "project": project,
                "bucket": bucket,
                "prefix": prefix,
                "external_id": external_id,
                "role_arn": role_arn,
                "region_name": region_name,
                "s3_endpoint": s3endpoint,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    S3SExportStorage,
                    parse_obj_as(
                        type_=S3SExportStorage,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _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 get(self, id: int, *, request_options: typing.Optional[RequestOptions] = None) -> S3SExportStorage:
        """

        Get a specific S3 export storage connection. You will need to provide the export storage ID. You can find this using [List export storages](list).

        Parameters
        ----------
        id : int
            Export storage ID

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

        Returns
        -------
        S3SExportStorage


        Examples
        --------
        from label_studio_sdk import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.export_storage.s3s.get(
            id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/storages/export/s3s/{jsonable_encoder(id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    S3SExportStorage,
                    parse_obj_as(
                        type_=S3SExportStorage,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _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 S3 export storage connection. You will need to provide the export storage ID. You can find this using [List export storages](list).

        Parameters
        ----------
        id : int
            Export storage ID

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

        Returns
        -------
        None

        Examples
        --------
        from label_studio_sdk import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.export_storage.s3s.delete(
            id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/storages/export/s3s/{jsonable_encoder(id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            _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,
        *,
        can_delete_objects: typing.Optional[bool] = OMIT,
        title: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        project: typing.Optional[int] = OMIT,
        bucket: typing.Optional[str] = OMIT,
        prefix: typing.Optional[str] = OMIT,
        external_id: typing.Optional[str] = OMIT,
        role_arn: typing.Optional[str] = OMIT,
        region_name: typing.Optional[str] = OMIT,
        s3endpoint: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> S3SExportStorage:
        """

        Update a specific S3 export storage connection. You will need to provide the export storage ID. You can find this using [List export storages](list).

        Parameters
        ----------
        id : int
            Export storage ID

        can_delete_objects : typing.Optional[bool]
            Deletion from storage enabled.

        title : typing.Optional[str]
            Storage title

        description : typing.Optional[str]
            Storage description

        project : typing.Optional[int]
            Project ID

        bucket : typing.Optional[str]
            S3 bucket name

        prefix : typing.Optional[str]
            S3 bucket prefix

        external_id : typing.Optional[str]
            AWS External ID

        role_arn : typing.Optional[str]
            AWS Role ARN

        region_name : typing.Optional[str]
            AWS Region

        s3endpoint : typing.Optional[str]
            S3 Endpoint

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

        Returns
        -------
        S3SExportStorage


        Examples
        --------
        from label_studio_sdk import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.export_storage.s3s.update(
            id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/storages/export/s3s/{jsonable_encoder(id)}",
            method="PATCH",
            json={
                "can_delete_objects": can_delete_objects,
                "title": title,
                "description": description,
                "project": project,
                "bucket": bucket,
                "prefix": prefix,
                "external_id": external_id,
                "role_arn": role_arn,
                "region_name": region_name,
                "s3_endpoint": s3endpoint,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    S3SExportStorage,
                    parse_obj_as(
                        type_=S3SExportStorage,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _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 validate(
        self,
        *,
        can_delete_objects: typing.Optional[bool] = OMIT,
        title: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        project: typing.Optional[int] = OMIT,
        bucket: typing.Optional[str] = OMIT,
        prefix: typing.Optional[str] = OMIT,
        external_id: typing.Optional[str] = OMIT,
        role_arn: typing.Optional[str] = OMIT,
        region_name: typing.Optional[str] = OMIT,
        s3endpoint: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """

        Validate a specific S3 export storage connection. This is useful to ensure that the storage configuration settings are correct and operational before attempting to export data.

        Parameters
        ----------
        can_delete_objects : typing.Optional[bool]
            Deletion from storage enabled.

        title : typing.Optional[str]
            Storage title

        description : typing.Optional[str]
            Storage description

        project : typing.Optional[int]
            Project ID

        bucket : typing.Optional[str]
            S3 bucket name

        prefix : typing.Optional[str]
            S3 bucket prefix

        external_id : typing.Optional[str]
            AWS External ID

        role_arn : typing.Optional[str]
            AWS Role ARN

        region_name : typing.Optional[str]
            AWS Region

        s3endpoint : typing.Optional[str]
            S3 Endpoint

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

        Returns
        -------
        None

        Examples
        --------
        from label_studio_sdk import LabelStudio

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.export_storage.s3s.validate()
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/storages/export/s3s/validate",
            method="POST",
            json={
                "can_delete_objects": can_delete_objects,
                "title": title,
                "description": description,
                "project": project,
                "bucket": bucket,
                "prefix": prefix,
                "external_id": external_id,
                "role_arn": role_arn,
                "region_name": region_name,
                "s3_endpoint": s3endpoint,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            _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 AsyncS3SClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list(
        self, *, project: typing.Optional[int] = None, request_options: typing.Optional[RequestOptions] = None
    ) -> typing.List[S3SExportStorage]:
        """

        You can connect your S3 bucket to Label Studio as a source storage or target storage. Use this API request to get a list of all S3 export (target) storage connections for a specific project.

        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](../projects/list).

        For more information about working with external storage, see [Sync data from external storage](https://labelstud.io/guide/storage).

        Parameters
        ----------
        project : typing.Optional[int]
            Project ID

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

        Returns
        -------
        typing.List[S3SExportStorage]


        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.export_storage.s3s.list()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/storages/export/s3s",
            method="GET",
            params={
                "project": project,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[S3SExportStorage],
                    parse_obj_as(
                        type_=typing.List[S3SExportStorage],  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _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 create(
        self,
        *,
        can_delete_objects: typing.Optional[bool] = OMIT,
        title: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        project: typing.Optional[int] = OMIT,
        bucket: typing.Optional[str] = OMIT,
        prefix: typing.Optional[str] = OMIT,
        external_id: typing.Optional[str] = OMIT,
        role_arn: typing.Optional[str] = OMIT,
        region_name: typing.Optional[str] = OMIT,
        s3endpoint: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> S3SExportStorage:
        """

        Create a new target storage connection to a S3 bucket with IAM role access.

        For information about the required fields and prerequisites, see [Amazon S3](https://docs.humansignal.com/guide/storage#Set-up-an-S3-connection-with-IAM-role-access) in the Label Studio documentation.

        Parameters
        ----------
        can_delete_objects : typing.Optional[bool]
            Deletion from storage enabled.

        title : typing.Optional[str]
            Storage title

        description : typing.Optional[str]
            Storage description

        project : typing.Optional[int]
            Project ID

        bucket : typing.Optional[str]
            S3 bucket name

        prefix : typing.Optional[str]
            S3 bucket prefix

        external_id : typing.Optional[str]
            AWS External ID

        role_arn : typing.Optional[str]
            AWS Role ARN

        region_name : typing.Optional[str]
            AWS Region

        s3endpoint : typing.Optional[str]
            S3 Endpoint

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

        Returns
        -------
        S3SExportStorage


        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.export_storage.s3s.create()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/storages/export/s3s",
            method="POST",
            json={
                "can_delete_objects": can_delete_objects,
                "title": title,
                "description": description,
                "project": project,
                "bucket": bucket,
                "prefix": prefix,
                "external_id": external_id,
                "role_arn": role_arn,
                "region_name": region_name,
                "s3_endpoint": s3endpoint,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    S3SExportStorage,
                    parse_obj_as(
                        type_=S3SExportStorage,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _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 get(self, id: int, *, request_options: typing.Optional[RequestOptions] = None) -> S3SExportStorage:
        """

        Get a specific S3 export storage connection. You will need to provide the export storage ID. You can find this using [List export storages](list).

        Parameters
        ----------
        id : int
            Export storage ID

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

        Returns
        -------
        S3SExportStorage


        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.export_storage.s3s.get(
                id=1,
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/storages/export/s3s/{jsonable_encoder(id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    S3SExportStorage,
                    parse_obj_as(
                        type_=S3SExportStorage,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _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 S3 export storage connection. You will need to provide the export storage ID. You can find this using [List export storages](list).

        Parameters
        ----------
        id : int
            Export storage ID

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

        Returns
        -------
        None

        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.export_storage.s3s.delete(
                id=1,
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/storages/export/s3s/{jsonable_encoder(id)}",
            method="DELETE",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            _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,
        *,
        can_delete_objects: typing.Optional[bool] = OMIT,
        title: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        project: typing.Optional[int] = OMIT,
        bucket: typing.Optional[str] = OMIT,
        prefix: typing.Optional[str] = OMIT,
        external_id: typing.Optional[str] = OMIT,
        role_arn: typing.Optional[str] = OMIT,
        region_name: typing.Optional[str] = OMIT,
        s3endpoint: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> S3SExportStorage:
        """

        Update a specific S3 export storage connection. You will need to provide the export storage ID. You can find this using [List export storages](list).

        Parameters
        ----------
        id : int
            Export storage ID

        can_delete_objects : typing.Optional[bool]
            Deletion from storage enabled.

        title : typing.Optional[str]
            Storage title

        description : typing.Optional[str]
            Storage description

        project : typing.Optional[int]
            Project ID

        bucket : typing.Optional[str]
            S3 bucket name

        prefix : typing.Optional[str]
            S3 bucket prefix

        external_id : typing.Optional[str]
            AWS External ID

        role_arn : typing.Optional[str]
            AWS Role ARN

        region_name : typing.Optional[str]
            AWS Region

        s3endpoint : typing.Optional[str]
            S3 Endpoint

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

        Returns
        -------
        S3SExportStorage


        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.export_storage.s3s.update(
                id=1,
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/storages/export/s3s/{jsonable_encoder(id)}",
            method="PATCH",
            json={
                "can_delete_objects": can_delete_objects,
                "title": title,
                "description": description,
                "project": project,
                "bucket": bucket,
                "prefix": prefix,
                "external_id": external_id,
                "role_arn": role_arn,
                "region_name": region_name,
                "s3_endpoint": s3endpoint,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    S3SExportStorage,
                    parse_obj_as(
                        type_=S3SExportStorage,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            _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 validate(
        self,
        *,
        can_delete_objects: typing.Optional[bool] = OMIT,
        title: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        project: typing.Optional[int] = OMIT,
        bucket: typing.Optional[str] = OMIT,
        prefix: typing.Optional[str] = OMIT,
        external_id: typing.Optional[str] = OMIT,
        role_arn: typing.Optional[str] = OMIT,
        region_name: typing.Optional[str] = OMIT,
        s3endpoint: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> None:
        """

        Validate a specific S3 export storage connection. This is useful to ensure that the storage configuration settings are correct and operational before attempting to export data.

        Parameters
        ----------
        can_delete_objects : typing.Optional[bool]
            Deletion from storage enabled.

        title : typing.Optional[str]
            Storage title

        description : typing.Optional[str]
            Storage description

        project : typing.Optional[int]
            Project ID

        bucket : typing.Optional[str]
            S3 bucket name

        prefix : typing.Optional[str]
            S3 bucket prefix

        external_id : typing.Optional[str]
            AWS External ID

        role_arn : typing.Optional[str]
            AWS Role ARN

        region_name : typing.Optional[str]
            AWS Region

        s3endpoint : typing.Optional[str]
            S3 Endpoint

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

        Returns
        -------
        None

        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.export_storage.s3s.validate()


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/storages/export/s3s/validate",
            method="POST",
            json={
                "can_delete_objects": can_delete_objects,
                "title": title,
                "description": description,
                "project": project,
                "bucket": bucket,
                "prefix": prefix,
                "external_id": external_id,
                "role_arn": role_arn,
                "region_name": region_name,
                "s3_endpoint": s3endpoint,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return
            _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)
