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

import typing

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.event_stream_subscribe_events_event_type_enum import EventStreamSubscribeEventsEventTypeEnum
from ..types.event_stream_subscribe_events_response_content import EventStreamSubscribeEventsResponseContent
from .raw_client import AsyncRawEventsClient, RawEventsClient


class EventsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawEventsClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> RawEventsClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawEventsClient
        """
        return self._raw_client

    def subscribe(
        self,
        *,
        from_: typing.Optional[str] = None,
        from_timestamp: typing.Optional[str] = None,
        event_type: typing.Optional[
            typing.Union[
                EventStreamSubscribeEventsEventTypeEnum, typing.Sequence[EventStreamSubscribeEventsEventTypeEnum]
            ]
        ] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.Iterator[EventStreamSubscribeEventsResponseContent]:
        """
        Subscribe to events via Server-Sent Events (SSE)

        Parameters
        ----------
        from_ : typing.Optional[str]
            Opaque token representing position in the stream. If not provided, stream will start from the latest events.

        from_timestamp : typing.Optional[str]
            RFC-3339 timestamp indicating where to start streaming events from. This should only be used on the initial query when a cursor may not be available. Subsequent requests should use the cursor (from) as it will be more accurate.

        event_type : typing.Optional[typing.Union[EventStreamSubscribeEventsEventTypeEnum, typing.Sequence[EventStreamSubscribeEventsEventTypeEnum]]]
            Event type(s) to listen for. Specify multiple times for multiple types (e.g., ?event_type=user.created&event_type=user.updated). If not provided, all event types will be streamed.

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

        Yields
        ------
        typing.Iterator[EventStreamSubscribeEventsResponseContent]
            Event stream successfully established.

        Examples
        --------
        from auth0 import Auth0

        client = Auth0(
            token="YOUR_TOKEN",
        )
        response = client.events.subscribe(
            from_="from",
            from_timestamp="from_timestamp",
            event_type=["connection.created"],
        )
        for chunk in response:
            yield chunk
        """
        with self._raw_client.subscribe(
            from_=from_, from_timestamp=from_timestamp, event_type=event_type, request_options=request_options
        ) as r:
            yield from r.data


class AsyncEventsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawEventsClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawEventsClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawEventsClient
        """
        return self._raw_client

    async def subscribe(
        self,
        *,
        from_: typing.Optional[str] = None,
        from_timestamp: typing.Optional[str] = None,
        event_type: typing.Optional[
            typing.Union[
                EventStreamSubscribeEventsEventTypeEnum, typing.Sequence[EventStreamSubscribeEventsEventTypeEnum]
            ]
        ] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> typing.AsyncIterator[EventStreamSubscribeEventsResponseContent]:
        """
        Subscribe to events via Server-Sent Events (SSE)

        Parameters
        ----------
        from_ : typing.Optional[str]
            Opaque token representing position in the stream. If not provided, stream will start from the latest events.

        from_timestamp : typing.Optional[str]
            RFC-3339 timestamp indicating where to start streaming events from. This should only be used on the initial query when a cursor may not be available. Subsequent requests should use the cursor (from) as it will be more accurate.

        event_type : typing.Optional[typing.Union[EventStreamSubscribeEventsEventTypeEnum, typing.Sequence[EventStreamSubscribeEventsEventTypeEnum]]]
            Event type(s) to listen for. Specify multiple times for multiple types (e.g., ?event_type=user.created&event_type=user.updated). If not provided, all event types will be streamed.

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

        Yields
        ------
        typing.AsyncIterator[EventStreamSubscribeEventsResponseContent]
            Event stream successfully established.

        Examples
        --------
        import asyncio

        from auth0 import AsyncAuth0

        client = AsyncAuth0(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            response = await client.events.subscribe(
                from_="from",
                from_timestamp="from_timestamp",
                event_type=["connection.created"],
            )
            async for chunk in response:
                yield chunk


        asyncio.run(main())
        """
        async with self._raw_client.subscribe(
            from_=from_, from_timestamp=from_timestamp, event_type=event_type, request_options=request_options
        ) as r:
            async for _chunk in r.data:
                yield _chunk
