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

from __future__ import annotations

from typing import TYPE_CHECKING, Any, Dict, Literal, Optional, Union

if TYPE_CHECKING:
    from .._client import AsyncWorkOSClient, WorkOSClient

from .._types import RequestOptions, enum_value
from .models import RadarListEntryAlreadyPresentResponse, RadarStandaloneResponse
from .models import RadarListAction, RadarListType
from workos.common.models.radar_standalone_assess_request_action import (
    RadarStandaloneAssessRequestAction,
)
from workos.common.models.radar_standalone_assess_request_auth_method import (
    RadarStandaloneAssessRequestAuthMethod,
)


class Radar:
    """Radar API resources."""

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

    def create_attempt(
        self,
        *,
        ip_address: str,
        user_agent: str,
        email: str,
        auth_method: Union[RadarStandaloneAssessRequestAuthMethod, str],
        action: Union[RadarStandaloneAssessRequestAction, str],
        signals_id: Optional[str] = None,
        request_options: Optional[RequestOptions] = None,
    ) -> RadarStandaloneResponse:
        """Create an attempt

        Assess a request for risk using the Radar engine and receive a verdict.

        Args:
            ip_address: The IP address of the request to assess.
            user_agent: The user agent string of the request to assess.
            email: The email address of the user making the request.
            auth_method: The authentication method being used.
            action: The action being performed.
            signals_id: An optional Radar signals ID for the request.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            RadarStandaloneResponse

        Raises:
            BadRequestError: If the request is malformed (400).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        body: Dict[str, Any] = {
            k: v
            for k, v in {
                "ip_address": ip_address,
                "user_agent": user_agent,
                "email": email,
                "auth_method": enum_value(auth_method),
                "action": enum_value(action),
                "signals_id": signals_id,
            }.items()
            if v is not None
        }
        return self._client.request(
            method="post",
            path=("radar", "attempts"),
            body=body,
            model=RadarStandaloneResponse,
            request_options=request_options,
        )

    def update_attempt(
        self,
        id: str,
        *,
        challenge_status: Optional[Literal["success"]] = None,
        attempt_status: Optional[Literal["success"]] = None,
        request_options: Optional[RequestOptions] = None,
    ) -> None:
        """Update a Radar attempt

        You may optionally inform Radar that an authentication attempt or challenge was successful using this endpoint. Some Radar controls depend on tracking recent successful attempts, such as impossible travel.

        Args:
            id: The unique identifier of the Radar attempt to update.
            challenge_status: Set to `"success"` to mark the challenge as completed.
            attempt_status: Set to `"success"` to mark the authentication attempt as successful.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Raises:
            BadRequestError: If the request is malformed (400).
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        body: Dict[str, Any] = {
            k: v
            for k, v in {
                "challenge_status": challenge_status,
                "attempt_status": attempt_status,
            }.items()
            if v is not None
        }
        self._client.request(
            method="put",
            path=("radar", "attempts", str(id)),
            body=body,
            request_options=request_options,
        )

    def add_list_entry(
        self,
        type: Union[RadarListType, str],
        action: Union[RadarListAction, str],
        *,
        entry: str,
        request_options: Optional[RequestOptions] = None,
    ) -> RadarListEntryAlreadyPresentResponse:
        """Add an entry to a Radar list

        Add an entry to a Radar list.

        Args:
            type: The type of the Radar list (e.g. ip_address, domain, email).
            action: The list action indicating whether to add the entry to the allow or block list.
            entry: The value to add to the list. Must match the format of the list type (e.g. a valid IP address for `ip_address`, a valid email for `email`).
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            RadarListEntryAlreadyPresentResponse

        Raises:
            BadRequestError: If the request is malformed (400).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        body: Dict[str, Any] = {
            "entry": entry,
        }
        return self._client.request(
            method="post",
            path=("radar", "lists", str(enum_value(type)), str(enum_value(action))),
            body=body,
            model=RadarListEntryAlreadyPresentResponse,
            request_options=request_options,
        )

    def remove_list_entry(
        self,
        type: Union[RadarListType, str],
        action: Union[RadarListAction, str],
        *,
        entry: str,
        request_options: Optional[RequestOptions] = None,
    ) -> None:
        """Remove an entry from a Radar list

        Remove an entry from a Radar list.

        Args:
            type: The type of the Radar list (e.g. ip_address, domain, email).
            action: The list action indicating whether to remove the entry from the allow or block list.
            entry: The value to remove from the list. Must match an existing entry.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Raises:
            BadRequestError: If the request is malformed (400).
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        body: Dict[str, Any] = {
            "entry": entry,
        }
        self._client.request(
            method="delete",
            path=("radar", "lists", str(enum_value(type)), str(enum_value(action))),
            body=body,
            request_options=request_options,
        )


class AsyncRadar:
    """Radar API resources (async)."""

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

    async def create_attempt(
        self,
        *,
        ip_address: str,
        user_agent: str,
        email: str,
        auth_method: Union[RadarStandaloneAssessRequestAuthMethod, str],
        action: Union[RadarStandaloneAssessRequestAction, str],
        signals_id: Optional[str] = None,
        request_options: Optional[RequestOptions] = None,
    ) -> RadarStandaloneResponse:
        """Create an attempt

        Assess a request for risk using the Radar engine and receive a verdict.

        Args:
            ip_address: The IP address of the request to assess.
            user_agent: The user agent string of the request to assess.
            email: The email address of the user making the request.
            auth_method: The authentication method being used.
            action: The action being performed.
            signals_id: An optional Radar signals ID for the request.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            RadarStandaloneResponse

        Raises:
            BadRequestError: If the request is malformed (400).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        body: Dict[str, Any] = {
            k: v
            for k, v in {
                "ip_address": ip_address,
                "user_agent": user_agent,
                "email": email,
                "auth_method": enum_value(auth_method),
                "action": enum_value(action),
                "signals_id": signals_id,
            }.items()
            if v is not None
        }
        return await self._client.request(
            method="post",
            path=("radar", "attempts"),
            body=body,
            model=RadarStandaloneResponse,
            request_options=request_options,
        )

    async def update_attempt(
        self,
        id: str,
        *,
        challenge_status: Optional[Literal["success"]] = None,
        attempt_status: Optional[Literal["success"]] = None,
        request_options: Optional[RequestOptions] = None,
    ) -> None:
        """Update a Radar attempt

        You may optionally inform Radar that an authentication attempt or challenge was successful using this endpoint. Some Radar controls depend on tracking recent successful attempts, such as impossible travel.

        Args:
            id: The unique identifier of the Radar attempt to update.
            challenge_status: Set to `"success"` to mark the challenge as completed.
            attempt_status: Set to `"success"` to mark the authentication attempt as successful.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Raises:
            BadRequestError: If the request is malformed (400).
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        body: Dict[str, Any] = {
            k: v
            for k, v in {
                "challenge_status": challenge_status,
                "attempt_status": attempt_status,
            }.items()
            if v is not None
        }
        await self._client.request(
            method="put",
            path=("radar", "attempts", str(id)),
            body=body,
            request_options=request_options,
        )

    async def add_list_entry(
        self,
        type: Union[RadarListType, str],
        action: Union[RadarListAction, str],
        *,
        entry: str,
        request_options: Optional[RequestOptions] = None,
    ) -> RadarListEntryAlreadyPresentResponse:
        """Add an entry to a Radar list

        Add an entry to a Radar list.

        Args:
            type: The type of the Radar list (e.g. ip_address, domain, email).
            action: The list action indicating whether to add the entry to the allow or block list.
            entry: The value to add to the list. Must match the format of the list type (e.g. a valid IP address for `ip_address`, a valid email for `email`).
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Returns:
            RadarListEntryAlreadyPresentResponse

        Raises:
            BadRequestError: If the request is malformed (400).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        body: Dict[str, Any] = {
            "entry": entry,
        }
        return await self._client.request(
            method="post",
            path=("radar", "lists", str(enum_value(type)), str(enum_value(action))),
            body=body,
            model=RadarListEntryAlreadyPresentResponse,
            request_options=request_options,
        )

    async def remove_list_entry(
        self,
        type: Union[RadarListType, str],
        action: Union[RadarListAction, str],
        *,
        entry: str,
        request_options: Optional[RequestOptions] = None,
    ) -> None:
        """Remove an entry from a Radar list

        Remove an entry from a Radar list.

        Args:
            type: The type of the Radar list (e.g. ip_address, domain, email).
            action: The list action indicating whether to remove the entry from the allow or block list.
            entry: The value to remove from the list. Must match an existing entry.
            request_options: Per-request options. Supports extra_headers, timeout, max_retries, and base_url override.

        Raises:
            BadRequestError: If the request is malformed (400).
            NotFoundError: If the resource is not found (404).
            AuthenticationError: If the API key is invalid (401).
            RateLimitExceededError: If rate limited (429).
            ServerError: If the server returns a 5xx error.
        """
        body: Dict[str, Any] = {
            "entry": entry,
        }
        await self._client.request(
            method="delete",
            path=("radar", "lists", str(enum_value(type)), str(enum_value(action))),
            body=body,
            request_options=request_options,
        )
