Skip to content

models

models

Pydantic model generation for cocapi responses

create_dynamic_model

create_dynamic_model(
    response: dict[str, Any], endpoint_type: str
) -> dict[str, Any] | Any

Create a Pydantic model from JSON response.

First tries to match a concrete model from the schema registry. Falls back to dynamic model generation for unknown endpoints.

PARAMETER DESCRIPTION
response

The JSON response data

TYPE: dict[str, Any]

endpoint_type

Type of endpoint (clan, player, etc.)

TYPE: str

RETURNS DESCRIPTION
dict[str, Any] | Any

Either a Pydantic model instance or the original dict

Source code in cocapi/models.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def create_dynamic_model(
    response: dict[str, Any], endpoint_type: str
) -> dict[str, Any] | Any:
    """
    Create a Pydantic model from JSON response.

    First tries to match a concrete model from the schema registry.
    Falls back to dynamic model generation for unknown endpoints.

    Args:
        response: The JSON response data
        endpoint_type: Type of endpoint (clan, player, etc.)

    Returns:
        Either a Pydantic model instance or the original dict
    """
    if not PYDANTIC_AVAILABLE:
        logging.warning("Pydantic not available - returning dict response")
        return response

    if response.get("result") == "error":
        return response

    # 1. Try concrete model from schema registry
    try:
        from .model_registry import resolve_response

        resolved = resolve_response(response, endpoint_type)
        if resolved is not None:
            return resolved
    except ImportError:
        pass  # schemas not installed, fall through to dynamic

    # 2. Fallback to dynamic model generation
    try:
        model_name = _generate_model_name(endpoint_type)
        field_definitions = _analyze_response_structure(response)
        DynamicModel = create_model(model_name, **field_definitions)
        return DynamicModel(**response)

    except Exception as e:
        logging.warning(f"Failed to create dynamic model: {e}")
        return response

validate_pydantic_available

validate_pydantic_available() -> bool

Check if Pydantic is available for dynamic model creation

Source code in cocapi/models.py
122
123
124
def validate_pydantic_available() -> bool:
    """Check if Pydantic is available for dynamic model creation"""
    return PYDANTIC_AVAILABLE

get_pydantic_info

get_pydantic_info() -> dict[str, Any]

Get information about Pydantic availability and version

Source code in cocapi/models.py
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
def get_pydantic_info() -> dict[str, Any]:
    """Get information about Pydantic availability and version"""
    if not PYDANTIC_AVAILABLE:
        return {
            "available": False,
            "version": None,
            "message": "Pydantic not installed. Install with: pip install 'cocapi[pydantic]'",
        }

    try:
        import pydantic

        return {
            "available": True,
            "version": pydantic.VERSION,
            "message": "Pydantic available for dynamic model generation",
        }
    except Exception as e:
        return {
            "available": False,
            "version": None,
            "message": f"Pydantic import error: {e}",
        }