# Generated by smithplates HTTP codegen. Do not edit.

from __future__ import annotations

from typing import cast

from pydantic import BaseModel, TypeAdapter


def validate_api_model(model_type: type[object], value: object) -> object:
    """Validate wire JSON or header-derived dicts into generated API models."""
    if isinstance(model_type, type) and issubclass(model_type, BaseModel):
        return model_type.model_validate(value)
    return TypeAdapter(model_type).validate_python(value)


def dump_api_model(
    value: object,
    *,
    exclude: set[str] | None = None,
) -> dict[str, object]:
    """Serialize generated API models for HTTP request/response bodies."""
    if isinstance(value, BaseModel):
        return value.model_dump(mode="json", exclude_none=True, exclude=exclude)
    dumped = TypeAdapter(type(value)).dump_python(
        value,
        mode="json",
        exclude_none=True,
        exclude=exclude,
    )
    if not isinstance(dumped, dict):
        msg = f"Expected dict from API model dump, got {type(dumped)!r}"
        raise TypeError(msg)
    return cast(dict[str, object], dumped)
