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

from __future__ import annotations

import httpx

from ..types import oauth_connection_list_params
from .._types import Body, Omit, Query, Headers, NoneType, NotGiven, omit, not_given
from .._utils import 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 ..pagination import SyncCursor, AsyncCursor
from .._base_client import AsyncPaginator, make_request_options
from ..types.oauth_connection_list_response import OAuthConnectionListResponse
from ..types.oauth_connection_retrieve_response import OAuthConnectionRetrieveResponse
from ..types.oauth_connection_get_token_response import OAuthConnectionGetTokenResponse

__all__ = ["OAuthConnectionsResource", "AsyncOAuthConnectionsResource"]


class OAuthConnectionsResource(SyncAPIResource):
    @cached_property
    def with_raw_response(self) -> OAuthConnectionsResourceWithRawResponse:
        """
        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/reminix-ai/reminix-python#accessing-raw-response-data-eg-headers
        """
        return OAuthConnectionsResourceWithRawResponse(self)

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

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

    def retrieve(
        self,
        name: 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,
    ) -> OAuthConnectionRetrieveResponse:
        """
        Get OAuth connection by name

        Args:
          name: Connection name (slug)

          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 name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        return self._get(
            f"/oauth-connections/{name}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=OAuthConnectionRetrieveResponse,
        )

    def list(
        self,
        *,
        cursor: str | Omit = omit,
        limit: int | Omit = omit,
        provider: 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,
    ) -> SyncCursor[OAuthConnectionListResponse]:
        """
        List OAuth connections

        Args:
          cursor: Pagination cursor

          limit: Maximum number of items to return (1-100)

          provider: Filter by provider slug

          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
        """
        return self._get_api_list(
            "/oauth-connections",
            page=SyncCursor[OAuthConnectionListResponse],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "cursor": cursor,
                        "limit": limit,
                        "provider": provider,
                    },
                    oauth_connection_list_params.OAuthConnectionListParams,
                ),
            ),
            model=OAuthConnectionListResponse,
        )

    def delete(
        self,
        name: 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,
    ) -> None:
        """
        Delete an OAuth connection

        Args:
          name: Connection name (slug)

          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 name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        extra_headers = {"Accept": "*/*", **(extra_headers or {})}
        return self._delete(
            f"/oauth-connections/{name}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=NoneType,
        )

    def get_token(
        self,
        name: 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,
    ) -> OAuthConnectionGetTokenResponse:
        """
        Get a fresh access token for a connection

        Args:
          name: Connection name (slug)

          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 name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        return self._get(
            f"/oauth-connections/{name}/token",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=OAuthConnectionGetTokenResponse,
        )


class AsyncOAuthConnectionsResource(AsyncAPIResource):
    @cached_property
    def with_raw_response(self) -> AsyncOAuthConnectionsResourceWithRawResponse:
        """
        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/reminix-ai/reminix-python#accessing-raw-response-data-eg-headers
        """
        return AsyncOAuthConnectionsResourceWithRawResponse(self)

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

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

    async def retrieve(
        self,
        name: 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,
    ) -> OAuthConnectionRetrieveResponse:
        """
        Get OAuth connection by name

        Args:
          name: Connection name (slug)

          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 name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        return await self._get(
            f"/oauth-connections/{name}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=OAuthConnectionRetrieveResponse,
        )

    def list(
        self,
        *,
        cursor: str | Omit = omit,
        limit: int | Omit = omit,
        provider: 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,
    ) -> AsyncPaginator[OAuthConnectionListResponse, AsyncCursor[OAuthConnectionListResponse]]:
        """
        List OAuth connections

        Args:
          cursor: Pagination cursor

          limit: Maximum number of items to return (1-100)

          provider: Filter by provider slug

          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
        """
        return self._get_api_list(
            "/oauth-connections",
            page=AsyncCursor[OAuthConnectionListResponse],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "cursor": cursor,
                        "limit": limit,
                        "provider": provider,
                    },
                    oauth_connection_list_params.OAuthConnectionListParams,
                ),
            ),
            model=OAuthConnectionListResponse,
        )

    async def delete(
        self,
        name: 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,
    ) -> None:
        """
        Delete an OAuth connection

        Args:
          name: Connection name (slug)

          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 name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        extra_headers = {"Accept": "*/*", **(extra_headers or {})}
        return await self._delete(
            f"/oauth-connections/{name}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=NoneType,
        )

    async def get_token(
        self,
        name: 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,
    ) -> OAuthConnectionGetTokenResponse:
        """
        Get a fresh access token for a connection

        Args:
          name: Connection name (slug)

          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 name:
            raise ValueError(f"Expected a non-empty value for `name` but received {name!r}")
        return await self._get(
            f"/oauth-connections/{name}/token",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=OAuthConnectionGetTokenResponse,
        )


class OAuthConnectionsResourceWithRawResponse:
    def __init__(self, oauth_connections: OAuthConnectionsResource) -> None:
        self._oauth_connections = oauth_connections

        self.retrieve = to_raw_response_wrapper(
            oauth_connections.retrieve,
        )
        self.list = to_raw_response_wrapper(
            oauth_connections.list,
        )
        self.delete = to_raw_response_wrapper(
            oauth_connections.delete,
        )
        self.get_token = to_raw_response_wrapper(
            oauth_connections.get_token,
        )


class AsyncOAuthConnectionsResourceWithRawResponse:
    def __init__(self, oauth_connections: AsyncOAuthConnectionsResource) -> None:
        self._oauth_connections = oauth_connections

        self.retrieve = async_to_raw_response_wrapper(
            oauth_connections.retrieve,
        )
        self.list = async_to_raw_response_wrapper(
            oauth_connections.list,
        )
        self.delete = async_to_raw_response_wrapper(
            oauth_connections.delete,
        )
        self.get_token = async_to_raw_response_wrapper(
            oauth_connections.get_token,
        )


class OAuthConnectionsResourceWithStreamingResponse:
    def __init__(self, oauth_connections: OAuthConnectionsResource) -> None:
        self._oauth_connections = oauth_connections

        self.retrieve = to_streamed_response_wrapper(
            oauth_connections.retrieve,
        )
        self.list = to_streamed_response_wrapper(
            oauth_connections.list,
        )
        self.delete = to_streamed_response_wrapper(
            oauth_connections.delete,
        )
        self.get_token = to_streamed_response_wrapper(
            oauth_connections.get_token,
        )


class AsyncOAuthConnectionsResourceWithStreamingResponse:
    def __init__(self, oauth_connections: AsyncOAuthConnectionsResource) -> None:
        self._oauth_connections = oauth_connections

        self.retrieve = async_to_streamed_response_wrapper(
            oauth_connections.retrieve,
        )
        self.list = async_to_streamed_response_wrapper(
            oauth_connections.list,
        )
        self.delete = async_to_streamed_response_wrapper(
            oauth_connections.delete,
        )
        self.get_token = async_to_streamed_response_wrapper(
            oauth_connections.get_token,
        )
