Skip to content

APIClasses

cognite.pygen._core.models.api_classes

This module contains classes representing the API classes generated by the SDK generator.

APIClass dataclass

This is the base class for all API classes.

Parameters:

Name Type Description Default
parent_attribute str

The name of the attribute in the client class that this API class is attached.

required
name str

The name of the API class.

required
file_name str

The name of the file where the API class is located.

required
Source code in cognite/pygen/_core/models/api_classes.py
@dataclass(frozen=True)
class APIClass:
    """This is the base class for all API classes.

    Args:
        parent_attribute: The name of the attribute in the client class that this API class is attached.
        name: The name of the API class.
        file_name: The name of the file where the API class is located.

    """

    parent_attribute: str
    name: str
    file_name: str

MultiAPIClass dataclass

This represents a set of APIs which are generated from a single data model.

The motivation for having this class is the case when you want to create one SDK for multiple data models.

Parameters:

Name Type Description Default
sub_apis_by_view_id dict[ViewId, APIClass]

A dictionary mapping view IDs to the corresponding API classes.

required
parent_attribute str

The name of the attribute in the client class that this API class is attached

required
name str

The name of the API class.

required
model DataModel[View]

The data model that the APIs are generated from.

required
Source code in cognite/pygen/_core/models/api_classes.py
@dataclass(frozen=True)
class MultiAPIClass:
    """This represents a set of APIs which are generated from a single data model.

    The motivation for having this class is the case when you want to create one SDK for multiple data models.

    Args:
        sub_apis_by_view_id: A dictionary mapping view IDs to the corresponding API classes.
        parent_attribute: The name of the attribute in the client class that this API class is attached
        name: The name of the API class.
        model: The data model that the APIs are generated from.
    """

    sub_apis_by_view_id: dict[dm.ViewId, APIClass]
    parent_attribute: str
    name: str
    model: dm.DataModel[dm.View]

    @property
    def model_id(self) -> dm.DataModelId:
        return self.model.as_id()

    @classmethod
    def from_data_model(
        cls,
        data_model: dm.DataModel[dm.View],
        api_class_by_view_id: dict[dm.ViewId, APIClass],
        multi_api_class: pygen_config.MultiAPIClassNaming,
    ) -> MultiAPIClass:
        sub_apis: dict[dm.ViewId, APIClass] = {}
        for view in sorted(data_model.views, key=lambda v: (v.name or v.external_id, v.space, v.version)):
            if view.as_id() in api_class_by_view_id:
                sub_apis[view.as_id()] = api_class_by_view_id[view.as_id()]

        data_model_name = data_model.name or data_model.external_id

        return cls(
            sub_apis_by_view_id=sub_apis,
            parent_attribute=create_name(data_model_name, multi_api_class.client_attribute),
            name=f"{create_name(data_model_name, multi_api_class.name)}APIs",
            model=data_model,
        )