# 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.http_response import AsyncHttpResponse, HttpResponse
from ..core.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..core.serialization import convert_and_respect_annotation_metadata
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.authorized_operation_type import AuthorizedOperationType
from ..types.authz_search_response import AuthzSearchResponse
from ..types.http_validation_error import HttpValidationError
from ..types.principal_request import PrincipalRequest
from ..types.revoke_all_response import RevokeAllResponse
from ..types.terminal_use_resource import TerminalUseResource
from ..types.terminal_use_resource_type import TerminalUseResourceType

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


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

    def check_permission(
        self,
        *,
        operation: AuthorizedOperationType,
        principal: PrincipalRequest,
        resource: TerminalUseResource,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.Dict[str, typing.Any]]:
        """
        Check if a principal has permission to perform an operation on a resource.

        Parameters
        ----------
        operation : AuthorizedOperationType

        principal : PrincipalRequest

        resource : TerminalUseResource

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

        Returns
        -------
        HttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/authz/check",
            method="POST",
            json={
                "operation": operation,
                "principal": convert_and_respect_annotation_metadata(
                    object_=principal, annotation=PrincipalRequest, direction="write"
                ),
                "resource": convert_and_respect_annotation_metadata(
                    object_=resource, annotation=TerminalUseResource, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def grant_permission(
        self,
        *,
        operation: AuthorizedOperationType,
        principal: PrincipalRequest,
        resource: TerminalUseResource,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.Dict[str, typing.Any]]:
        """
        Grant a permission to a principal for a resource.

        Parameters
        ----------
        operation : AuthorizedOperationType

        principal : PrincipalRequest

        resource : TerminalUseResource

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

        Returns
        -------
        HttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/authz/grant",
            method="POST",
            json={
                "operation": operation,
                "principal": convert_and_respect_annotation_metadata(
                    object_=principal, annotation=PrincipalRequest, direction="write"
                ),
                "resource": convert_and_respect_annotation_metadata(
                    object_=resource, annotation=TerminalUseResource, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def revoke_permission(
        self,
        *,
        operation: AuthorizedOperationType,
        principal: PrincipalRequest,
        resource: TerminalUseResource,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.Dict[str, typing.Any]]:
        """
        Revoke a permission from a principal for a resource.

        Parameters
        ----------
        operation : AuthorizedOperationType

        principal : PrincipalRequest

        resource : TerminalUseResource

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

        Returns
        -------
        HttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/authz/revoke",
            method="POST",
            json={
                "operation": operation,
                "principal": convert_and_respect_annotation_metadata(
                    object_=principal, annotation=PrincipalRequest, direction="write"
                ),
                "resource": convert_and_respect_annotation_metadata(
                    object_=resource, annotation=TerminalUseResource, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def revoke_all_for_resource(
        self, *, resource: TerminalUseResource, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[RevokeAllResponse]:
        """
        Revoke all permission tuples for a resource (used during deletion).

        Parameters
        ----------
        resource : TerminalUseResource

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

        Returns
        -------
        HttpResponse[RevokeAllResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/authz/revoke-all",
            method="POST",
            json={
                "resource": convert_and_respect_annotation_metadata(
                    object_=resource, annotation=TerminalUseResource, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RevokeAllResponse,
                    parse_obj_as(
                        type_=RevokeAllResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def search_resources(
        self,
        *,
        filter_resource: TerminalUseResourceType,
        principal: PrincipalRequest,
        filter_operation: typing.Optional[AuthorizedOperationType] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[AuthzSearchResponse]:
        """
        List resource IDs that a principal can access.

        Parameters
        ----------
        filter_resource : TerminalUseResourceType
            Type of resource to search for

        principal : PrincipalRequest

        filter_operation : typing.Optional[AuthorizedOperationType]
            Operation to filter by

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

        Returns
        -------
        HttpResponse[AuthzSearchResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/authz/search",
            method="POST",
            json={
                "filter_operation": filter_operation,
                "filter_resource": filter_resource,
                "principal": convert_and_respect_annotation_metadata(
                    object_=principal, annotation=PrincipalRequest, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    AuthzSearchResponse,
                    parse_obj_as(
                        type_=AuthzSearchResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def write_membership_tuple(
        self,
        *,
        member_id: str,
        org_id: str,
        relation: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.Dict[str, typing.Any]]:
        """
        Write an organization membership tuple.

        Parameters
        ----------
        member_id : str
            Member ID

        org_id : str
            Organization ID

        relation : typing.Optional[str]
            Relation name

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

        Returns
        -------
        HttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/authz/tuple/membership",
            method="POST",
            json={
                "member_id": member_id,
                "org_id": org_id,
                "relation": relation,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def delete_membership_tuple(
        self,
        *,
        member_id: str,
        org_id: str,
        relation: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.Dict[str, typing.Any]]:
        """
        Delete an organization membership tuple.

        Parameters
        ----------
        member_id : str
            Member ID

        org_id : str
            Organization ID

        relation : typing.Optional[str]
            Relation name

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

        Returns
        -------
        HttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/authz/tuple/membership/delete",
            method="POST",
            json={
                "member_id": member_id,
                "org_id": org_id,
                "relation": relation,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def write_parent_tuple(
        self,
        *,
        child_id: str,
        child_type: TerminalUseResourceType,
        parent_id: str,
        parent_type: TerminalUseResourceType,
        relation: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.Dict[str, typing.Any]]:
        """
        Write a parent relationship tuple to establish resource hierarchy.

        Parameters
        ----------
        child_id : str
            ID of child resource

        child_type : TerminalUseResourceType
            Type of child resource

        parent_id : str
            ID of parent resource

        parent_type : TerminalUseResourceType
            Type of parent resource

        relation : typing.Optional[str]
            Relation name

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

        Returns
        -------
        HttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/authz/tuple/parent",
            method="POST",
            json={
                "child_id": child_id,
                "child_type": child_type,
                "parent_id": parent_id,
                "parent_type": parent_type,
                "relation": relation,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def write_role_tuple(
        self,
        *,
        principal: PrincipalRequest,
        resource: TerminalUseResource,
        role: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.Dict[str, typing.Any]]:
        """
        Write a role tuple to grant a role on a resource.

        Parameters
        ----------
        principal : PrincipalRequest

        resource : TerminalUseResource

        role : str
            Role name (viewer, editor, admin, owner)

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

        Returns
        -------
        HttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/authz/tuple/role",
            method="POST",
            json={
                "principal": convert_and_respect_annotation_metadata(
                    object_=principal, annotation=PrincipalRequest, direction="write"
                ),
                "resource": convert_and_respect_annotation_metadata(
                    object_=resource, annotation=TerminalUseResource, direction="write"
                ),
                "role": role,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def delete_role_tuple(
        self,
        *,
        principal: PrincipalRequest,
        resource: TerminalUseResource,
        role: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[typing.Dict[str, typing.Any]]:
        """
        Delete a role tuple to revoke a role on a resource.

        Parameters
        ----------
        principal : PrincipalRequest

        resource : TerminalUseResource

        role : str
            Role name (viewer, editor, admin, owner)

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

        Returns
        -------
        HttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/authz/tuple/role/delete",
            method="POST",
            json={
                "principal": convert_and_respect_annotation_metadata(
                    object_=principal, annotation=PrincipalRequest, direction="write"
                ),
                "resource": convert_and_respect_annotation_metadata(
                    object_=resource, annotation=TerminalUseResource, direction="write"
                ),
                "role": role,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)


class AsyncRawAuthorizationInternalClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def check_permission(
        self,
        *,
        operation: AuthorizedOperationType,
        principal: PrincipalRequest,
        resource: TerminalUseResource,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.Dict[str, typing.Any]]:
        """
        Check if a principal has permission to perform an operation on a resource.

        Parameters
        ----------
        operation : AuthorizedOperationType

        principal : PrincipalRequest

        resource : TerminalUseResource

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

        Returns
        -------
        AsyncHttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/authz/check",
            method="POST",
            json={
                "operation": operation,
                "principal": convert_and_respect_annotation_metadata(
                    object_=principal, annotation=PrincipalRequest, direction="write"
                ),
                "resource": convert_and_respect_annotation_metadata(
                    object_=resource, annotation=TerminalUseResource, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def grant_permission(
        self,
        *,
        operation: AuthorizedOperationType,
        principal: PrincipalRequest,
        resource: TerminalUseResource,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.Dict[str, typing.Any]]:
        """
        Grant a permission to a principal for a resource.

        Parameters
        ----------
        operation : AuthorizedOperationType

        principal : PrincipalRequest

        resource : TerminalUseResource

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

        Returns
        -------
        AsyncHttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/authz/grant",
            method="POST",
            json={
                "operation": operation,
                "principal": convert_and_respect_annotation_metadata(
                    object_=principal, annotation=PrincipalRequest, direction="write"
                ),
                "resource": convert_and_respect_annotation_metadata(
                    object_=resource, annotation=TerminalUseResource, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def revoke_permission(
        self,
        *,
        operation: AuthorizedOperationType,
        principal: PrincipalRequest,
        resource: TerminalUseResource,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.Dict[str, typing.Any]]:
        """
        Revoke a permission from a principal for a resource.

        Parameters
        ----------
        operation : AuthorizedOperationType

        principal : PrincipalRequest

        resource : TerminalUseResource

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

        Returns
        -------
        AsyncHttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/authz/revoke",
            method="POST",
            json={
                "operation": operation,
                "principal": convert_and_respect_annotation_metadata(
                    object_=principal, annotation=PrincipalRequest, direction="write"
                ),
                "resource": convert_and_respect_annotation_metadata(
                    object_=resource, annotation=TerminalUseResource, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def revoke_all_for_resource(
        self, *, resource: TerminalUseResource, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[RevokeAllResponse]:
        """
        Revoke all permission tuples for a resource (used during deletion).

        Parameters
        ----------
        resource : TerminalUseResource

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

        Returns
        -------
        AsyncHttpResponse[RevokeAllResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/authz/revoke-all",
            method="POST",
            json={
                "resource": convert_and_respect_annotation_metadata(
                    object_=resource, annotation=TerminalUseResource, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RevokeAllResponse,
                    parse_obj_as(
                        type_=RevokeAllResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def search_resources(
        self,
        *,
        filter_resource: TerminalUseResourceType,
        principal: PrincipalRequest,
        filter_operation: typing.Optional[AuthorizedOperationType] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[AuthzSearchResponse]:
        """
        List resource IDs that a principal can access.

        Parameters
        ----------
        filter_resource : TerminalUseResourceType
            Type of resource to search for

        principal : PrincipalRequest

        filter_operation : typing.Optional[AuthorizedOperationType]
            Operation to filter by

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

        Returns
        -------
        AsyncHttpResponse[AuthzSearchResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/authz/search",
            method="POST",
            json={
                "filter_operation": filter_operation,
                "filter_resource": filter_resource,
                "principal": convert_and_respect_annotation_metadata(
                    object_=principal, annotation=PrincipalRequest, direction="write"
                ),
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    AuthzSearchResponse,
                    parse_obj_as(
                        type_=AuthzSearchResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def write_membership_tuple(
        self,
        *,
        member_id: str,
        org_id: str,
        relation: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.Dict[str, typing.Any]]:
        """
        Write an organization membership tuple.

        Parameters
        ----------
        member_id : str
            Member ID

        org_id : str
            Organization ID

        relation : typing.Optional[str]
            Relation name

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

        Returns
        -------
        AsyncHttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/authz/tuple/membership",
            method="POST",
            json={
                "member_id": member_id,
                "org_id": org_id,
                "relation": relation,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def delete_membership_tuple(
        self,
        *,
        member_id: str,
        org_id: str,
        relation: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.Dict[str, typing.Any]]:
        """
        Delete an organization membership tuple.

        Parameters
        ----------
        member_id : str
            Member ID

        org_id : str
            Organization ID

        relation : typing.Optional[str]
            Relation name

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

        Returns
        -------
        AsyncHttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/authz/tuple/membership/delete",
            method="POST",
            json={
                "member_id": member_id,
                "org_id": org_id,
                "relation": relation,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def write_parent_tuple(
        self,
        *,
        child_id: str,
        child_type: TerminalUseResourceType,
        parent_id: str,
        parent_type: TerminalUseResourceType,
        relation: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.Dict[str, typing.Any]]:
        """
        Write a parent relationship tuple to establish resource hierarchy.

        Parameters
        ----------
        child_id : str
            ID of child resource

        child_type : TerminalUseResourceType
            Type of child resource

        parent_id : str
            ID of parent resource

        parent_type : TerminalUseResourceType
            Type of parent resource

        relation : typing.Optional[str]
            Relation name

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

        Returns
        -------
        AsyncHttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/authz/tuple/parent",
            method="POST",
            json={
                "child_id": child_id,
                "child_type": child_type,
                "parent_id": parent_id,
                "parent_type": parent_type,
                "relation": relation,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def write_role_tuple(
        self,
        *,
        principal: PrincipalRequest,
        resource: TerminalUseResource,
        role: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.Dict[str, typing.Any]]:
        """
        Write a role tuple to grant a role on a resource.

        Parameters
        ----------
        principal : PrincipalRequest

        resource : TerminalUseResource

        role : str
            Role name (viewer, editor, admin, owner)

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

        Returns
        -------
        AsyncHttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/authz/tuple/role",
            method="POST",
            json={
                "principal": convert_and_respect_annotation_metadata(
                    object_=principal, annotation=PrincipalRequest, direction="write"
                ),
                "resource": convert_and_respect_annotation_metadata(
                    object_=resource, annotation=TerminalUseResource, direction="write"
                ),
                "role": role,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def delete_role_tuple(
        self,
        *,
        principal: PrincipalRequest,
        resource: TerminalUseResource,
        role: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[typing.Dict[str, typing.Any]]:
        """
        Delete a role tuple to revoke a role on a resource.

        Parameters
        ----------
        principal : PrincipalRequest

        resource : TerminalUseResource

        role : str
            Role name (viewer, editor, admin, owner)

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

        Returns
        -------
        AsyncHttpResponse[typing.Dict[str, typing.Any]]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/authz/tuple/role/delete",
            method="POST",
            json={
                "principal": convert_and_respect_annotation_metadata(
                    object_=principal, annotation=PrincipalRequest, direction="write"
                ),
                "resource": convert_and_respect_annotation_metadata(
                    object_=resource, annotation=TerminalUseResource, direction="write"
                ),
                "role": role,
            },
            headers={
                "content-type": "application/json",
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    typing.Dict[str, typing.Any],
                    parse_obj_as(
                        type_=typing.Dict[str, typing.Any],  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
