"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from __future__ import annotations
from pydantic import model_serializer
from typing import Optional
from typing_extensions import NotRequired, TypedDict
from unkey.py.types import BaseModel, UNSET_SENTINEL


class KeysVerifyKeyRatelimitTypedDict(TypedDict):
    name: str
    r"""References an existing ratelimit by its name. Key Ratelimits will take precedence over identifier-based limits."""
    cost: NotRequired[int]
    r"""Optionally override how expensive this operation is and how many tokens are deducted from the current limit."""
    limit: NotRequired[int]
    r"""Optionally override the maximum number of requests allowed within the specified interval."""
    duration: NotRequired[int]
    r"""Optionally override the duration of the rate limit window duration."""


class KeysVerifyKeyRatelimit(BaseModel):
    name: str
    r"""References an existing ratelimit by its name. Key Ratelimits will take precedence over identifier-based limits."""

    cost: Optional[int] = 1
    r"""Optionally override how expensive this operation is and how many tokens are deducted from the current limit."""

    limit: Optional[int] = None
    r"""Optionally override the maximum number of requests allowed within the specified interval."""

    duration: Optional[int] = None
    r"""Optionally override the duration of the rate limit window duration."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["cost", "limit", "duration"])
        serialized = handler(self)
        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k, serialized.get(n))

            if val != UNSET_SENTINEL:
                if val is not None or k not in optional_fields:
                    m[k] = val

        return m
