Unit¶
For code to be allowed registration with an Application–be it a local package, an extension or something else–it must adhere to the Aioli Unit format, which mainly serves the purpose of providing modularity, consistency and flexibility.
Units, in its simplest form, are tagged groups of one or more Components in the form of:
- Services: Implements application logic and exposes an API for internal consumption
- Controllers: Handles HTTP requests and typically interacts with Service APIs
-
class
aioli.
Unit
(meta=None, auto_meta=False, controllers=None, services=None, config=None)[source]¶ Associates components and meta with a unit, for registration with a Aioli Application.
Parameters: - meta – Unit metadata, cannot be used with auto_meta
- auto_meta – Attempt to automatically resolve meta for Unit, cannot be used with meta
- controllers – List of Controller classes to register with the Unit
- services – List of Services classes to register with the Unit
- config – Unit Configuration Schema
Variables: - app – Application instance
- meta – Unit meta dictionary
- log – Unit logger
- config – Unit config
- controllers – List of Controllers registered with the Unit
- services – List of Services registered with the Unit
Example – Creating a Unit with Controller and Service layers
from aioli import Unit
from .service import VisitService, VisitorService
from .controller import HttpController
from .config import ConfigSchema
export = Unit(
auto_meta=True,
controllers=[HttpController],
services=[VisitService, VisitorService],
config=ConfigSchema,
)
Extend¶
Units can be connected using integrate()
or connect()
,
and those with the sole purpose of serving others, are known as Extensions.
Example – Leverage the aioli-rdbms Unit to gain database access
from aioli import BaseService
from aioli_rdbms import DatabaseService
from .database import UserModel
class UsersService(BaseService):
db = None
async def on_startup(self):
self.db = (
self.integrate(DatabaseService)
.use_model(UserModel)
)
async def get_one(user_id):
return await self.db.get_one(pk=user_id)
...