API Reference¶
Complete API documentation for all chapkit modules, classes, and functions.
Core Layer¶
Framework-agnostic infrastructure components.
Database¶
database
¶
Async SQLAlchemy database connection manager.
Database
¶
Generic async SQLAlchemy database connection manager.
Source code in src/chapkit/core/database.py
__init__(url, *, echo=False, alembic_dir=None, auto_migrate=True, pool_size=5, max_overflow=10, pool_recycle=3600, pool_pre_ping=True)
¶
Initialize database with connection URL and pool configuration.
Source code in src/chapkit/core/database.py
init()
async
¶
Initialize database tables using Alembic migrations or direct creation.
Source code in src/chapkit/core/database.py
session()
async
¶
SqliteDatabase
¶
Bases: Database
SQLite-specific database implementation with optimizations.
Source code in src/chapkit/core/database.py
__init__(url, *, echo=False, alembic_dir=None, auto_migrate=True, pool_size=5, max_overflow=10, pool_recycle=3600, pool_pre_ping=True)
¶
Initialize SQLite database with connection URL and pool configuration.
Source code in src/chapkit/core/database.py
is_in_memory()
¶
init()
async
¶
Initialize database tables and configure SQLite using Alembic migrations.
Source code in src/chapkit/core/database.py
SqliteDatabaseBuilder
¶
Builder for SQLite database configuration with fluent API.
Source code in src/chapkit/core/database.py
__init__()
¶
Initialize builder with default values.
Source code in src/chapkit/core/database.py
in_memory()
classmethod
¶
from_file(path)
classmethod
¶
Create a file-based SQLite database configuration.
Source code in src/chapkit/core/database.py
with_echo(enabled=True)
¶
with_migrations(enabled=True, alembic_dir=None)
¶
with_pool(size=5, max_overflow=10, recycle=3600, pre_ping=True)
¶
Configure connection pool settings.
Source code in src/chapkit/core/database.py
build()
¶
Build and return configured SqliteDatabase instance.
Source code in src/chapkit/core/database.py
Models¶
models
¶
Base ORM classes for SQLAlchemy models.
Base
¶
Entity
¶
Bases: Base
Optional base with common columns for your models.
Source code in src/chapkit/core/models.py
Repository¶
repository
¶
Base repository classes for data access layer.
Repository
¶
Bases: ABC
Abstract repository interface for data access operations.
Source code in src/chapkit/core/repository.py
save(entity)
abstractmethod
async
¶
save_all(entities)
abstractmethod
async
¶
commit()
abstractmethod
async
¶
refresh_many(entities)
abstractmethod
async
¶
delete(entity)
abstractmethod
async
¶
delete_by_id(id)
abstractmethod
async
¶
delete_all()
abstractmethod
async
¶
delete_all_by_id(ids)
abstractmethod
async
¶
count()
abstractmethod
async
¶
exists_by_id(id)
abstractmethod
async
¶
find_all()
abstractmethod
async
¶
find_all_paginated(offset, limit)
abstractmethod
async
¶
find_all_by_id(ids)
abstractmethod
async
¶
BaseRepository
¶
Bases: Repository[T, IdT]
Base repository implementation with common CRUD operations.
Source code in src/chapkit/core/repository.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
|
__init__(session, model)
¶
save(entity)
async
¶
save_all(entities)
async
¶
commit()
async
¶
refresh_many(entities)
async
¶
delete(entity)
async
¶
delete_by_id(id)
async
¶
delete_all()
async
¶
delete_all_by_id(ids)
async
¶
Delete multiple entities by their IDs.
Source code in src/chapkit/core/repository.py
count()
async
¶
exists_by_id(id)
async
¶
Check if an entity exists by its ID.
Source code in src/chapkit/core/repository.py
find_all()
async
¶
find_all_paginated(offset, limit)
async
¶
Find entities with pagination.
find_all_by_id(ids)
async
¶
Find entities by their IDs.
Source code in src/chapkit/core/repository.py
Manager¶
manager
¶
Base classes for service layer managers with lifecycle hooks.
LifecycleHooks
¶
Lifecycle hooks for entity operations.
Source code in src/chapkit/core/manager.py
Manager
¶
Bases: ABC
Abstract manager interface for business logic operations.
Source code in src/chapkit/core/manager.py
save(data)
abstractmethod
async
¶
save_all(items)
abstractmethod
async
¶
delete_by_id(id)
abstractmethod
async
¶
delete_all()
abstractmethod
async
¶
delete_all_by_id(ids)
abstractmethod
async
¶
count()
abstractmethod
async
¶
exists_by_id(id)
abstractmethod
async
¶
find_by_id(id)
abstractmethod
async
¶
find_all()
abstractmethod
async
¶
find_paginated(page, size)
abstractmethod
async
¶
BaseManager
¶
Bases: LifecycleHooks[ModelT, InSchemaT]
, Manager[InSchemaT, OutSchemaT, IdT]
Base manager implementation with CRUD operations and lifecycle hooks.
Source code in src/chapkit/core/manager.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
|
__init__(repo, model_cls, out_schema_cls)
¶
Initialize manager with repository, model class, and output schema class.
Source code in src/chapkit/core/manager.py
save(data)
async
¶
Save an entity (create or update).
Source code in src/chapkit/core/manager.py
delete_by_id(id)
async
¶
Delete an entity by its ID.
Source code in src/chapkit/core/manager.py
delete_all()
async
¶
Delete all entities.
Source code in src/chapkit/core/manager.py
delete_all_by_id(ids)
async
¶
Delete multiple entities by their IDs.
Source code in src/chapkit/core/manager.py
count()
async
¶
exists_by_id(id)
async
¶
find_by_id(id)
async
¶
find_all()
async
¶
find_paginated(page, size)
async
¶
Find entities with pagination.
Source code in src/chapkit/core/manager.py
find_all_by_id(ids)
async
¶
Schemas¶
schemas
¶
Core Pydantic schemas for entities, responses, and jobs.
EntityIn
¶
EntityOut
¶
Bases: BaseModel
Base output schema for entities with ID and timestamps.
Source code in src/chapkit/core/schemas.py
PaginatedResponse
¶
Bases: BaseModel
, Generic[T]
Paginated response with items, total count, page number, and computed page count.
Source code in src/chapkit/core/schemas.py
pages
property
¶
Total number of pages.
BulkOperationError
¶
Bases: BaseModel
Error information for a single item in a bulk operation.
Source code in src/chapkit/core/schemas.py
BulkOperationResult
¶
Bases: BaseModel
Result of bulk operation with counts of succeeded/failed items and error details.
Source code in src/chapkit/core/schemas.py
ProblemDetail
¶
Bases: BaseModel
RFC 9457 Problem Details with URN error type, status, and human-readable messages.
Source code in src/chapkit/core/schemas.py
JobStatus
¶
JobRecord
¶
Bases: BaseModel
Complete record of a scheduled job's state and metadata.
Source code in src/chapkit/core/schemas.py
Exceptions¶
exceptions
¶
Custom exceptions with RFC 9457 Problem Details support.
ErrorType
¶
URN-based error type identifiers for RFC 9457 Problem Details.
Source code in src/chapkit/core/exceptions.py
ChapkitException
¶
Bases: Exception
Base exception for chapkit with RFC 9457 Problem Details support.
Source code in src/chapkit/core/exceptions.py
NotFoundError
¶
Bases: ChapkitException
Resource not found exception (404).
Source code in src/chapkit/core/exceptions.py
ValidationError
¶
Bases: ChapkitException
Validation failed exception (400).
Source code in src/chapkit/core/exceptions.py
ConflictError
¶
Bases: ChapkitException
Resource conflict exception (409).
Source code in src/chapkit/core/exceptions.py
InvalidULIDError
¶
Bases: ChapkitException
Invalid ULID format exception (400).
Source code in src/chapkit/core/exceptions.py
BadRequestError
¶
UnauthorizedError
¶
ForbiddenError
¶
Scheduler¶
scheduler
¶
Job scheduler for async task management with in-memory asyncio implementation.
JobScheduler
¶
Bases: BaseModel
, ABC
Abstract job scheduler interface for async task management.
Source code in src/chapkit/core/scheduler.py
add_job(target, /, *args, **kwargs)
abstractmethod
async
¶
get_status(job_id)
abstractmethod
async
¶
get_record(job_id)
abstractmethod
async
¶
get_all_records()
abstractmethod
async
¶
cancel(job_id)
abstractmethod
async
¶
delete(job_id)
abstractmethod
async
¶
wait(job_id, timeout=None)
abstractmethod
async
¶
AIOJobScheduler
¶
Bases: JobScheduler
In-memory asyncio scheduler. Sync callables run in thread pool, concurrency controlled via semaphore.
Source code in src/chapkit/core/scheduler.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
__init__(**data)
¶
Initialize scheduler with optional concurrency limit.
set_max_concurrency(n)
async
¶
Set maximum number of concurrent jobs.
add_job(target, /, *args, **kwargs)
async
¶
Add a job to the scheduler and return its ID.
Source code in src/chapkit/core/scheduler.py
get_all_records()
async
¶
Get all job records sorted by submission time.
Source code in src/chapkit/core/scheduler.py
get_record(job_id)
async
¶
Get the full record of a job.
Source code in src/chapkit/core/scheduler.py
get_status(job_id)
async
¶
Get the status of a job.
get_result(job_id)
async
¶
Get the result of a completed job.
Source code in src/chapkit/core/scheduler.py
wait(job_id, timeout=None)
async
¶
Wait for a job to complete.
Source code in src/chapkit/core/scheduler.py
cancel(job_id)
async
¶
Cancel a running job.
Source code in src/chapkit/core/scheduler.py
delete(job_id)
async
¶
Delete a job record.
Source code in src/chapkit/core/scheduler.py
Types¶
types
¶
Custom types for chapkit - SQLAlchemy and Pydantic types.
JsonSafe = Annotated[Any, PlainSerializer(_serialize_with_metadata, return_type=Any)]
module-attribute
¶
Pydantic type for JSON-safe serialization with graceful handling of non-serializable values.
ULIDType
¶
Bases: TypeDecorator[ULID]
SQLAlchemy custom type for ULID stored as 26-character strings.
Source code in src/chapkit/core/types.py
Logging¶
logging
¶
Structured logging configuration with request tracing support.
configure_logging()
¶
Configure structlog and intercept standard library logging.
Source code in src/chapkit/core/logging.py
get_logger(name=None)
¶
add_request_context(**context)
¶
clear_request_context(*keys)
¶
FastAPI Layer¶
FastAPI-specific components for building web services.
Service Builders¶
Service builder classes for composing FastAPI applications.
BaseServiceBuilder¶
BaseServiceBuilder
¶
Base service builder providing core FastAPI functionality without module dependencies.
Source code in src/chapkit/core/api/service_builder.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 |
|
__init__(*, info, database_url='sqlite+aiosqlite:///:memory:', include_error_handlers=True, include_logging=False)
¶
Initialize base service builder with core options.
Source code in src/chapkit/core/api/service_builder.py
with_database(url_or_instance=None, *, pool_size=5, max_overflow=10, pool_recycle=3600, pool_pre_ping=True)
¶
Configure database with URL string, Database instance, or default in-memory SQLite.
Source code in src/chapkit/core/api/service_builder.py
with_landing_page()
¶
with_logging(enabled=True)
¶
with_health(*, prefix='/health', tags=None, checks=None, include_database_check=True)
¶
Add health check endpoint with optional custom checks.
Source code in src/chapkit/core/api/service_builder.py
with_system(*, prefix='/api/v1/system', tags=None)
¶
Add system info endpoint.
Source code in src/chapkit/core/api/service_builder.py
with_jobs(*, prefix='/api/v1/jobs', tags=None, max_concurrency=None)
¶
Add job scheduler endpoints.
Source code in src/chapkit/core/api/service_builder.py
with_auth(*, api_keys=None, api_key_file=None, env_var='CHAPKIT_API_KEYS', header_name='X-API-Key', unauthenticated_paths=None)
¶
Enable API key authentication.
Source code in src/chapkit/core/api/service_builder.py
with_monitoring(*, prefix='/metrics', tags=None, service_name=None, enable_traces=False)
¶
Enable OpenTelemetry monitoring with Prometheus endpoint and auto-instrumentation.
Source code in src/chapkit/core/api/service_builder.py
with_app(path, prefix=None)
¶
Register static app from filesystem path or package resource tuple.
Source code in src/chapkit/core/api/service_builder.py
with_apps(path)
¶
Auto-discover and register all apps in directory.
include_router(router)
¶
override_dependency(dependency, override)
¶
Override a dependency for testing or customization.
on_startup(hook)
¶
on_shutdown(hook)
¶
build()
¶
Build and configure the FastAPI application.
Source code in src/chapkit/core/api/service_builder.py
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
|
create(*, info, **kwargs)
classmethod
¶
ServiceInfo¶
ServiceInfo
¶
Bases: BaseModel
Service metadata for FastAPI application.
Source code in src/chapkit/core/api/service_builder.py
Routers¶
Base router classes and generic routers.
Router¶
Router
¶
Bases: ABC
Base class for FastAPI routers.
Source code in src/chapkit/core/api/router.py
CrudRouter¶
CrudRouter
¶
Bases: Router
Router base class for standard REST CRUD operations.
Source code in src/chapkit/core/api/crud.py
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
|
__init__(prefix, tags, entity_in_type, entity_out_type, manager_factory, *, permissions=None, **kwargs)
¶
Initialize CRUD router with entity types and manager factory.
Source code in src/chapkit/core/api/crud.py
register_entity_operation(name, handler, *, http_method='GET', response_model=None, status_code=None, summary=None, description=None)
¶
Register a custom entity operation with $ prefix.
Entity operations are automatically inserted before generic {entity_id} routes to ensure proper route matching (e.g., /{entity_id}/$validate should match before /{entity_id}).
Source code in src/chapkit/core/api/crud.py
register_collection_operation(name, handler, *, http_method='GET', response_model=None, status_code=None, summary=None, description=None)
¶
Register a custom collection operation with $ prefix.
Collection operations are automatically inserted before parametric {entity_id} routes to ensure proper route matching (e.g., /$stats should match before /{entity_id}).
Source code in src/chapkit/core/api/crud.py
CrudPermissions¶
CrudPermissions
dataclass
¶
HealthRouter¶
HealthRouter
¶
Bases: Router
Health check router for service health monitoring.
Source code in src/chapkit/core/api/routers/health.py
__init__(prefix, tags, checks=None, **kwargs)
¶
Initialize health router with optional health checks.
Source code in src/chapkit/core/api/routers/health.py
JobRouter¶
JobRouter
¶
Bases: Router
REST API router for job scheduler operations.
Source code in src/chapkit/core/api/routers/job.py
22 23 24 25 26 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
__init__(prefix, tags, scheduler_factory, **kwargs)
¶
Initialize job router with scheduler factory.
Source code in src/chapkit/core/api/routers/job.py
SystemRouter¶
SystemRouter
¶
Bases: Router
System information router.
Source code in src/chapkit/core/api/routers/system.py
App System¶
Static web application hosting system.
AppLoader¶
AppLoader
¶
Loads and validates apps from filesystem or package resources.
Source code in src/chapkit/core/api/app.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
load(path, prefix=None)
staticmethod
¶
Load and validate app from filesystem path or package resource tuple.
Source code in src/chapkit/core/api/app.py
discover(path)
staticmethod
¶
Discover all apps with manifest.json in directory.
Source code in src/chapkit/core/api/app.py
AppManifest¶
AppManifest
¶
Bases: BaseModel
App manifest configuration.
Source code in src/chapkit/core/api/app.py
validate_prefix(v)
classmethod
¶
Validate mount prefix format.
Source code in src/chapkit/core/api/app.py
validate_entry(v)
classmethod
¶
Validate entry file path for security.
Source code in src/chapkit/core/api/app.py
App¶
App
dataclass
¶
Represents a loaded app with manifest and directory.
Source code in src/chapkit/core/api/app.py
AppManager¶
AppManager
¶
Lightweight manager for app metadata queries.
Source code in src/chapkit/core/api/app.py
AppInfo¶
AppInfo
¶
Bases: BaseModel
App metadata for API responses.
Source code in src/chapkit/core/api/app.py
Authentication¶
API key authentication middleware and utilities.
APIKeyMiddleware¶
APIKeyMiddleware
¶
Bases: BaseHTTPMiddleware
Middleware for API key authentication via X-API-Key header.
Source code in src/chapkit/core/api/auth.py
__init__(app, *, api_keys, header_name='X-API-Key', unauthenticated_paths)
¶
Initialize API key middleware.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
app
|
Any
|
ASGI application |
required |
api_keys
|
Set[str]
|
Set of valid API keys |
required |
header_name
|
str
|
HTTP header name for API key |
'X-API-Key'
|
unauthenticated_paths
|
Set[str]
|
Paths that don't require authentication |
required |
Source code in src/chapkit/core/api/auth.py
dispatch(request, call_next)
async
¶
Process request with API key authentication.
Source code in src/chapkit/core/api/auth.py
Middleware¶
Error handling and logging middleware.
middleware
¶
FastAPI middleware for error handling, CORS, and other cross-cutting concerns.
RequestLoggingMiddleware
¶
Bases: BaseHTTPMiddleware
Middleware for logging HTTP requests with unique request IDs and context binding.
Source code in src/chapkit/core/api/middleware.py
dispatch(request, call_next)
async
¶
Process request with logging and context binding.
Source code in src/chapkit/core/api/middleware.py
database_error_handler(request, exc)
async
¶
Handle database errors and return error response.
Source code in src/chapkit/core/api/middleware.py
validation_error_handler(request, exc)
async
¶
Handle validation errors and return error response.
Source code in src/chapkit/core/api/middleware.py
chapkit_exception_handler(request, exc)
async
¶
Handle ChapkitException and return RFC 9457 Problem Details.
Source code in src/chapkit/core/api/middleware.py
add_error_handlers(app)
¶
Add error handlers to FastAPI application.
Source code in src/chapkit/core/api/middleware.py
Dependencies¶
FastAPI dependency injection functions.
dependencies
¶
Generic FastAPI dependency injection for database and scheduler.
Pagination¶
Pagination helpers for collection endpoints.
pagination
¶
Pagination utilities for API endpoints.
PaginationParams
¶
Bases: BaseModel
Query parameters for opt-in pagination (both page and size required).
Source code in src/chapkit/core/api/pagination.py
create_paginated_response(items, total, page, size)
¶
Create paginated response with items and metadata.
Utilities¶
Utility functions for FastAPI applications.
utilities
¶
Utility functions for FastAPI routers and endpoints.
build_location_url(request, path)
¶
run_app(app, *, host=None, port=None, workers=None, reload=None, log_level=None, **uvicorn_kwargs)
¶
Run FastAPI app with Uvicorn development server.
For reload to work, pass a string in "module:app" format. App instance disables reload automatically.
Examples:
# Direct execution (reload disabled)
if __name__ == "__main__":
run_app(app)
# With module path (reload enabled)
run_app("examples.config_api:app")
# Production: multiple workers
run_app(app, workers=4)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
app
|
Any | str
|
FastAPI app instance OR string "module:app" path |
required |
host
|
str | None
|
Server host (default: "127.0.0.1", env: HOST) |
None
|
port
|
int | None
|
Server port (default: 8000, env: PORT) |
None
|
workers
|
int | None
|
Number of worker processes (default: 1, env: WORKERS) |
None
|
reload
|
bool | None
|
Enable auto-reload (default: True for string, False for instance) |
None
|
log_level
|
str | None
|
Logging level (default: from LOG_LEVEL env var or "info") |
None
|
**uvicorn_kwargs
|
Any
|
Additional uvicorn.run() arguments |
{}
|
Source code in src/chapkit/core/api/utilities.py
Application Layer¶
High-level application orchestration.
ServiceBuilder¶
Domain-aware service builder with module support.
ServiceBuilder
¶
Bases: BaseServiceBuilder
Service builder with integrated module support (config, artifact, task).
Source code in src/chapkit/api/service_builder.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
|
__init__(**kwargs)
¶
Initialize service builder with module-specific state.
Source code in src/chapkit/api/service_builder.py
with_tasks(*, prefix='/api/v1/tasks', tags=None, permissions=None, validate_on_startup=True, allow_create=None, allow_read=None, allow_update=None, allow_delete=None)
¶
Enable task execution endpoints with script runner.
Source code in src/chapkit/api/service_builder.py
with_ml(runner, *, prefix='/api/v1/ml', tags=None)
¶
Enable ML train/predict endpoints with model runner.
Source code in src/chapkit/api/service_builder.py
MLServiceBuilder¶
Specialized builder for machine learning services.
MLServiceBuilder
¶
Bases: ServiceBuilder
Specialized service builder for ML services with all required components pre-configured.
Source code in src/chapkit/api/service_builder.py
__init__(*, info, config_schema, hierarchy, runner, database_url='sqlite+aiosqlite:///:memory:', include_error_handlers=True, include_logging=True)
¶
Initialize ML service builder with required ML components.
Source code in src/chapkit/api/service_builder.py
Dependencies¶
Application-level dependency injection functions.
dependencies
¶
Feature-specific FastAPI dependency injection for managers.
get_config_manager(session)
async
¶
Get a config manager instance for dependency injection.
Source code in src/chapkit/api/dependencies.py
get_artifact_manager(session)
async
¶
Get an artifact manager instance for dependency injection.
Source code in src/chapkit/api/dependencies.py
get_task_manager(session, artifact_manager)
async
¶
Get a task manager instance for dependency injection.
Source code in src/chapkit/api/dependencies.py
get_ml_manager()
async
¶
Get an ML manager instance for dependency injection.
Note: This is a placeholder. The actual dependency is built by ServiceBuilder with the runner in closure, then overridden via app.dependency_overrides.
Source code in src/chapkit/api/dependencies.py
Domain Modules¶
Vertical slice modules with complete functionality.
Config Module¶
Key-value configuration with JSON data support.
config
¶
Config feature - key-value configuration with JSON data storage.
ConfigManager
¶
Bases: BaseManager[Config, ConfigIn[DataT], ConfigOut[DataT], ULID]
Manager for Config entities with artifact linking operations.
Source code in src/chapkit/modules/config/manager.py
__init__(repo, data_cls)
¶
Initialize config manager with repository and data class.
Source code in src/chapkit/modules/config/manager.py
find_by_name(name)
async
¶
Find a config by its unique name.
link_artifact(config_id, artifact_id)
async
¶
unlink_artifact(artifact_id)
async
¶
get_config_for_artifact(artifact_id, artifact_repo)
async
¶
Get the config for an artifact by traversing to its root.
Source code in src/chapkit/modules/config/manager.py
get_linked_artifacts(config_id)
async
¶
Get all root artifacts linked to a config.
Source code in src/chapkit/modules/config/manager.py
Config
¶
Bases: Entity
ORM model for configuration with JSON data storage.
Source code in src/chapkit/modules/config/models.py
data
property
writable
¶
Return JSON data as dict.
ConfigArtifact
¶
Bases: Base
Junction table linking Configs to root Artifacts.
Source code in src/chapkit/modules/config/models.py
ConfigRepository
¶
Bases: BaseRepository[Config, ULID]
Repository for Config entities with artifact linking operations.
Source code in src/chapkit/modules/config/repository.py
__init__(session)
¶
find_by_name(name)
async
¶
Find a config by its unique name.
link_artifact(config_id, artifact_id)
async
¶
Link a config to a root artifact.
Source code in src/chapkit/modules/config/repository.py
unlink_artifact(artifact_id)
async
¶
Unlink an artifact from its config.
delete_by_id(id)
async
¶
Delete a config and cascade delete all linked artifact trees.
Source code in src/chapkit/modules/config/repository.py
find_by_root_artifact_id(artifact_id)
async
¶
Find the config linked to a root artifact.
Source code in src/chapkit/modules/config/repository.py
find_artifacts_for_config(config_id)
async
¶
Find all root artifacts linked to a config.
Source code in src/chapkit/modules/config/repository.py
ConfigRouter
¶
Bases: CrudRouter[ConfigIn[BaseConfig], ConfigOut[BaseConfig]]
CRUD router for Config entities with artifact linking operations.
Source code in src/chapkit/modules/config/router.py
17 18 19 20 21 22 23 24 25 26 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
__init__(prefix, tags, manager_factory, entity_in_type, entity_out_type, permissions=None, enable_artifact_operations=False, **kwargs)
¶
Initialize config router with entity types and manager factory.
Source code in src/chapkit/modules/config/router.py
BaseConfig
¶
ConfigIn
¶
ConfigOut
¶
Bases: EntityOut
Output schema for configuration entities.
Source code in src/chapkit/modules/config/schemas.py
convert_dict_to_model(v, info)
classmethod
¶
Convert dict to BaseConfig model if data_cls is provided in validation context.
Source code in src/chapkit/modules/config/schemas.py
serialize_data(value)
¶
Serialize BaseConfig data to JSON dict.
Source code in src/chapkit/modules/config/schemas.py
LinkArtifactRequest
¶
Artifact Module¶
Hierarchical artifact tree storage.
artifact
¶
Artifact feature - hierarchical data storage with parent-child relationships.
ArtifactManager
¶
Bases: BaseManager[Artifact, ArtifactIn, ArtifactOut, ULID]
Manager for Artifact entities with hierarchical tree operations.
Source code in src/chapkit/modules/artifact/manager.py
18 19 20 21 22 23 24 25 26 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
|
__init__(repo, hierarchy=None, config_repo=None)
¶
Initialize artifact manager with repository, hierarchy, and optional config repo.
Source code in src/chapkit/modules/artifact/manager.py
find_subtree(start_id)
async
¶
Find all artifacts in the subtree rooted at the given ID.
Source code in src/chapkit/modules/artifact/manager.py
expand_artifact(artifact_id)
async
¶
Expand a single artifact with hierarchy metadata but without children.
Source code in src/chapkit/modules/artifact/manager.py
build_tree(start_id)
async
¶
Build a hierarchical tree structure rooted at the given artifact ID.
Source code in src/chapkit/modules/artifact/manager.py
pre_save(entity, data)
async
¶
pre_update(entity, data, old_values)
async
¶
Recalculate artifact level and cascade updates to descendants if parent changed.
Source code in src/chapkit/modules/artifact/manager.py
Artifact
¶
Bases: Entity
ORM model for hierarchical artifacts with parent-child relationships.
Source code in src/chapkit/modules/artifact/models.py
ArtifactRepository
¶
Bases: BaseRepository[Artifact, ULID]
Repository for Artifact entities with tree traversal operations.
Source code in src/chapkit/modules/artifact/repository.py
__init__(session)
¶
find_by_id(id)
async
¶
Find an artifact by ID with children eagerly loaded.
find_subtree(start_id)
async
¶
Find all artifacts in the subtree rooted at the given ID using recursive CTE.
Source code in src/chapkit/modules/artifact/repository.py
get_root_artifact(artifact_id)
async
¶
Find the root artifact by traversing up the parent chain.
Source code in src/chapkit/modules/artifact/repository.py
ArtifactRouter
¶
Bases: CrudRouter[ArtifactIn, ArtifactOut]
CRUD router for Artifact entities with tree operations and config access.
Source code in src/chapkit/modules/artifact/router.py
18 19 20 21 22 23 24 25 26 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
|
__init__(prefix, tags, manager_factory, entity_in_type, entity_out_type, permissions=None, enable_config_access=False, **kwargs)
¶
Initialize artifact router with entity types and manager factory.
Source code in src/chapkit/modules/artifact/router.py
ArtifactHierarchy
¶
Bases: BaseModel
Configuration for artifact hierarchy with level labels.
Source code in src/chapkit/modules/artifact/schemas.py
ArtifactIn
¶
ArtifactOut
¶
ArtifactTreeNode
¶
Bases: ArtifactOut
Artifact node with tree structure metadata and optional config.
Source code in src/chapkit/modules/artifact/schemas.py
from_artifact(artifact)
classmethod
¶
PandasDataFrame
¶
Bases: BaseModel
Pydantic schema for serializing pandas DataFrames.
Source code in src/chapkit/modules/artifact/schemas.py
from_dataframe(df)
classmethod
¶
Create schema from pandas DataFrame.
Source code in src/chapkit/modules/artifact/schemas.py
Task Module¶
Script execution template system.
task
¶
Task feature - reusable command templates for task execution.
TaskManager
¶
Bases: BaseManager[Task, TaskIn, TaskOut, ULID]
Manager for Task template entities with artifact-based execution.
Source code in src/chapkit/modules/task/manager.py
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
|
__init__(repo, scheduler=None, database=None, artifact_manager=None)
¶
Initialize task manager with repository, scheduler, database, and artifact manager.
Source code in src/chapkit/modules/task/manager.py
find_all(*, enabled=None)
async
¶
Find all tasks, optionally filtered by enabled status.
Source code in src/chapkit/modules/task/manager.py
execute_task(task_id)
async
¶
Execute a task by submitting it to the scheduler and return the job ID.
Source code in src/chapkit/modules/task/manager.py
Task
¶
Bases: Entity
ORM model for reusable task templates containing commands to execute.
Source code in src/chapkit/modules/task/models.py
TaskRegistry
¶
Global registry for Python task functions.
Source code in src/chapkit/modules/task/registry.py
register(name)
classmethod
¶
Decorator to register a task function with support for type-based dependency injection.
Source code in src/chapkit/modules/task/registry.py
register_function(name, func)
classmethod
¶
Imperatively register a task function.
Source code in src/chapkit/modules/task/registry.py
get(name)
classmethod
¶
Retrieve a registered task function.
list_all()
classmethod
¶
TaskRepository
¶
Bases: BaseRepository[Task, ULID]
Repository for Task template entities.
Source code in src/chapkit/modules/task/repository.py
TaskRouter
¶
Bases: CrudRouter[TaskIn, TaskOut]
CRUD router for Task entities with execution operation.
Source code in src/chapkit/modules/task/router.py
__init__(prefix, tags, manager_factory, entity_in_type, entity_out_type, permissions=None, **kwargs)
¶
Initialize task router with entity types and manager factory.
Source code in src/chapkit/modules/task/router.py
TaskIn
¶
Bases: EntityIn
Input schema for creating or updating task templates.
Source code in src/chapkit/modules/task/schemas.py
TaskOut
¶
Bases: EntityOut
Output schema for task template entities.
Source code in src/chapkit/modules/task/schemas.py
validate_and_disable_orphaned_tasks(app)
async
¶
Validate Python tasks and disable orphaned ones that reference missing functions.
Source code in src/chapkit/modules/task/validation.py
ML Module¶
Machine learning train and predict operations.
ml
¶
ML module for train/predict operations with artifact-based model storage.
MLManager
¶
Manager for ML train/predict operations with job scheduling and artifact storage.
Source code in src/chapkit/modules/ml/manager.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
|
__init__(runner, scheduler, database, config_schema)
¶
Initialize ML manager with runner, scheduler, database, and config schema.
Source code in src/chapkit/modules/ml/manager.py
execute_train(request)
async
¶
Submit a training job to the scheduler and return job/artifact IDs.
Source code in src/chapkit/modules/ml/manager.py
execute_predict(request)
async
¶
Submit a prediction job to the scheduler and return job/artifact IDs.
Source code in src/chapkit/modules/ml/manager.py
MLRouter
¶
Bases: Router
Router with $train and $predict collection operations.
Source code in src/chapkit/modules/ml/router.py
__init__(prefix, tags, manager_factory, **kwargs)
¶
Initialize ML router with manager factory.
Source code in src/chapkit/modules/ml/router.py
BaseModelRunner
¶
Bases: ABC
Abstract base class for model runners with lifecycle hooks.
Source code in src/chapkit/modules/ml/runner.py
on_init()
async
¶
on_cleanup()
async
¶
on_train(config, data, geo=None)
abstractmethod
async
¶
Train a model and return the trained model object (must be pickleable).
on_predict(config, model, historic, future, geo=None)
abstractmethod
async
¶
Make predictions using a trained model and return predictions as DataFrame.
Source code in src/chapkit/modules/ml/runner.py
FunctionalModelRunner
¶
Bases: BaseModelRunner
, Generic[ConfigT]
Functional model runner wrapping train and predict functions.
Source code in src/chapkit/modules/ml/runner.py
__init__(on_train, on_predict)
¶
Initialize functional runner with train and predict functions.
on_train(config, data, geo=None)
async
¶
Train a model and return the trained model object.
Source code in src/chapkit/modules/ml/runner.py
on_predict(config, model, historic, future, geo=None)
async
¶
Make predictions using a trained model.
Source code in src/chapkit/modules/ml/runner.py
ShellModelRunner
¶
Bases: BaseModelRunner
Shell-based model runner that executes external scripts for train/predict operations.
Source code in src/chapkit/modules/ml/runner.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
__init__(train_command, predict_command, model_format='pickle')
¶
Initialize shell runner with command templates for train/predict operations.
Source code in src/chapkit/modules/ml/runner.py
on_train(config, data, geo=None)
async
¶
Train a model by executing external training script.
Source code in src/chapkit/modules/ml/runner.py
on_predict(config, model, historic, future, geo=None)
async
¶
Make predictions by executing external prediction script.
Source code in src/chapkit/modules/ml/runner.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
ModelRunnerProtocol
¶
Bases: Protocol
Protocol defining the interface for model runners.
Source code in src/chapkit/modules/ml/schemas.py
PredictionArtifactData
¶
Bases: BaseModel
Schema for prediction artifact data stored in the artifact system.
Source code in src/chapkit/modules/ml/schemas.py
PredictRequest
¶
Bases: BaseModel
Request schema for making predictions.
Source code in src/chapkit/modules/ml/schemas.py
PredictResponse
¶
Bases: BaseModel
Response schema for predict operation submission.
Source code in src/chapkit/modules/ml/schemas.py
TrainedModelArtifactData
¶
Bases: BaseModel
Schema for trained model artifact data stored in the artifact system.
Source code in src/chapkit/modules/ml/schemas.py
TrainRequest
¶
Bases: BaseModel
Request schema for training a model.
Source code in src/chapkit/modules/ml/schemas.py
TrainResponse
¶
Bases: BaseModel
Response schema for train operation submission.