Handlers
lightapi.handlers
Attributes
Classes
AbstractHandler
dataclass
Bases: ABC
Abstract base class for handling HTTP requests related to a specific model.
Attributes:
Name | Type | Description |
---|---|---|
model |
Base
|
The SQLAlchemy model class to operate on. |
Source code in lightapi/handlers.py
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 |
|
Attributes
Functions
handle
abstractmethod
async
Abstract method to handle the HTTP request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db |
Session
|
The SQLAlchemy session for database operations. |
required |
request |
Request
|
The aiohttp web request object. |
required |
Raises:
Type | Description |
---|---|
NotImplementedError
|
If the method is not implemented by subclasses. |
Source code in lightapi/handlers.py
__call__
async
Calls the handler with the provided request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
Request
|
The aiohttp web request object. |
required |
Returns:
Type | Description |
---|---|
web.Response: The response returned by the handler. |
Source code in lightapi/handlers.py
get_request_json
async
Extracts JSON data from the request body.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
Request
|
The aiohttp web request object. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
The JSON data from the request body. |
Source code in lightapi/handlers.py
get_item_by_id
Retrieves an item by its primary key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db |
Session
|
The SQLAlchemy session for database operations. |
required |
item_id |
int
|
The primary key of the item to retrieve. |
required |
Returns:
Name | Type | Description |
---|---|---|
Base |
The item retrieved from the database, or None if not found. |
Source code in lightapi/handlers.py
add_and_commit_item
Adds and commits a new item to the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db |
Session
|
The SQLAlchemy session for database operations. |
required |
item |
Base
|
The item to add and commit. |
required |
Returns:
Name | Type | Description |
---|---|---|
Base |
The item after committing to the database. |
Source code in lightapi/handlers.py
delete_and_commit_item
Deletes and commits the removal of an item from the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db |
Session
|
The SQLAlchemy session for database operations. |
required |
item |
Base
|
The item to delete. |
required |
Source code in lightapi/handlers.py
json_response
Creates a JSON response for the given item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item |
Base
|
The item to serialize and return. |
required |
status |
int
|
The HTTP status code. Defaults to 200. |
200
|
Returns:
Type | Description |
---|---|
web.Response: The JSON response containing the serialized item. |
Source code in lightapi/handlers.py
json_error_response
Creates a JSON response for an error message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
error_message |
str
|
The error message to return. |
required |
status |
int
|
The HTTP status code. Defaults to 404. |
404
|
Returns:
Type | Description |
---|---|
web.Response: The JSON response containing the error message. |
Source code in lightapi/handlers.py
CreateHandler
dataclass
Bases: AbstractHandler
Handles HTTP POST requests to create a new item.
Source code in lightapi/handlers.py
Functions
handle
async
Processes the POST request to create and save a new item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db |
Session
|
The SQLAlchemy session for database operations. |
required |
request |
Request
|
The aiohttp web request object. |
required |
Returns:
Type | Description |
---|---|
web.Response: The JSON response containing the created item. |
Source code in lightapi/handlers.py
ReadHandler
dataclass
Bases: AbstractHandler
Handles HTTP GET requests to retrieve one or all items.
Source code in lightapi/handlers.py
Functions
handle
async
Processes the GET request to retrieve an item by ID or all items.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db |
Session
|
The SQLAlchemy session for database operations. |
required |
request |
Request
|
The aiohttp web request object. |
required |
Returns:
Type | Description |
---|---|
web.Response: The JSON response containing the item(s) or an error message. |
Source code in lightapi/handlers.py
UpdateHandler
dataclass
Bases: AbstractHandler
Handles HTTP PUT requests to update an existing item.
Source code in lightapi/handlers.py
Functions
handle
async
Processes the PUT request to update an existing item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db |
Session
|
The SQLAlchemy session for database operations. |
required |
request |
Request
|
The aiohttp web request object. |
required |
Returns:
Type | Description |
---|---|
web.Response: The JSON response containing the updated item or an error message. |
Source code in lightapi/handlers.py
PatchHandler
dataclass
Bases: AbstractHandler
Handles HTTP PATCH requests to partially update an existing item.
Source code in lightapi/handlers.py
Functions
handle
async
Processes the PATCH request to partially update an existing item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db |
Session
|
The SQLAlchemy session for database operations. |
required |
request |
Request
|
The aiohttp web request object. |
required |
Returns:
Type | Description |
---|---|
web.Response: The JSON response containing the updated item or an error message. |
Source code in lightapi/handlers.py
DeleteHandler
dataclass
Bases: AbstractHandler
Handles HTTP DELETE requests to delete an existing item.
Source code in lightapi/handlers.py
Functions
handle
async
Processes the DELETE request to remove an existing item.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db |
Session
|
The SQLAlchemy session for database operations. |
required |
request |
Request
|
The aiohttp web request object. |
required |
Returns:
Type | Description |
---|---|
web.Response: An empty response with status 204 if the item is deleted. |
Source code in lightapi/handlers.py
RetrieveAllHandler
dataclass
Bases: AbstractHandler
Handles HTTP GET requests to retrieve all items.
Source code in lightapi/handlers.py
Functions
handle
async
Processes the GET request to retrieve all items.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db |
Session
|
The SQLAlchemy session for database operations. |
required |
request |
Request
|
The aiohttp web request object. |
required |
Returns:
Type | Description |
---|---|
web.Response: The JSON response containing all items. |
Source code in lightapi/handlers.py
OptionsHandler
dataclass
Bases: AbstractHandler
Handles HTTP OPTIONS requests to provide allowed methods and headers.
Source code in lightapi/handlers.py
Functions
handle
async
Processes the OPTIONS request to provide allowed methods and headers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db |
Session
|
The SQLAlchemy session for database operations. |
required |
request |
Request
|
The aiohttp web request object. |
required |
Returns:
Type | Description |
---|---|
web.Response: The JSON response containing allowed methods and headers. |
Source code in lightapi/handlers.py
HeadHandler
dataclass
Bases: AbstractHandler
Handles HTTP HEAD requests to check the resource existence.
Source code in lightapi/handlers.py
Functions
handle
async
Processes the HEAD request to check the resource existence.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db |
Session
|
The SQLAlchemy session for database operations. |
required |
request |
Request
|
The aiohttp web request object. |
required |
Returns:
Type | Description |
---|---|
web.Response: An empty response with status 200. |
Source code in lightapi/handlers.py
Functions
create_handler
create_handler(model: Type[Base]) -> List[RouteDef]
Creates a list of route handlers for the given model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Base
|
The SQLAlchemy model class for which to create handlers. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
List[RouteDef]
|
A list of aiohttp web routes for the specified model. |