katana_public_api_client.models_pydantic.converters¶
katana_public_api_client.models_pydantic.converters
¶
Batch conversion utilities for attrs <-> Pydantic model conversion.
This module provides convenience functions for converting API responses and collections of models between attrs and Pydantic representations.
Classes¶
Functions¶
batch_convert(attrs_objects, pydantic_class)
¶
Convert a list of attrs objects to Pydantic models.
Parameters:
-
attrs_objects(Sequence[object]) –A sequence of attrs model instances.
-
pydantic_class(type[T]) –The Pydantic model class to convert to.
Returns:
-
list[T]–A list of Pydantic model instances.
Example
Source code in katana_public_api_client/models_pydantic/converters.py
batch_convert_to_attrs(pydantic_objects)
¶
Convert a list of Pydantic models to attrs objects.
Parameters:
-
pydantic_objects(Sequence[KatanaPydanticBase]) –A sequence of Pydantic model instances.
Returns:
Source code in katana_public_api_client/models_pydantic/converters.py
convert_response(response, pydantic_class)
¶
Convert an API response's parsed data to Pydantic models.
This is the recommended way to convert API responses when you want Pydantic models instead of attrs models.
Parameters:
-
response(Response[object] | Response[list[object]] | Response[None]) –The Response object from an API call.
-
pydantic_class(type[T]) –The Pydantic model class to convert to.
Returns:
-
T | list[T] | None–- None if response.parsed is None
-
T | list[T] | None–- A list of Pydantic models if response.parsed is a list
-
T | list[T] | None–- A single Pydantic model otherwise
Example
from katana_public_api_client.api.product import get_all_products
from katana_public_api_client.models_pydantic import Product
from katana_public_api_client.models_pydantic.converters import (
convert_response,
)
response = await get_all_products.asyncio_detailed(client=client)
products = convert_response(response, Product) # list[Product]
Source code in katana_public_api_client/models_pydantic/converters.py
to_attrs(pydantic_obj)
¶
Convert any registered Pydantic object to its attrs equivalent.
This is equivalent to calling pydantic_obj.to_attrs() but provides a consistent functional interface.
Parameters:
-
pydantic_obj(KatanaPydanticBase) –A Pydantic model instance.
Returns:
-
object | None–The corresponding attrs model instance.
Example
Source code in katana_public_api_client/models_pydantic/converters.py
to_pydantic(attrs_obj)
¶
Convert any registered attrs object to its Pydantic equivalent.
This function automatically looks up the correct Pydantic class from the registry.
Parameters:
-
attrs_obj(object) –An attrs model instance.
Returns:
-
KatanaPydanticBase | None–The corresponding Pydantic model instance, or None if the class
-
KatanaPydanticBase | None–is not registered.