"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
# @generated-id: 6153dd0f41bc

from __future__ import annotations
from datetime import datetime
from mistralai.client.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from mistralai.client.utils import validate_const
import pydantic
from pydantic import model_serializer
from pydantic.functional_validators import AfterValidator
from typing import Literal, Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class OAuth2TokenTypedDict(TypedDict):
    access_token: str
    token_type: Literal["Bearer"]
    expires_in: NotRequired[Nullable[int]]
    scope: NotRequired[Nullable[str]]
    refresh_token: NotRequired[Nullable[str]]
    expires_at: NotRequired[Nullable[datetime]]


class OAuth2Token(BaseModel):
    access_token: str

    token_type: Annotated[
        Annotated[
            Optional[Literal["Bearer"]], AfterValidator(validate_const("Bearer"))
        ],
        pydantic.Field(alias="token_type"),
    ] = "Bearer"

    expires_in: OptionalNullable[int] = UNSET

    scope: OptionalNullable[str] = UNSET

    refresh_token: OptionalNullable[str] = UNSET

    expires_at: OptionalNullable[datetime] = UNSET

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            ["token_type", "expires_in", "scope", "refresh_token", "expires_at"]
        )
        nullable_fields = set(["expires_in", "scope", "refresh_token", "expires_at"])
        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))
            is_nullable_and_explicitly_set = (
                k in nullable_fields
                and (self.__pydantic_fields_set__.intersection({n}))  # pylint: disable=no-member
            )

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

        return m


try:
    OAuth2Token.model_rebuild()
except NameError:
    pass
