# 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.members_create_response import MembersCreateResponse
from .types.members_list_response_item import MembersListResponseItem

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


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

    def list(
        self, id: int, *, request_options: typing.Optional[RequestOptions] = None
    ) -> typing.List[MembersListResponseItem]:
        """
        List all workspace memberships for 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
        -------
        typing.List[MembersListResponseItem]


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

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.workspaces.members.list(
            id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/workspaces/{jsonable_encoder(id)}/memberships", method="GET", request_options=request_options
        )
        try:
            if 200 <= _response.status_code < 300:
                return pydantic_v1.parse_obj_as(typing.List[MembersListResponseItem], _response.json())  # type: ignore
            _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, id: int, *, user: typing.Optional[int] = OMIT, request_options: typing.Optional[RequestOptions] = None
    ) -> MembersCreateResponse:
        """
        Create a new workspace membership. You will need to provide the workspace ID. You can find this using [List workspaces](list).

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

        user : typing.Optional[int]
            User ID of the workspace member

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

        Returns
        -------
        MembersCreateResponse


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

        client = LabelStudio(
            api_key="YOUR_API_KEY",
        )
        client.workspaces.members.create(
            id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/workspaces/{jsonable_encoder(id)}/memberships",
            method="POST",
            json={"user": user},
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return pydantic_v1.parse_obj_as(MembersCreateResponse, _response.json())  # type: ignore
            _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, *, user: typing.Optional[int] = OMIT, request_options: typing.Optional[RequestOptions] = None
    ) -> None:
        """
        Delete a specific workspace membership. You will need to provide the workspace ID and the user ID. You can find this using [List workspace memberships](list).

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

        user : typing.Optional[int]
            User ID of the workspace member

        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.workspaces.members.delete(
            id=1,
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            f"api/workspaces/{jsonable_encoder(id)}/memberships",
            method="DELETE",
            json={"user": user},
            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 AsyncMembersClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def list(
        self, id: int, *, request_options: typing.Optional[RequestOptions] = None
    ) -> typing.List[MembersListResponseItem]:
        """
        List all workspace memberships for 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
        -------
        typing.List[MembersListResponseItem]


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

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )
        await client.workspaces.members.list(
            id=1,
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/workspaces/{jsonable_encoder(id)}/memberships", method="GET", request_options=request_options
        )
        try:
            if 200 <= _response.status_code < 300:
                return pydantic_v1.parse_obj_as(typing.List[MembersListResponseItem], _response.json())  # type: ignore
            _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, id: int, *, user: typing.Optional[int] = OMIT, request_options: typing.Optional[RequestOptions] = None
    ) -> MembersCreateResponse:
        """
        Create a new workspace membership. You will need to provide the workspace ID. You can find this using [List workspaces](list).

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

        user : typing.Optional[int]
            User ID of the workspace member

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

        Returns
        -------
        MembersCreateResponse


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

        client = AsyncLabelStudio(
            api_key="YOUR_API_KEY",
        )
        await client.workspaces.members.create(
            id=1,
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/workspaces/{jsonable_encoder(id)}/memberships",
            method="POST",
            json={"user": user},
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                return pydantic_v1.parse_obj_as(MembersCreateResponse, _response.json())  # type: ignore
            _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, *, user: typing.Optional[int] = OMIT, request_options: typing.Optional[RequestOptions] = None
    ) -> None:
        """
        Delete a specific workspace membership. You will need to provide the workspace ID and the user ID. You can find this using [List workspace memberships](list).

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

        user : typing.Optional[int]
            User ID of the workspace member

        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.workspaces.members.delete(
            id=1,
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"api/workspaces/{jsonable_encoder(id)}/memberships",
            method="DELETE",
            json={"user": user},
            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)
