Skip to main content

Ports-and-adapters bootstrap decorators, autoload utilities, and standalone authz helpers.

Project description

iisi-app-core

iisi-app-core is a small Python library for two things:

  • bootstrapping ports-and-adapters applications with decorators, autoload, and punq
  • lightweight role-based authorization with an explicit Principal

Requirements

  • Python >=3.14,<4

Installation

pip install iisi-app-core

Bootstrap Quickstart

1. Mark ports and implementations

from iisi_app_core import (
    ContainerBuilder,
    application,
    driven_adapter,
    driven_port,
    driving_adapter,
    driving_port,
)


@driven_port
class UserRepository:
    def get(self, user_id: str) -> dict[str, str]:
        raise NotImplementedError


@driven_adapter(port=UserRepository)
class DynamoUserRepository(UserRepository):
    def get(self, user_id: str) -> dict[str, str]:
        return {"id": user_id, "name": "Ada"}


@driving_port
class GetUserProfilePort:
    def get_user_profile(self, user_id: str) -> dict[str, str]:
        raise NotImplementedError


@application(port=GetUserProfilePort)
class GetUserProfile(GetUserProfilePort):
    def __init__(self, repo: UserRepository) -> None:
        self.repo = repo

    def get_user_profile(self, user_id: str) -> dict[str, str]:
        return self.repo.get(user_id)


@driving_adapter
class GetUserProfileHandler:
    def __init__(self, app: GetUserProfilePort) -> None:
        self.app = app

    def handle(self, user_id: str) -> dict[str, str]:
        return self.app.get_user_profile(user_id)

ContainerBuilder.build() validates the boundaries:

  • driving adapters may depend only on @driving_port ports
  • applications may depend only on @driven_port ports
  • depending on a decorated implementation directly raises PortsAndAdaptersViolationError

2. Autoload modules

import app.users

from iisi_app_core import autoload


modules = autoload(app.users)

autoload(...) expects an imported package object and recursively imports all Python modules under it, excluding __init__.py.

3. Build the container

from iisi_app_core import ComponentRegistry, ContainerBuilder


registry = ComponentRegistry.from_modules(modules)
container = ContainerBuilder(registry).build()

handler = container.resolve(GetUserProfileHandler)
result = handler.handle("123")

Both the declared port and the concrete implementation are registered in punq.

4. Optional FastAPI + Mangum bootstrap

Install the framework extra when you want a web bootstrap path:

pip install "iisi-app-core[fastapi]"
from typing import Annotated

import app.users
from fastapi import APIRouter, FastAPI

from iisi_app_core.integrations.fastapi import FastApiMangumBuilder, from_container


def create_app(ctx) -> FastAPI:
    from app.users.ports import GetUserProfilePort

    router = APIRouter()

    @router.get("/users/{user_id}")
    def get_user_profile(
        user_id: str,
        use_case: Annotated[GetUserProfilePort, from_container(GetUserProfilePort)],
    ) -> dict[str, str]:
        return use_case.get_user_profile(user_id)

    app = FastAPI()
    app.include_router(router)
    return app


built = (
    FastApiMangumBuilder()
    .autoload(app.users)
    .app(create_app)
    .enable_mangum(lifespan="off")
    .build()
)

app = built.app
handler = built.handler

FastApiMangumBuilder reuses the existing autoload(...) and ContainerBuilder.build() logic, then exposes the built container, registry, and modules through app.state.

Standalone Authz

authz is explicit and does not use request-local global state.

from iisi_app_core import Principal, Role, require_role


@require_role(Role.READER)
def view_profile(*, principal: Principal, user_id: str) -> str:
    return f"{principal.name}:{user_id}"


principal = Principal.of(name="alice", role=Role.READER, tenant_id="tenant")
result = view_profile(principal=principal, user_id="123")

If your callable does not receive principal= directly, provide principal_getter=...:

from iisi_app_core import Role, require_role


@require_role(Role.ADMIN, principal_getter=lambda args, kwargs: args[0].principal)
def run(command) -> str:
    return "ok"

Available authz helpers:

  • Principal, Role
  • authorize, ensure_at_least, at_least, has_role
  • require_role
  • DomainValidationError, ForbiddenError

Public API

Top-level exports are intentionally small:

  • bootstrap: autoload, ModuleAutoloadError, decorators, metadata types, ComponentRegistry, ContainerBuilder
  • authz: Principal, Role, require_role, helper functions, DomainValidationError, ForbiddenError

Everything else from earlier versions is out of scope for this library.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iisi_app_core-0.2.0.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iisi_app_core-0.2.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file iisi_app_core-0.2.0.tar.gz.

File metadata

  • Download URL: iisi_app_core-0.2.0.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for iisi_app_core-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9e97ca40bfe5fb64f433d9639d569c10e3d4f221827ca28b9d86223d4939b2c7
MD5 8e543fc02d1c2a215b79012d06b679ac
BLAKE2b-256 927384ed8598792ab0b4568ba438fe2414811bc0d8cbf778fc22055181395eff

See more details on using hashes here.

File details

Details for the file iisi_app_core-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: iisi_app_core-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for iisi_app_core-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 587097f7ae31f2cbf82313b2756c3f98c08a9b975c8fdaa035a96f5e5f7c279b
MD5 a9f1e9544ce01d089d7f35e74be89057
BLAKE2b-256 ad296ffc779bc3188abbee988cb7538c262cfe7b63b96dcc855ba5d570f2b99d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page