|
| 1 | +import asyncio |
| 2 | +from typing import TYPE_CHECKING |
| 3 | + |
| 4 | +from .http.endpoints import Endpoint |
| 5 | +from .data import StatusData, LogsData, FullLogsData, BackupData |
| 6 | +if TYPE_CHECKING: |
| 7 | + from .app import Application |
| 8 | +from typing import Callable |
| 9 | + |
| 10 | + |
| 11 | +class ListenerManager: |
| 12 | + def __init__(self, app: 'Application'): |
| 13 | + self.app: Application = app |
| 14 | + self.listeners: dict[str, Callable] = {} |
| 15 | + |
| 16 | + def add_listener(self, endpoint: Endpoint, call: Callable): |
| 17 | + self.listeners[endpoint.name] = call |
| 18 | + |
| 19 | + async def on_request_end(self, endpoint: Endpoint, **kwargs): |
| 20 | + call: Callable = self.listeners.get(endpoint.name) |
| 21 | + if not call: |
| 22 | + return |
| 23 | + match endpoint.name: |
| 24 | + case 'APP_STATUS': |
| 25 | + status: StatusData = kwargs.get('status') |
| 26 | + if asyncio.iscoroutinefunction(call): |
| 27 | + return await call(status=status) |
| 28 | + return call(status=status) |
| 29 | + case 'LOGS': |
| 30 | + logs: LogsData = kwargs.get('logs') |
| 31 | + if asyncio.iscoroutinefunction(call): |
| 32 | + return await call(logs=logs) |
| 33 | + return call(logs=logs) |
| 34 | + case 'FULL_LOGS': |
| 35 | + full_logs: FullLogsData = kwargs.get('full_logs') |
| 36 | + if asyncio.iscoroutinefunction(call): |
| 37 | + return await call(full_logs=full_logs) |
| 38 | + return call(full_logs=full_logs) |
| 39 | + case 'BACKUP': |
| 40 | + backup: BackupData = kwargs.get('backup') |
| 41 | + if asyncio.iscoroutinefunction(call): |
| 42 | + return await call(backup=backup) |
| 43 | + return call(backup=backup) |
0 commit comments