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

from __future__ import annotations
from dataclasses import dataclass, field
from dub.models.errors import DubError
from dub.types import BaseModel, UNSET_SENTINEL
from enum import Enum
import httpx
from pydantic import model_serializer
from typing import Optional
from typing_extensions import NotRequired, TypedDict


class NotFoundCode(str, Enum):
    r"""A short code indicating the error code returned."""

    NOT_FOUND = "not_found"


class NotFoundErrorTypedDict(TypedDict):
    code: NotFoundCode
    r"""A short code indicating the error code returned."""
    message: str
    r"""A human readable explanation of what went wrong."""
    doc_url: NotRequired[str]
    r"""A link to our documentation with more details about this error code"""


class NotFoundError(BaseModel):
    code: NotFoundCode
    r"""A short code indicating the error code returned."""

    message: str
    r"""A human readable explanation of what went wrong."""

    doc_url: Optional[str] = None
    r"""A link to our documentation with more details about this error code"""

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

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

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

        return m


class NotFoundData(BaseModel):
    error: NotFoundError


@dataclass(unsafe_hash=True)
class NotFound(DubError):
    r"""The server cannot find the requested resource."""

    data: NotFoundData = field(hash=False)

    def __init__(
        self,
        data: NotFoundData,
        raw_response: httpx.Response,
        body: Optional[str] = None,
    ):
        fallback = body or raw_response.text
        message = str(data.error.message) or fallback
        super().__init__(message, raw_response, body)
        object.__setattr__(self, "data", data)
