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

import typing
from ..core.client_wrapper import SyncClientWrapper
from .members.client import MembersClient
from ..core.request_options import RequestOptions
from ..types.workspace import Workspace
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
from .members.client import AsyncMembersClient

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


class WorkspacesClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper
        self.members = MembersClient(client_wrapper=self._client_wrapper)

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

        List all workspaces for your organization.

        Workspaces in Label Studio let you organize your projects and users into separate spaces. This is useful for managing different teams, departments, or projects within your organization.

        For more information, see [Workspaces in Label Studio](https://docs.humansignal.com/guide/workspaces).

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

        Returns
        -------
        typing.List[Workspace]


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

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.workspaces.list()
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/workspaces",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[Workspace],
                    parse_obj_as(
                        type_=typing.List[Workspace],  # 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,
        *,
        title: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        is_public: typing.Optional[bool] = OMIT,
        is_personal: typing.Optional[bool] = OMIT,
        color: typing.Optional[str] = OMIT,
        is_archived: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Workspace:
        """

        Create a new workspace.

        Workspaces in Label Studio let you organize your projects and users into separate spaces. This is useful for managing different teams, departments, or projects within your organization.

        For more information, see [Workspaces in Label Studio](https://docs.humansignal.com/guide/workspaces).

        Parameters
        ----------
        title : typing.Optional[str]
            Workspace title

        description : typing.Optional[str]
            Workspace description

        is_public : typing.Optional[bool]
            Is workspace public

        is_personal : typing.Optional[bool]
            Is workspace personal

        color : typing.Optional[str]
            Workspace color in HEX format

        is_archived : typing.Optional[bool]
            Is workspace archived

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

        Returns
        -------
        Workspace


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

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.workspaces.create()
        """
        _response = self._client_wrapper.httpx_client.request(
            "api/workspaces",
            method="POST",
            json={
                "title": title,
                "description": description,
                "is_public": is_public,
                "is_personal": is_personal,
                "color": color,
                "is_archived": is_archived,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Workspace,
                    parse_obj_as(
                        type_=Workspace,  # 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) -> Workspace:
        """

        Get information about a specific workspace. You will need to provide the workspace ID. You can find this using [List workspaces](list).

        Parameters
        ----------
        id : int
            Workspace ID

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

        Returns
        -------
        Workspace


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

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.workspaces.get(
            id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/workspaces/{jsonable_encoder(id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Workspace,
                    parse_obj_as(
                        type_=Workspace,  # 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 workspace. You will need to provide the workspace ID. You can find this using [List workspaces](list).

        Parameters
        ----------
        id : int
            Workspace 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.workspaces.delete(
            id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/workspaces/{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,
        *,
        title: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        is_public: typing.Optional[bool] = OMIT,
        is_personal: typing.Optional[bool] = OMIT,
        color: typing.Optional[str] = OMIT,
        is_archived: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Workspace:
        """

        Update a specific workspace. You will need to provide the workspace ID. You can find this using [List workspaces](list).

        Parameters
        ----------
        id : int
            Workspace ID

        title : typing.Optional[str]
            Workspace title

        description : typing.Optional[str]
            Workspace description

        is_public : typing.Optional[bool]
            Is workspace public

        is_personal : typing.Optional[bool]
            Is workspace personal

        color : typing.Optional[str]
            Workspace color in HEX format

        is_archived : typing.Optional[bool]
            Is workspace archived

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

        Returns
        -------
        Workspace


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

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.workspaces.update(
            id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/workspaces/{jsonable_encoder(id)}",
            method="PATCH",
            json={
                "title": title,
                "description": description,
                "is_public": is_public,
                "is_personal": is_personal,
                "color": color,
                "is_archived": is_archived,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Workspace,
                    parse_obj_as(
                        type_=Workspace,  # 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)


class AsyncWorkspacesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper
        self.members = AsyncMembersClient(client_wrapper=self._client_wrapper)

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

        List all workspaces for your organization.

        Workspaces in Label Studio let you organize your projects and users into separate spaces. This is useful for managing different teams, departments, or projects within your organization.

        For more information, see [Workspaces in Label Studio](https://docs.humansignal.com/guide/workspaces).

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

        Returns
        -------
        typing.List[Workspace]


        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


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


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/workspaces",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    typing.List[Workspace],
                    parse_obj_as(
                        type_=typing.List[Workspace],  # 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,
        *,
        title: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        is_public: typing.Optional[bool] = OMIT,
        is_personal: typing.Optional[bool] = OMIT,
        color: typing.Optional[str] = OMIT,
        is_archived: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Workspace:
        """

        Create a new workspace.

        Workspaces in Label Studio let you organize your projects and users into separate spaces. This is useful for managing different teams, departments, or projects within your organization.

        For more information, see [Workspaces in Label Studio](https://docs.humansignal.com/guide/workspaces).

        Parameters
        ----------
        title : typing.Optional[str]
            Workspace title

        description : typing.Optional[str]
            Workspace description

        is_public : typing.Optional[bool]
            Is workspace public

        is_personal : typing.Optional[bool]
            Is workspace personal

        color : typing.Optional[str]
            Workspace color in HEX format

        is_archived : typing.Optional[bool]
            Is workspace archived

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

        Returns
        -------
        Workspace


        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


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


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "api/workspaces",
            method="POST",
            json={
                "title": title,
                "description": description,
                "is_public": is_public,
                "is_personal": is_personal,
                "color": color,
                "is_archived": is_archived,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Workspace,
                    parse_obj_as(
                        type_=Workspace,  # 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) -> Workspace:
        """

        Get information about a specific workspace. You will need to provide the workspace ID. You can find this using [List workspaces](list).

        Parameters
        ----------
        id : int
            Workspace ID

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

        Returns
        -------
        Workspace


        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


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


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

        Parameters
        ----------
        id : int
            Workspace 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.workspaces.delete(
                id=1,
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/workspaces/{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,
        *,
        title: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        is_public: typing.Optional[bool] = OMIT,
        is_personal: typing.Optional[bool] = OMIT,
        color: typing.Optional[str] = OMIT,
        is_archived: typing.Optional[bool] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Workspace:
        """

        Update a specific workspace. You will need to provide the workspace ID. You can find this using [List workspaces](list).

        Parameters
        ----------
        id : int
            Workspace ID

        title : typing.Optional[str]
            Workspace title

        description : typing.Optional[str]
            Workspace description

        is_public : typing.Optional[bool]
            Is workspace public

        is_personal : typing.Optional[bool]
            Is workspace personal

        color : typing.Optional[str]
            Workspace color in HEX format

        is_archived : typing.Optional[bool]
            Is workspace archived

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

        Returns
        -------
        Workspace


        Examples
        --------
        import asyncio

        from label_studio_sdk import AsyncLabelStudio

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )


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


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/workspaces/{jsonable_encoder(id)}",
            method="PATCH",
            json={
                "title": title,
                "description": description,
                "is_public": is_public,
                "is_personal": is_personal,
                "color": color,
                "is_archived": is_archived,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    Workspace,
                    parse_obj_as(
                        type_=Workspace,  # 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)
