App Class¶
The App
class is the core of any Artanis application. It handles route registration, middleware management, and ASGI request processing.
App ¶
Main Artanis application class.
The core application class that handles route registration, middleware management, and ASGI request processing. Provides an Express.js-inspired API for building web applications.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
enable_request_logging
|
bool
|
Whether to enable automatic request logging |
True
|
Attributes:
Name | Type | Description |
---|---|---|
router |
Router instance for handling routes |
|
middleware_manager |
Manages global and path-based middleware |
|
middleware_executor |
Executes middleware chains |
|
event_manager |
Manages application events and handlers |
|
logger |
Application logger instance |
Example
Source code in src/artanis/application.py
Attributes¶
global_middleware
property
¶
Get global middleware list.
Returns:
Type | Description |
---|---|
list[Callable[..., Any]]
|
List of global middleware functions |
path_middleware
property
¶
Get path-based middleware dictionary.
Returns:
Type | Description |
---|---|
dict[str, list[Callable[..., Any]]]
|
Dictionary mapping paths to middleware lists |
routes
property
¶
Get all registered routes.
Returns:
Type | Description |
---|---|
list[dict[str, Any]]
|
List of all registered route dictionaries |
Functions¶
__call__
async
¶
ASGI application entry point.
Handles incoming HTTP requests and ASGI lifespan events.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scope
|
dict[str, Any]
|
ASGI scope dictionary |
required |
receive
|
Callable[..., Any]
|
ASGI receive callable |
required |
send
|
Callable[..., Any]
|
ASGI send callable |
required |
Source code in src/artanis/application.py
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 |
|
add_event_handler ¶
add_event_handler(
event_name: str,
handler: Callable[..., Any],
priority: int = 0,
condition: Callable[..., bool] | None = None,
) -> None
Register an event handler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event_name
|
str
|
Name of the event to handle (e.g., 'startup', 'shutdown', or custom) |
required |
handler
|
Callable[..., Any]
|
Function to call when event is triggered |
required |
priority
|
int
|
Execution priority (higher numbers run first) |
0
|
condition
|
Callable[..., bool] | None
|
Optional condition function to determine if handler should run |
None
|
Examples:
# Built-in lifecycle events
app.add_event_handler("startup", setup_database)
app.add_event_handler("shutdown", cleanup_database)
# Custom events
app.add_event_handler("user_registered", send_welcome_email)
app.add_event_handler("payment_processed", update_inventory, priority=10)
# Conditional handlers
app.add_event_handler("order_placed",
send_notification,
condition=lambda data: data.get("urgent", False))
Source code in src/artanis/application.py
add_event_middleware ¶
Add middleware that runs for all events.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
middleware
|
Callable[..., Any]
|
Function that will be called for every event |
required |
Example
Source code in src/artanis/application.py
add_openapi_metadata ¶
add_openapi_metadata(
title: str | None = None,
version: str | None = None,
description: str | None = None,
servers: list[dict[str, str]] | None = None,
tags: list[dict[str, str]] | None = None,
security_schemes: dict[str, dict[str, Any]]
| None = None,
) -> None
Add metadata to OpenAPI specification.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title
|
str | None
|
API title |
None
|
version
|
str | None
|
API version |
None
|
description
|
str | None
|
API description |
None
|
servers
|
list[dict[str, str]] | None
|
List of server objects |
None
|
tags
|
list[dict[str, str]] | None
|
List of tag objects |
None
|
security_schemes
|
dict[str, dict[str, Any]] | None
|
Security scheme definitions |
None
|
Example
app.add_openapi_metadata(
title="My API",
version="2.0.0",
description="A comprehensive REST API",
servers=[
{"url": "https://api.example.com", "description": "Production"},
{"url": "https://staging-api.example.com", "description": "Staging"}
],
tags=[
{"name": "users", "description": "User operations"},
{"name": "auth", "description": "Authentication"}
],
security_schemes={
"bearer": {
"type": "http",
"scheme": "bearer",
"bearerFormat": "JWT"
}
}
)
Source code in src/artanis/application.py
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 |
|
add_openapi_validation ¶
add_openapi_validation(
validate_requests: bool = True,
validate_responses: bool = False,
strict_mode: bool = False,
) -> None
Add OpenAPI request/response validation middleware.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
validate_requests
|
bool
|
Whether to validate incoming requests |
True
|
validate_responses
|
bool
|
Whether to validate outgoing responses |
False
|
strict_mode
|
bool
|
Whether to enforce strict validation |
False
|
Example
Source code in src/artanis/application.py
all ¶
Register a route that responds to all HTTP methods.
This registers the handler for all standard HTTP methods (GET, POST, PUT, DELETE, PATCH, OPTIONS).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
URL path pattern |
required |
handler
|
Callable[..., Any]
|
Route handler function |
required |
Example
Source code in src/artanis/application.py
delete ¶
Register a DELETE route.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
URL path pattern |
required |
handler
|
Callable[..., Any]
|
Route handler function |
required |
emit_event
async
¶
emit_event(
event_name: str,
data: Any = None,
source: str | None = None,
**metadata: Any,
) -> None
Trigger all handlers for an event.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event_name
|
str
|
Name of the event to trigger |
required |
data
|
Any
|
Data to pass to event handlers |
None
|
source
|
str | None
|
Optional source identifier for the event |
None
|
**metadata
|
Any
|
Additional metadata to include in event context |
{}
|
Examples:
# Trigger custom events
await app.emit_event("user_registered", user_data)
await app.emit_event("payment_processed", payment_data, source="stripe")
await app.emit_event("order_completed", order_data, urgent=True)
Source code in src/artanis/application.py
export_openapi ¶
Export OpenAPI specification to a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
Path where to save the specification |
required |
format_type
|
str
|
Export format ("json" or "yaml") |
'json'
|
auto_generate
|
bool
|
Whether to auto-generate spec if not exists |
True
|
Source code in src/artanis/application.py
generate_openapi_spec ¶
generate_openapi_spec(
title: str = "Artanis API",
version: str = "1.0.0",
description: str = "API built with Artanis framework",
) -> dict[str, Any]
Generate OpenAPI specification from registered routes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title
|
str
|
API title |
'Artanis API'
|
version
|
str
|
API version |
'1.0.0'
|
description
|
str
|
API description |
'API built with Artanis framework'
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
OpenAPI specification dictionary |
Example
Source code in src/artanis/application.py
get ¶
Register a GET route.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
URL path pattern |
required |
handler
|
Callable[..., Any]
|
Route handler function |
required |
list_events ¶
Get list of all registered event names.
Returns:
Type | Description |
---|---|
list[str]
|
List of event names that have handlers |
Source code in src/artanis/application.py
mount ¶
Mount a subrouter at the specified path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Path prefix where the subrouter should be mounted |
required |
router
|
Router
|
Router instance to mount |
required |
Source code in src/artanis/application.py
post ¶
Register a POST route.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
URL path pattern |
required |
handler
|
Callable[..., Any]
|
Route handler function |
required |
put ¶
Register a PUT route.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
URL path pattern |
required |
handler
|
Callable[..., Any]
|
Route handler function |
required |
remove_event_handler ¶
Remove a specific event handler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event_name
|
str
|
Name of the event |
required |
handler
|
Callable[..., Any]
|
Handler function to remove |
required |
Returns:
Type | Description |
---|---|
bool
|
True if handler was found and removed, False otherwise |
Source code in src/artanis/application.py
serve_docs ¶
serve_docs(
docs_path: str = "/docs",
redoc_path: str = "/redoc",
openapi_path: str = "/openapi.json",
auto_generate: bool = True,
) -> None
Enable interactive API documentation endpoints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docs_path
|
str
|
Path for Swagger UI documentation |
'/docs'
|
redoc_path
|
str
|
Path for ReDoc documentation |
'/redoc'
|
openapi_path
|
str
|
Path for OpenAPI JSON specification |
'/openapi.json'
|
auto_generate
|
bool
|
Whether to auto-generate OpenAPI spec if not exists |
True
|
Example
Source code in src/artanis/application.py
use ¶
use(
path_or_middleware: str | Callable[..., Any],
middleware: Callable[..., Any] | None = None,
) -> None
Register middleware - Express style app.use() API.
Register middleware either globally or for specific paths.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_or_middleware
|
str | Callable[..., Any]
|
Either a path pattern (str) or middleware function |
required |
middleware
|
Callable[..., Any] | None
|
Middleware function (when first arg is a path) |
None
|
Examples:
# Global middleware
app.use(cors_middleware)
# Path-specific middleware
app.use('/api', auth_middleware)