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

from ..core.client_wrapper import SyncClientWrapper
import typing
from .types.audit_list_events_request_filter_ops_context_actor_type import (
    AuditListEventsRequestFilterOpsContextActorType,
)
from .types.audit_list_events_request_filter_ops_context_access_type import (
    AuditListEventsRequestFilterOpsContextAccessType,
)
from .types.audit_list_events_request_filter_ops_context_auth_mode import AuditListEventsRequestFilterOpsContextAuthMode
from .types.audit_list_events_request_filter_ops_action_type import AuditListEventsRequestFilterOpsActionType
from .types.audit_list_events_request_filter_ops_resource_type import AuditListEventsRequestFilterOpsResourceType
from .types.audit_list_events_request_sort_ops_order_by import AuditListEventsRequestSortOpsOrderBy
from ..core.request_options import RequestOptions
from ..types.v_1_audit_response import V1AuditResponse
from ..core.pydantic_utilities import parse_obj_as
from ..errors.not_found_error import NotFoundError
from json.decoder import JSONDecodeError
from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper


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

    def list_events(
        self,
        *,
        filter_ops_account_id: str,
        filter_ops_context_change_id: typing.Optional[str] = None,
        filter_ops_context_request_id: typing.Optional[str] = None,
        filter_ops_context_trace_id: typing.Optional[str] = None,
        filter_ops_context_session_id: typing.Optional[str] = None,
        filter_ops_context_actor: typing.Optional[str] = None,
        filter_ops_context_actor_type: typing.Optional[AuditListEventsRequestFilterOpsContextActorType] = None,
        filter_ops_context_access_type: typing.Optional[AuditListEventsRequestFilterOpsContextAccessType] = None,
        filter_ops_context_ip_address: typing.Optional[str] = None,
        filter_ops_context_origin: typing.Optional[str] = None,
        filter_ops_context_auth_mode: typing.Optional[AuditListEventsRequestFilterOpsContextAuthMode] = None,
        filter_ops_context_jwt_id: typing.Optional[str] = None,
        filter_ops_context_bearer_token_context_id: typing.Optional[str] = None,
        filter_ops_parent_account_id: typing.Optional[str] = None,
        filter_ops_workspace_id: typing.Optional[str] = None,
        filter_ops_vault_id: typing.Optional[str] = None,
        filter_ops_resource_i_ds: typing.Optional[str] = None,
        filter_ops_action_type: typing.Optional[AuditListEventsRequestFilterOpsActionType] = None,
        filter_ops_resource_type: typing.Optional[AuditListEventsRequestFilterOpsResourceType] = None,
        filter_ops_tags: typing.Optional[str] = None,
        filter_ops_response_code: typing.Optional[int] = None,
        filter_ops_start_time: typing.Optional[str] = None,
        filter_ops_end_time: typing.Optional[str] = None,
        filter_ops_api_name: typing.Optional[str] = None,
        filter_ops_response_message: typing.Optional[str] = None,
        filter_ops_http_method: typing.Optional[str] = None,
        filter_ops_http_uri: typing.Optional[str] = None,
        sort_ops_sort_by: typing.Optional[str] = None,
        sort_ops_order_by: typing.Optional[AuditListEventsRequestSortOpsOrderBy] = None,
        after_ops_timestamp: typing.Optional[str] = None,
        after_ops_change_id: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        offset: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> V1AuditResponse:
        """
        Lists audit events that match query parameters.

        Parameters
        ----------
        filter_ops_account_id : str
            Resources with the specified account ID.

        filter_ops_context_change_id : typing.Optional[str]
            ID for the audit event.

        filter_ops_context_request_id : typing.Optional[str]
            ID for the request that caused the event.

        filter_ops_context_trace_id : typing.Optional[str]
            ID for the request set by the service that received the request.

        filter_ops_context_session_id : typing.Optional[str]
            ID for the session in which the request was sent.

        filter_ops_context_actor : typing.Optional[str]
            Member who sent the request. Depending on `actorType`, this may be a user ID or a service account ID.

        filter_ops_context_actor_type : typing.Optional[AuditListEventsRequestFilterOpsContextActorType]
            Type of member who sent the request.

        filter_ops_context_access_type : typing.Optional[AuditListEventsRequestFilterOpsContextAccessType]
            Type of access for the request.

        filter_ops_context_ip_address : typing.Optional[str]
            IP Address of the client that made the request.

        filter_ops_context_origin : typing.Optional[str]
            HTTP Origin request header (including scheme, hostname, and port) of the request.

        filter_ops_context_auth_mode : typing.Optional[AuditListEventsRequestFilterOpsContextAuthMode]
            Authentication mode the `actor` used.

        filter_ops_context_jwt_id : typing.Optional[str]
            ID of the JWT token.

        filter_ops_context_bearer_token_context_id : typing.Optional[str]
            Embedded User Context.

        filter_ops_parent_account_id : typing.Optional[str]
            Resources with the specified parent account ID.

        filter_ops_workspace_id : typing.Optional[str]
            Resources with the specified workspace ID.

        filter_ops_vault_id : typing.Optional[str]
            Resources with the specified vault ID.

        filter_ops_resource_i_ds : typing.Optional[str]
            Resources with a specified ID. If a resource matches at least one ID, the associated event is returned. Format is a comma-separated list of "\<resourceType\>/\<resourceID\>". For example, "VAULT/12345, USER/67890".

        filter_ops_action_type : typing.Optional[AuditListEventsRequestFilterOpsActionType]
            Events with the specified action type.

        filter_ops_resource_type : typing.Optional[AuditListEventsRequestFilterOpsResourceType]
            Resources with the specified type.

        filter_ops_tags : typing.Optional[str]
            Events with associated tags. If an event matches at least one tag, the event is returned. Comma-separated list. For example, "login, get".

        filter_ops_response_code : typing.Optional[int]
            HTTP response code of the request.

        filter_ops_start_time : typing.Optional[str]
            Start timestamp for the query, in SQL format.

        filter_ops_end_time : typing.Optional[str]
            End timestamp for the query, in SQL format.

        filter_ops_api_name : typing.Optional[str]
            Name of the API called in the request.

        filter_ops_response_message : typing.Optional[str]
            Response message of the request.

        filter_ops_http_method : typing.Optional[str]
            HTTP method of the request.

        filter_ops_http_uri : typing.Optional[str]
            HTTP URI of the request.

        sort_ops_sort_by : typing.Optional[str]
            Fully-qualified field by which to sort results. Field names should be in camel case (for example, "capitalization.camelCase").

        sort_ops_order_by : typing.Optional[AuditListEventsRequestSortOpsOrderBy]
            Ascending or descending ordering of results.

        after_ops_timestamp : typing.Optional[str]
            Timestamp provided in the previous audit response's `nextOps` attribute. An alternate way to manage response pagination. Can't be used with `sortOps` or `offset`. For the first request in a series of audit requests, leave blank.

        after_ops_change_id : typing.Optional[str]
            Change ID provided in the previous audit response's `nextOps` attribute. An alternate way to manage response pagination. Can't be used with `sortOps` or `offset`. For the first request in a series of audit requests, leave blank.

        limit : typing.Optional[int]
            Number of results to return.

        offset : typing.Optional[int]
            Record position at which to start returning results.

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

        Returns
        -------
        V1AuditResponse
            A successful response.

        Examples
        --------
        from skyflow import Skyflow

        client = Skyflow(
            api_key="YOUR_API_KEY",
        )
        client.audit.list_events(
            filter_ops_account_id="filterOps.accountID",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "v1/audit/events",
            method="GET",
            params={
                "filterOps.context.changeID": filter_ops_context_change_id,
                "filterOps.context.requestID": filter_ops_context_request_id,
                "filterOps.context.traceID": filter_ops_context_trace_id,
                "filterOps.context.sessionID": filter_ops_context_session_id,
                "filterOps.context.actor": filter_ops_context_actor,
                "filterOps.context.actorType": filter_ops_context_actor_type,
                "filterOps.context.accessType": filter_ops_context_access_type,
                "filterOps.context.ipAddress": filter_ops_context_ip_address,
                "filterOps.context.origin": filter_ops_context_origin,
                "filterOps.context.authMode": filter_ops_context_auth_mode,
                "filterOps.context.jwtID": filter_ops_context_jwt_id,
                "filterOps.context.bearerTokenContextID": filter_ops_context_bearer_token_context_id,
                "filterOps.parentAccountID": filter_ops_parent_account_id,
                "filterOps.accountID": filter_ops_account_id,
                "filterOps.workspaceID": filter_ops_workspace_id,
                "filterOps.vaultID": filter_ops_vault_id,
                "filterOps.resourceIDs": filter_ops_resource_i_ds,
                "filterOps.actionType": filter_ops_action_type,
                "filterOps.resourceType": filter_ops_resource_type,
                "filterOps.tags": filter_ops_tags,
                "filterOps.responseCode": filter_ops_response_code,
                "filterOps.startTime": filter_ops_start_time,
                "filterOps.endTime": filter_ops_end_time,
                "filterOps.apiName": filter_ops_api_name,
                "filterOps.responseMessage": filter_ops_response_message,
                "filterOps.httpMethod": filter_ops_http_method,
                "filterOps.httpURI": filter_ops_http_uri,
                "sortOps.sortBy": sort_ops_sort_by,
                "sortOps.orderBy": sort_ops_order_by,
                "afterOps.timestamp": after_ops_timestamp,
                "afterOps.changeID": after_ops_change_id,
                "limit": limit,
                "offset": offset,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    V1AuditResponse,
                    parse_obj_as(
                        type_=V1AuditResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Dict[str, typing.Optional[typing.Any]],
                        parse_obj_as(
                            type_=typing.Dict[str, typing.Optional[typing.Any]],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)


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

    async def list_events(
        self,
        *,
        filter_ops_account_id: str,
        filter_ops_context_change_id: typing.Optional[str] = None,
        filter_ops_context_request_id: typing.Optional[str] = None,
        filter_ops_context_trace_id: typing.Optional[str] = None,
        filter_ops_context_session_id: typing.Optional[str] = None,
        filter_ops_context_actor: typing.Optional[str] = None,
        filter_ops_context_actor_type: typing.Optional[AuditListEventsRequestFilterOpsContextActorType] = None,
        filter_ops_context_access_type: typing.Optional[AuditListEventsRequestFilterOpsContextAccessType] = None,
        filter_ops_context_ip_address: typing.Optional[str] = None,
        filter_ops_context_origin: typing.Optional[str] = None,
        filter_ops_context_auth_mode: typing.Optional[AuditListEventsRequestFilterOpsContextAuthMode] = None,
        filter_ops_context_jwt_id: typing.Optional[str] = None,
        filter_ops_context_bearer_token_context_id: typing.Optional[str] = None,
        filter_ops_parent_account_id: typing.Optional[str] = None,
        filter_ops_workspace_id: typing.Optional[str] = None,
        filter_ops_vault_id: typing.Optional[str] = None,
        filter_ops_resource_i_ds: typing.Optional[str] = None,
        filter_ops_action_type: typing.Optional[AuditListEventsRequestFilterOpsActionType] = None,
        filter_ops_resource_type: typing.Optional[AuditListEventsRequestFilterOpsResourceType] = None,
        filter_ops_tags: typing.Optional[str] = None,
        filter_ops_response_code: typing.Optional[int] = None,
        filter_ops_start_time: typing.Optional[str] = None,
        filter_ops_end_time: typing.Optional[str] = None,
        filter_ops_api_name: typing.Optional[str] = None,
        filter_ops_response_message: typing.Optional[str] = None,
        filter_ops_http_method: typing.Optional[str] = None,
        filter_ops_http_uri: typing.Optional[str] = None,
        sort_ops_sort_by: typing.Optional[str] = None,
        sort_ops_order_by: typing.Optional[AuditListEventsRequestSortOpsOrderBy] = None,
        after_ops_timestamp: typing.Optional[str] = None,
        after_ops_change_id: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        offset: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> V1AuditResponse:
        """
        Lists audit events that match query parameters.

        Parameters
        ----------
        filter_ops_account_id : str
            Resources with the specified account ID.

        filter_ops_context_change_id : typing.Optional[str]
            ID for the audit event.

        filter_ops_context_request_id : typing.Optional[str]
            ID for the request that caused the event.

        filter_ops_context_trace_id : typing.Optional[str]
            ID for the request set by the service that received the request.

        filter_ops_context_session_id : typing.Optional[str]
            ID for the session in which the request was sent.

        filter_ops_context_actor : typing.Optional[str]
            Member who sent the request. Depending on `actorType`, this may be a user ID or a service account ID.

        filter_ops_context_actor_type : typing.Optional[AuditListEventsRequestFilterOpsContextActorType]
            Type of member who sent the request.

        filter_ops_context_access_type : typing.Optional[AuditListEventsRequestFilterOpsContextAccessType]
            Type of access for the request.

        filter_ops_context_ip_address : typing.Optional[str]
            IP Address of the client that made the request.

        filter_ops_context_origin : typing.Optional[str]
            HTTP Origin request header (including scheme, hostname, and port) of the request.

        filter_ops_context_auth_mode : typing.Optional[AuditListEventsRequestFilterOpsContextAuthMode]
            Authentication mode the `actor` used.

        filter_ops_context_jwt_id : typing.Optional[str]
            ID of the JWT token.

        filter_ops_context_bearer_token_context_id : typing.Optional[str]
            Embedded User Context.

        filter_ops_parent_account_id : typing.Optional[str]
            Resources with the specified parent account ID.

        filter_ops_workspace_id : typing.Optional[str]
            Resources with the specified workspace ID.

        filter_ops_vault_id : typing.Optional[str]
            Resources with the specified vault ID.

        filter_ops_resource_i_ds : typing.Optional[str]
            Resources with a specified ID. If a resource matches at least one ID, the associated event is returned. Format is a comma-separated list of "\<resourceType\>/\<resourceID\>". For example, "VAULT/12345, USER/67890".

        filter_ops_action_type : typing.Optional[AuditListEventsRequestFilterOpsActionType]
            Events with the specified action type.

        filter_ops_resource_type : typing.Optional[AuditListEventsRequestFilterOpsResourceType]
            Resources with the specified type.

        filter_ops_tags : typing.Optional[str]
            Events with associated tags. If an event matches at least one tag, the event is returned. Comma-separated list. For example, "login, get".

        filter_ops_response_code : typing.Optional[int]
            HTTP response code of the request.

        filter_ops_start_time : typing.Optional[str]
            Start timestamp for the query, in SQL format.

        filter_ops_end_time : typing.Optional[str]
            End timestamp for the query, in SQL format.

        filter_ops_api_name : typing.Optional[str]
            Name of the API called in the request.

        filter_ops_response_message : typing.Optional[str]
            Response message of the request.

        filter_ops_http_method : typing.Optional[str]
            HTTP method of the request.

        filter_ops_http_uri : typing.Optional[str]
            HTTP URI of the request.

        sort_ops_sort_by : typing.Optional[str]
            Fully-qualified field by which to sort results. Field names should be in camel case (for example, "capitalization.camelCase").

        sort_ops_order_by : typing.Optional[AuditListEventsRequestSortOpsOrderBy]
            Ascending or descending ordering of results.

        after_ops_timestamp : typing.Optional[str]
            Timestamp provided in the previous audit response's `nextOps` attribute. An alternate way to manage response pagination. Can't be used with `sortOps` or `offset`. For the first request in a series of audit requests, leave blank.

        after_ops_change_id : typing.Optional[str]
            Change ID provided in the previous audit response's `nextOps` attribute. An alternate way to manage response pagination. Can't be used with `sortOps` or `offset`. For the first request in a series of audit requests, leave blank.

        limit : typing.Optional[int]
            Number of results to return.

        offset : typing.Optional[int]
            Record position at which to start returning results.

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

        Returns
        -------
        V1AuditResponse
            A successful response.

        Examples
        --------
        import asyncio

        from skyflow import AsyncSkyflow

        client = AsyncSkyflow(
            api_key="YOUR_API_KEY",
        )


        async def main() -> None:
            await client.audit.list_events(
                filter_ops_account_id="filterOps.accountID",
            )


        asyncio.run(main())
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v1/audit/events",
            method="GET",
            params={
                "filterOps.context.changeID": filter_ops_context_change_id,
                "filterOps.context.requestID": filter_ops_context_request_id,
                "filterOps.context.traceID": filter_ops_context_trace_id,
                "filterOps.context.sessionID": filter_ops_context_session_id,
                "filterOps.context.actor": filter_ops_context_actor,
                "filterOps.context.actorType": filter_ops_context_actor_type,
                "filterOps.context.accessType": filter_ops_context_access_type,
                "filterOps.context.ipAddress": filter_ops_context_ip_address,
                "filterOps.context.origin": filter_ops_context_origin,
                "filterOps.context.authMode": filter_ops_context_auth_mode,
                "filterOps.context.jwtID": filter_ops_context_jwt_id,
                "filterOps.context.bearerTokenContextID": filter_ops_context_bearer_token_context_id,
                "filterOps.parentAccountID": filter_ops_parent_account_id,
                "filterOps.accountID": filter_ops_account_id,
                "filterOps.workspaceID": filter_ops_workspace_id,
                "filterOps.vaultID": filter_ops_vault_id,
                "filterOps.resourceIDs": filter_ops_resource_i_ds,
                "filterOps.actionType": filter_ops_action_type,
                "filterOps.resourceType": filter_ops_resource_type,
                "filterOps.tags": filter_ops_tags,
                "filterOps.responseCode": filter_ops_response_code,
                "filterOps.startTime": filter_ops_start_time,
                "filterOps.endTime": filter_ops_end_time,
                "filterOps.apiName": filter_ops_api_name,
                "filterOps.responseMessage": filter_ops_response_message,
                "filterOps.httpMethod": filter_ops_http_method,
                "filterOps.httpURI": filter_ops_http_uri,
                "sortOps.sortBy": sort_ops_sort_by,
                "sortOps.orderBy": sort_ops_order_by,
                "afterOps.timestamp": after_ops_timestamp,
                "afterOps.changeID": after_ops_change_id,
                "limit": limit,
                "offset": offset,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                return typing.cast(
                    V1AuditResponse,
                    parse_obj_as(
                        type_=V1AuditResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
            if _response.status_code == 404:
                raise NotFoundError(
                    typing.cast(
                        typing.Dict[str, typing.Optional[typing.Any]],
                        parse_obj_as(
                            type_=typing.Dict[str, typing.Optional[typing.Any]],  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)
