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 dataclasses import dataclass
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

@dataclass(frozen=True, slots=True)
@application(port=GetUserProfilePort)
class GetUserProfile(GetUserProfilePort):
    
    repo: UserRepository

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

@dataclass(frozen=True, slots=True)
@driving_adapter
class GetUserProfileHandler:

    app: GetUserProfilePort

    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.1.tar.gz (17.1 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.1-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: iisi_app_core-0.2.1.tar.gz
  • Upload date:
  • Size: 17.1 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.1.tar.gz
Algorithm Hash digest
SHA256 534944deffd82217661d9cc1de05087e2358662dd4b5cd5315a8239b41d21911
MD5 b7a44144d4f3ae7e207e45414e5f31a5
BLAKE2b-256 f453e430127dc65139023f935f061477e157b218013c635b08f879b2fbfa12f8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: iisi_app_core-0.2.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 24057c43a6f5251535bb29624af6275fecf858d4f7c5ff6e651dfab9bbb8081c
MD5 2b9ba710ab241f1e37efa2193a6086ee
BLAKE2b-256 d074243cbf65dedc614455406dad85a8367e0a622cb93d56afae1cc081ca9538

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