Skip to content

Class tigrbl.api._api.Api

tigrbl.api._api.Api

Api(*, engine=None, **router_kwargs)

Bases: APISpec, APIRouter

API router with model and table registries.

Source code in tigrbl/api/_api.py
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
60
def __init__(
    self, *, engine: EngineCfg | None = None, **router_kwargs: Any
) -> None:
    # Manually initialize fields from ``APISpec`` so ``repr`` and other
    # dataclass-generated helpers have the expected attributes, while also
    # preparing mutable containers used at runtime.
    self.name = getattr(self, "NAME", "api")
    self.prefix = self.PREFIX
    self.engine = engine if engine is not None else getattr(self, "ENGINE", None)
    self.tags = list(getattr(self, "TAGS", []))
    self.ops = tuple(getattr(self, "OPS", ()))
    self.schemas = SimpleNamespace()
    self.hooks = SimpleNamespace()
    self.security_deps = tuple(getattr(self, "SECURITY_DEPS", ()))
    self.deps = tuple(getattr(self, "DEPS", ()))
    self.response = getattr(self, "RESPONSE", None)
    # ``models`` is expected to be a dict at runtime for registry lookups.
    self.models: dict[str, type] = {}

    ApiRouter.__init__(
        self,
        prefix=self.PREFIX,
        tags=self.tags,
        dependencies=list(self.security_deps) + list(self.deps),
        **router_kwargs,
    )

    # namespace containers
    self.tables: dict[str, Any] = {}

    _engine_ctx = engine if engine is not None else getattr(self, "ENGINE", None)
    if _engine_ctx is not None:
        _resolver.register_api(self, _engine_ctx)
        _resolver.resolve_provider(api=self)

MODELS class-attribute instance-attribute

MODELS = ()

TABLES class-attribute instance-attribute

TABLES = ()

name instance-attribute

name = getattr(self, 'NAME', 'api')

prefix instance-attribute

prefix = PREFIX

engine instance-attribute

engine = (
    engine
    if engine is not None
    else getattr(self, "ENGINE", None)
)

tags instance-attribute

tags = list(getattr(self, 'TAGS', []))

ops instance-attribute

ops = tuple(getattr(self, 'OPS', ()))

schemas instance-attribute

schemas = SimpleNamespace()

hooks instance-attribute

hooks = SimpleNamespace()

security_deps instance-attribute

security_deps = tuple(getattr(self, 'SECURITY_DEPS', ()))

deps instance-attribute

deps = tuple(getattr(self, 'DEPS', ()))

response instance-attribute

response = getattr(self, 'RESPONSE', None)

models instance-attribute

models = {}

tables instance-attribute

tables = {}

install_engines

install_engines(*, api=None, models=None)
Source code in tigrbl/api/_api.py
62
63
64
65
66
67
68
69
70
71
72
def install_engines(
    self, *, api: Any = None, models: tuple[Any, ...] | None = None
) -> None:
    # If class declared APIS/MODELS, use them unless explicit args are passed.
    apis = (api,) if api is not None else self.APIS
    models = models if models is not None else self.MODELS
    if apis:
        for a in apis:
            install_from_objects(app=self, api=a, models=models)
    else:
        install_from_objects(app=self, api=None, models=models)