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

from __future__ import annotations
from .apikeyauth import APIKeyAuth, APIKeyAuthTypedDict
from .oauth2tokenauth import OAuth2TokenAuth, OAuth2TokenAuthTypedDict
from .toolconfiguration import ToolConfiguration, ToolConfigurationTypedDict
from functools import partial
from mistralai.client.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from mistralai.client.utils import validate_const
from mistralai.client.utils.unions import parse_open_union
import pydantic
from pydantic import ConfigDict, model_serializer
from pydantic.functional_validators import AfterValidator, BeforeValidator
from typing import Any, Literal, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


AuthorizationTypedDict = TypeAliasType(
    "AuthorizationTypedDict", Union[OAuth2TokenAuthTypedDict, APIKeyAuthTypedDict]
)


class UnknownAuthorization(BaseModel):
    r"""A Authorization variant the SDK doesn't recognize. Preserves the raw payload."""

    type: Literal["UNKNOWN"] = "UNKNOWN"
    raw: Any
    is_unknown: Literal[True] = True

    model_config = ConfigDict(frozen=True)


_AUTHORIZATION_VARIANTS: dict[str, Any] = {
    "api-key": APIKeyAuth,
    "oauth2-token": OAuth2TokenAuth,
}


Authorization = Annotated[
    Union[APIKeyAuth, OAuth2TokenAuth, UnknownAuthorization],
    BeforeValidator(
        partial(
            parse_open_union,
            disc_key="type",
            variants=_AUTHORIZATION_VARIANTS,
            unknown_cls=UnknownAuthorization,
            union_name="Authorization",
        )
    ),
]


class CustomConnectorTypedDict(TypedDict):
    connector_id: str
    type: Literal["connector"]
    authorization: NotRequired[Nullable[AuthorizationTypedDict]]
    tool_configuration: NotRequired[Nullable[ToolConfigurationTypedDict]]


class CustomConnector(BaseModel):
    connector_id: str

    type: Annotated[
        Annotated[Literal["connector"], AfterValidator(validate_const("connector"))],
        pydantic.Field(alias="type"),
    ] = "connector"

    authorization: OptionalNullable[Authorization] = UNSET

    tool_configuration: OptionalNullable[ToolConfiguration] = UNSET

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["authorization", "tool_configuration"])
        nullable_fields = set(["authorization", "tool_configuration"])
        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:
    CustomConnector.model_rebuild()
except NameError:
    pass
