# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

from typing import Type, Optional, cast

import httpx

from ..._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
from ..._utils import path_template, maybe_transform, async_maybe_transform
from ..._compat import cached_property
from ..._resource import SyncAPIResource, AsyncAPIResource
from ..._response import (
    to_raw_response_wrapper,
    to_streamed_response_wrapper,
    async_to_raw_response_wrapper,
    async_to_streamed_response_wrapper,
)
from ..._wrappers import ResultWrapper
from ...pagination import SyncV4PagePaginationArray, AsyncV4PagePaginationArray
from ..._base_client import AsyncPaginator, make_request_options
from ...types.resource_sharing import recipient_get_params, recipient_list_params, recipient_create_params
from ...types.resource_sharing.recipient_get_response import RecipientGetResponse
from ...types.resource_sharing.recipient_list_response import RecipientListResponse
from ...types.resource_sharing.recipient_create_response import RecipientCreateResponse
from ...types.resource_sharing.recipient_delete_response import RecipientDeleteResponse

__all__ = ["RecipientsResource", "AsyncRecipientsResource"]


class RecipientsResource(SyncAPIResource):
    @cached_property
    def with_raw_response(self) -> RecipientsResourceWithRawResponse:
        """
        This property can be used as a prefix for any HTTP method call to return
        the raw response object instead of the parsed content.

        For more information, see https://www.github.com/cloudflare/cloudflare-python#accessing-raw-response-data-eg-headers
        """
        return RecipientsResourceWithRawResponse(self)

    @cached_property
    def with_streaming_response(self) -> RecipientsResourceWithStreamingResponse:
        """
        An alternative to `.with_raw_response` that doesn't eagerly read the response body.

        For more information, see https://www.github.com/cloudflare/cloudflare-python#with_streaming_response
        """
        return RecipientsResourceWithStreamingResponse(self)

    def create(
        self,
        share_id: str,
        *,
        path_account_id: str,
        body_account_id: str | Omit = omit,
        organization_id: str | Omit = omit,
        recipient_account_id: str | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> Optional[RecipientCreateResponse]:
        """
        Adds a recipient to a resource share, granting them access to the shared
        resources.

        Args:
          path_account_id: Account identifier.

          share_id: Share identifier tag.

          body_account_id: Deprecated alias for `recipient_account_id`. Use `recipient_account_id` instead.
              The body field collided with the URL path parameter of the same name, which
              prevented SDK generators from distinguishing the source account (in the URL)
              from the recipient account (in the body). Both names will continue to be
              accepted until 2027-05-26 (see `x-sunset`).

          organization_id: Organization identifier.

          recipient_account_id: The account that will receive the share.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not path_account_id:
            raise ValueError(f"Expected a non-empty value for `path_account_id` but received {path_account_id!r}")
        if not share_id:
            raise ValueError(f"Expected a non-empty value for `share_id` but received {share_id!r}")
        return self._post(
            path_template(
                "/accounts/{path_account_id}/shares/{share_id}/recipients",
                path_account_id=path_account_id,
                share_id=share_id,
            ),
            body=maybe_transform(
                {
                    "body_account_id": body_account_id,
                    "organization_id": organization_id,
                    "recipient_account_id": recipient_account_id,
                },
                recipient_create_params.RecipientCreateParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[Optional[RecipientCreateResponse]]._unwrapper,
            ),
            cast_to=cast(Type[Optional[RecipientCreateResponse]], ResultWrapper[RecipientCreateResponse]),
        )

    def list(
        self,
        share_id: str,
        *,
        account_id: str,
        include_resources: bool | Omit = omit,
        page: int | Omit = omit,
        per_page: int | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> SyncV4PagePaginationArray[RecipientListResponse]:
        """
        List share recipients by share ID.

        Args:
          account_id: Account identifier.

          share_id: Share identifier tag.

          include_resources: Include resources in the response.

          page: Page number. Defaults to `1` when `per_page` is supplied without `page`. May be
              omitted entirely along with `per_page` to receive a non-paginated response.

          per_page: Number of objects to return per page. Defaults to `20` when `page` is supplied
              without `per_page`. May be omitted entirely along with `page` to receive a
              non-paginated response.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not share_id:
            raise ValueError(f"Expected a non-empty value for `share_id` but received {share_id!r}")
        return self._get_api_list(
            path_template(
                "/accounts/{account_id}/shares/{share_id}/recipients", account_id=account_id, share_id=share_id
            ),
            page=SyncV4PagePaginationArray[RecipientListResponse],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "include_resources": include_resources,
                        "page": page,
                        "per_page": per_page,
                    },
                    recipient_list_params.RecipientListParams,
                ),
            ),
            model=RecipientListResponse,
        )

    def delete(
        self,
        recipient_id: str,
        *,
        account_id: str,
        share_id: str,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> Optional[RecipientDeleteResponse]:
        """
        Deletion is not immediate, an updated share recipient object with a new status
        will be returned.

        Args:
          account_id: Account identifier.

          share_id: Share identifier tag.

          recipient_id: Share Recipient identifier tag.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not share_id:
            raise ValueError(f"Expected a non-empty value for `share_id` but received {share_id!r}")
        if not recipient_id:
            raise ValueError(f"Expected a non-empty value for `recipient_id` but received {recipient_id!r}")
        return self._delete(
            path_template(
                "/accounts/{account_id}/shares/{share_id}/recipients/{recipient_id}",
                account_id=account_id,
                share_id=share_id,
                recipient_id=recipient_id,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[Optional[RecipientDeleteResponse]]._unwrapper,
            ),
            cast_to=cast(Type[Optional[RecipientDeleteResponse]], ResultWrapper[RecipientDeleteResponse]),
        )

    def get(
        self,
        recipient_id: str,
        *,
        account_id: str,
        share_id: str,
        include_resources: bool | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> Optional[RecipientGetResponse]:
        """
        Get share recipient by ID.

        Args:
          account_id: Account identifier.

          share_id: Share identifier tag.

          recipient_id: Share Recipient identifier tag.

          include_resources: Include resources in the response.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not share_id:
            raise ValueError(f"Expected a non-empty value for `share_id` but received {share_id!r}")
        if not recipient_id:
            raise ValueError(f"Expected a non-empty value for `recipient_id` but received {recipient_id!r}")
        return self._get(
            path_template(
                "/accounts/{account_id}/shares/{share_id}/recipients/{recipient_id}",
                account_id=account_id,
                share_id=share_id,
                recipient_id=recipient_id,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {"include_resources": include_resources}, recipient_get_params.RecipientGetParams
                ),
                post_parser=ResultWrapper[Optional[RecipientGetResponse]]._unwrapper,
            ),
            cast_to=cast(Type[Optional[RecipientGetResponse]], ResultWrapper[RecipientGetResponse]),
        )


class AsyncRecipientsResource(AsyncAPIResource):
    @cached_property
    def with_raw_response(self) -> AsyncRecipientsResourceWithRawResponse:
        """
        This property can be used as a prefix for any HTTP method call to return
        the raw response object instead of the parsed content.

        For more information, see https://www.github.com/cloudflare/cloudflare-python#accessing-raw-response-data-eg-headers
        """
        return AsyncRecipientsResourceWithRawResponse(self)

    @cached_property
    def with_streaming_response(self) -> AsyncRecipientsResourceWithStreamingResponse:
        """
        An alternative to `.with_raw_response` that doesn't eagerly read the response body.

        For more information, see https://www.github.com/cloudflare/cloudflare-python#with_streaming_response
        """
        return AsyncRecipientsResourceWithStreamingResponse(self)

    async def create(
        self,
        share_id: str,
        *,
        path_account_id: str,
        body_account_id: str | Omit = omit,
        organization_id: str | Omit = omit,
        recipient_account_id: str | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> Optional[RecipientCreateResponse]:
        """
        Adds a recipient to a resource share, granting them access to the shared
        resources.

        Args:
          path_account_id: Account identifier.

          share_id: Share identifier tag.

          body_account_id: Deprecated alias for `recipient_account_id`. Use `recipient_account_id` instead.
              The body field collided with the URL path parameter of the same name, which
              prevented SDK generators from distinguishing the source account (in the URL)
              from the recipient account (in the body). Both names will continue to be
              accepted until 2027-05-26 (see `x-sunset`).

          organization_id: Organization identifier.

          recipient_account_id: The account that will receive the share.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not path_account_id:
            raise ValueError(f"Expected a non-empty value for `path_account_id` but received {path_account_id!r}")
        if not share_id:
            raise ValueError(f"Expected a non-empty value for `share_id` but received {share_id!r}")
        return await self._post(
            path_template(
                "/accounts/{path_account_id}/shares/{share_id}/recipients",
                path_account_id=path_account_id,
                share_id=share_id,
            ),
            body=await async_maybe_transform(
                {
                    "body_account_id": body_account_id,
                    "organization_id": organization_id,
                    "recipient_account_id": recipient_account_id,
                },
                recipient_create_params.RecipientCreateParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[Optional[RecipientCreateResponse]]._unwrapper,
            ),
            cast_to=cast(Type[Optional[RecipientCreateResponse]], ResultWrapper[RecipientCreateResponse]),
        )

    def list(
        self,
        share_id: str,
        *,
        account_id: str,
        include_resources: bool | Omit = omit,
        page: int | Omit = omit,
        per_page: int | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> AsyncPaginator[RecipientListResponse, AsyncV4PagePaginationArray[RecipientListResponse]]:
        """
        List share recipients by share ID.

        Args:
          account_id: Account identifier.

          share_id: Share identifier tag.

          include_resources: Include resources in the response.

          page: Page number. Defaults to `1` when `per_page` is supplied without `page`. May be
              omitted entirely along with `per_page` to receive a non-paginated response.

          per_page: Number of objects to return per page. Defaults to `20` when `page` is supplied
              without `per_page`. May be omitted entirely along with `page` to receive a
              non-paginated response.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not share_id:
            raise ValueError(f"Expected a non-empty value for `share_id` but received {share_id!r}")
        return self._get_api_list(
            path_template(
                "/accounts/{account_id}/shares/{share_id}/recipients", account_id=account_id, share_id=share_id
            ),
            page=AsyncV4PagePaginationArray[RecipientListResponse],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "include_resources": include_resources,
                        "page": page,
                        "per_page": per_page,
                    },
                    recipient_list_params.RecipientListParams,
                ),
            ),
            model=RecipientListResponse,
        )

    async def delete(
        self,
        recipient_id: str,
        *,
        account_id: str,
        share_id: str,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> Optional[RecipientDeleteResponse]:
        """
        Deletion is not immediate, an updated share recipient object with a new status
        will be returned.

        Args:
          account_id: Account identifier.

          share_id: Share identifier tag.

          recipient_id: Share Recipient identifier tag.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not share_id:
            raise ValueError(f"Expected a non-empty value for `share_id` but received {share_id!r}")
        if not recipient_id:
            raise ValueError(f"Expected a non-empty value for `recipient_id` but received {recipient_id!r}")
        return await self._delete(
            path_template(
                "/accounts/{account_id}/shares/{share_id}/recipients/{recipient_id}",
                account_id=account_id,
                share_id=share_id,
                recipient_id=recipient_id,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                post_parser=ResultWrapper[Optional[RecipientDeleteResponse]]._unwrapper,
            ),
            cast_to=cast(Type[Optional[RecipientDeleteResponse]], ResultWrapper[RecipientDeleteResponse]),
        )

    async def get(
        self,
        recipient_id: str,
        *,
        account_id: str,
        share_id: str,
        include_resources: bool | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> Optional[RecipientGetResponse]:
        """
        Get share recipient by ID.

        Args:
          account_id: Account identifier.

          share_id: Share identifier tag.

          recipient_id: Share Recipient identifier tag.

          include_resources: Include resources in the response.

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not account_id:
            raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
        if not share_id:
            raise ValueError(f"Expected a non-empty value for `share_id` but received {share_id!r}")
        if not recipient_id:
            raise ValueError(f"Expected a non-empty value for `recipient_id` but received {recipient_id!r}")
        return await self._get(
            path_template(
                "/accounts/{account_id}/shares/{share_id}/recipients/{recipient_id}",
                account_id=account_id,
                share_id=share_id,
                recipient_id=recipient_id,
            ),
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=await async_maybe_transform(
                    {"include_resources": include_resources}, recipient_get_params.RecipientGetParams
                ),
                post_parser=ResultWrapper[Optional[RecipientGetResponse]]._unwrapper,
            ),
            cast_to=cast(Type[Optional[RecipientGetResponse]], ResultWrapper[RecipientGetResponse]),
        )


class RecipientsResourceWithRawResponse:
    def __init__(self, recipients: RecipientsResource) -> None:
        self._recipients = recipients

        self.create = to_raw_response_wrapper(
            recipients.create,
        )
        self.list = to_raw_response_wrapper(
            recipients.list,
        )
        self.delete = to_raw_response_wrapper(
            recipients.delete,
        )
        self.get = to_raw_response_wrapper(
            recipients.get,
        )


class AsyncRecipientsResourceWithRawResponse:
    def __init__(self, recipients: AsyncRecipientsResource) -> None:
        self._recipients = recipients

        self.create = async_to_raw_response_wrapper(
            recipients.create,
        )
        self.list = async_to_raw_response_wrapper(
            recipients.list,
        )
        self.delete = async_to_raw_response_wrapper(
            recipients.delete,
        )
        self.get = async_to_raw_response_wrapper(
            recipients.get,
        )


class RecipientsResourceWithStreamingResponse:
    def __init__(self, recipients: RecipientsResource) -> None:
        self._recipients = recipients

        self.create = to_streamed_response_wrapper(
            recipients.create,
        )
        self.list = to_streamed_response_wrapper(
            recipients.list,
        )
        self.delete = to_streamed_response_wrapper(
            recipients.delete,
        )
        self.get = to_streamed_response_wrapper(
            recipients.get,
        )


class AsyncRecipientsResourceWithStreamingResponse:
    def __init__(self, recipients: AsyncRecipientsResource) -> None:
        self._recipients = recipients

        self.create = async_to_streamed_response_wrapper(
            recipients.create,
        )
        self.list = async_to_streamed_response_wrapper(
            recipients.list,
        )
        self.delete = async_to_streamed_response_wrapper(
            recipients.delete,
        )
        self.get = async_to_streamed_response_wrapper(
            recipients.get,
        )
