# This file is auto-generated by oagen. Do not edit.

from __future__ import annotations

from typing import TYPE_CHECKING, List, Optional, Union, cast

if TYPE_CHECKING:
    from .._client import AsyncWorkOSClient, WorkOSClient

from .._types import RequestOptions, enum_value
from .models import EventSchema, EventSchemaVariant
from workos.common.models.pagination_order import PaginationOrder
from .._pagination import AsyncPage, SyncPage


class Events:
    """Events API resources."""

    def __init__(self, client: "WorkOSClient") -> None:
        self._client = client

    def list_events(
        self,
        *,
        limit: Optional[int] = None,
        before: Optional[str] = None,
        after: Optional[str] = None,
        order: Optional[Union[PaginationOrder, str]] = "desc",
        events: Optional[List[str]] = None,
        range_start: Optional[str] = None,
        range_end: Optional[str] = None,
        organization_id: Optional[str] = None,
        request_options: Optional[RequestOptions] = None,
    ) -> SyncPage[EventSchemaVariant]:
        """List events

        List events for the current environment.

        Args:
            limit: Upper limit on the number of objects to return, between `1` and `100`. Defaults to `10`.
            before: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list. For example, if you make a list request and receive 100 objects, ending with `"obj_123"`, your subsequent call can include `before="obj_123"` to fetch a new batch of objects before `"obj_123"`.
            after: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list. For example, if you make a list request and receive 100 objects, ending with `"obj_123"`, your subsequent call can include `after="obj_123"` to fetch a new batch of objects after `"obj_123"`.
            order: Order the results by the creation time. Supported values are `"asc"` (ascending), `"desc"` (descending), and `"normal"` (descending with reversed cursor semantics where `before` fetches older records and `after` fetches newer records). Defaults to `desc`.
            events: Filter events by one or more event types (e.g. `dsync.user.created`).
            range_start: ISO-8601 date string to filter events created after this date.
            range_end: ISO-8601 date string to filter events created before this date.
            organization_id: Filter events by the [Organization](https://workos.com/docs/reference/organization) that the event is associated with.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            SyncPage[EventSchemaVariant]

        Raises:
            BadRequestError: If the request is malformed (400).
            UnprocessableEntityError: If the request data is unprocessable (422).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        params = {
            k: v
            for k, v in {
                "limit": limit,
                "before": before,
                "after": after,
                "order": enum_value(order) if order is not None else None,
                "events": ",".join(str(v) for v in events)
                if events is not None
                else None,
                "range_start": range_start,
                "range_end": range_end,
                "organization_id": organization_id,
            }.items()
            if v is not None
        }
        return cast(
            SyncPage[EventSchemaVariant],
            self._client.request_page(
                method="get",
                path=("events",),
                model=EventSchema,  # type: ignore[arg-type]  # dispatcher; pagination only calls from_dict
                params=params,
                request_options=request_options,
            ),
        )


class AsyncEvents:
    """Events API resources (async)."""

    def __init__(self, client: "AsyncWorkOSClient") -> None:
        self._client = client

    async def list_events(
        self,
        *,
        limit: Optional[int] = None,
        before: Optional[str] = None,
        after: Optional[str] = None,
        order: Optional[Union[PaginationOrder, str]] = "desc",
        events: Optional[List[str]] = None,
        range_start: Optional[str] = None,
        range_end: Optional[str] = None,
        organization_id: Optional[str] = None,
        request_options: Optional[RequestOptions] = None,
    ) -> AsyncPage[EventSchemaVariant]:
        """List events

        List events for the current environment.

        Args:
            limit: Upper limit on the number of objects to return, between `1` and `100`. Defaults to `10`.
            before: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list. For example, if you make a list request and receive 100 objects, ending with `"obj_123"`, your subsequent call can include `before="obj_123"` to fetch a new batch of objects before `"obj_123"`.
            after: An object ID that defines your place in the list. When the ID is not present, you are at the end of the list. For example, if you make a list request and receive 100 objects, ending with `"obj_123"`, your subsequent call can include `after="obj_123"` to fetch a new batch of objects after `"obj_123"`.
            order: Order the results by the creation time. Supported values are `"asc"` (ascending), `"desc"` (descending), and `"normal"` (descending with reversed cursor semantics where `before` fetches older records and `after` fetches newer records). Defaults to `desc`.
            events: Filter events by one or more event types (e.g. `dsync.user.created`).
            range_start: ISO-8601 date string to filter events created after this date.
            range_end: ISO-8601 date string to filter events created before this date.
            organization_id: Filter events by the [Organization](https://workos.com/docs/reference/organization) that the event is associated with.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            AsyncPage[EventSchemaVariant]

        Raises:
            BadRequestError: If the request is malformed (400).
            UnprocessableEntityError: If the request data is unprocessable (422).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        params = {
            k: v
            for k, v in {
                "limit": limit,
                "before": before,
                "after": after,
                "order": enum_value(order) if order is not None else None,
                "events": ",".join(str(v) for v in events)
                if events is not None
                else None,
                "range_start": range_start,
                "range_end": range_end,
                "organization_id": organization_id,
            }.items()
            if v is not None
        }
        return cast(
            AsyncPage[EventSchemaVariant],
            await self._client.request_page(
                method="get",
                path=("events",),
                model=EventSchema,  # type: ignore[arg-type]  # dispatcher; pagination only calls from_dict
                params=params,
                request_options=request_options,
            ),
        )
