auto_rest.handlers¶
An endpoint handler is a function designed to process incoming HTTP
requests for single API endpoint. In auto_rest
, handlers are
created dynamically using a factory pattern. This approach allows
handler logic to be customized and reused across multiple endpoints.
Example: Creating a Handler
New endpoint handlers are created dynamically using factory methods.
welcome_handler = create_welcome_handler()
Handler functions are defined as asynchronous coroutines. This provides improved performance when handling large numbers of incoming requests.
Example: Async Handlers
Python requires asynchronous coroutines to be run from an asynchronous
context. In the following example, this is achieved using asyncio.run
.
import asyncio
return_value = asyncio.run(welcome_handler())
Handlers are specifically designed to integrate with the FastAPI framework, including support for FastAPI's type hinting and data validation capabilities. This makes it easy to incorporate handlers into a FastAPI application.
Example: Adding a Handler to an Application
Use the add_api_route
method to dynamically add handler functions to
an existing application instance.
app = FastAPI(...)
handler = create_welcome_handler()
app.add_api_route("/", handler, methods=["GET"], ...)
Developer Note
FastAPI internally performs post-processing on values returned by endpoint handlers before sending them in an HTTP response. For this reason, handlers should always be tested within the context of a FastAPI application.
apply_ordering_params(query, order_by=None, direction='asc')
¶
Apply ordering to a database query.
Returns a copy of the provided query with ordering parameters applied.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
Select
|
The database query to apply parameters to. |
required |
order_by
|
str | None
|
The name of the column to order by. |
None
|
direction
|
Literal['desc', 'asc']
|
The direction to order by (defaults to "asc"). |
'asc'
|
Returns:
Type | Description |
---|---|
Select
|
A copy of the query modified to return ordered values. |
Source code in auto_rest/queries.py
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 |
|
apply_pagination_params(query, limit=0, offset=0)
¶
Apply pagination to a database query.
Returns a copy of the provided query with offset and limit parameters applied.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
Select
|
The database query to apply parameters to. |
required |
limit
|
int
|
The number of results to return. |
0
|
offset
|
int
|
The offset to start with. |
0
|
Returns:
Type | Description |
---|---|
Select
|
A copy of the query modified to only return the paginated values. |
Source code in auto_rest/queries.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
commit_session(session)
async
¶
Commit a SQLAlchemy session.
Supports synchronous and asynchronous sessions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session
|
DBSession
|
The session to commit. |
required |
Source code in auto_rest/queries.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
create_about_handler(name, version)
¶
Create an endpoint handler that returns the application name and version number.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The application name. |
required |
version
|
str
|
The returned version identifier. |
required |
Returns:
Type | Description |
---|---|
Callable[[], Awaitable[BaseModel]]
|
An async function that returns application info. |
Source code in auto_rest/handlers.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
create_db_engine(url, **kwargs)
¶
Initialize a new database engine.
Instantiates and returns an Engine
or AsyncEngine
instance depending
on whether the database URL uses a driver with support for async operations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
URL
|
A fully qualified database URL. |
required |
**kwargs
|
dict[str:any]
|
Keyword arguments passed to |
{}
|
Returns:
Type | Description |
---|---|
DBEngine
|
A SQLAlchemy |
Source code in auto_rest/models.py
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 |
|
create_db_metadata(engine)
¶
Create and reflect metadata for the database connection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine
|
DBEngine
|
The database engine to use for reflection. |
required |
Returns:
Type | Description |
---|---|
MetaData
|
A MetaData object reflecting the database schema. |
Source code in auto_rest/models.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|
create_db_url(driver, database, host=None, port=None, username=None, password=None)
¶
Create a database URL from the provided parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
driver
|
str
|
The SQLAlchemy-compatible database driver. |
required |
database
|
str
|
The database name or file path (for SQLite). |
required |
host
|
str | None
|
The database server hostname or IP address. |
None
|
port
|
int | None
|
The database server port number. |
None
|
username
|
str | None
|
The username for authentication. |
None
|
password
|
str | None
|
The password for the database user. |
None
|
Returns:
Type | Description |
---|---|
URL
|
A fully qualified database URL. |
Source code in auto_rest/models.py
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 |
|
create_delete_record_handler(engine, table)
¶
Create a function for handling DELETE requests against a record in the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine
|
DBEngine
|
Database engine to use when executing queries. |
required |
table
|
Table
|
The database table to query against. |
required |
Returns:
Type | Description |
---|---|
Callable[..., Awaitable[None]]
|
An async function that handles record deletion. |
Source code in auto_rest/handlers.py
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 |
|
create_engine_handler(engine)
¶
Create an endpoint handler that returns configuration details for a database engine.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine
|
DBEngine
|
Database engine to return the configuration for. |
required |
Returns:
Type | Description |
---|---|
Callable[[], Awaitable[BaseModel]]
|
An async function that returns database metadata. |
Source code in auto_rest/handlers.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
|
create_get_record_handler(engine, table)
¶
Create a function for handling GET requests against a single record in the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine
|
DBEngine
|
Database engine to use when executing queries. |
required |
table
|
Table
|
The database table to query against. |
required |
Returns:
Type | Description |
---|---|
Callable[..., Awaitable[BaseModel]]
|
An async function that returns a single record from the given database table. |
Source code in auto_rest/handlers.py
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 |
|
create_interface(table, pk_only=False, mode='default')
¶
Create a Pydantic interface for a SQLAlchemy model where all fields are required.
Modes
default: Values are marked as (not)required based on the column schema. required: Values are always marked required. required: Values are always marked optional.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table
|
Table
|
The SQLAlchemy table to create an interface for. |
required |
pk_only
|
bool
|
If True, only include primary key columns. |
False
|
mode
|
MODE_TYPE
|
Whether to force fields to all be optional or required. |
'default'
|
Returns:
Type | Description |
---|---|
type[BaseModel]
|
A dynamically generated Pydantic model with all fields required. |
Source code in auto_rest/interfaces.py
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 |
|
create_list_records_handler(engine, table)
¶
Create an endpoint handler that returns a list of records from a database table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine
|
DBEngine
|
Database engine to use when executing queries. |
required |
table
|
Table
|
The database table to query against. |
required |
Returns:
Type | Description |
---|---|
Callable[..., Awaitable[list[BaseModel]]]
|
An async function that returns a list of records from the given database model. |
Source code in auto_rest/handlers.py
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 |
|
create_patch_record_handler(engine, table)
¶
Create a function for handling PATCH requests against a record in the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine
|
DBEngine
|
Database engine to use when executing queries. |
required |
table
|
Table
|
The database table to query against. |
required |
Returns:
Type | Description |
---|---|
Callable[..., Awaitable[BaseModel]]
|
An async function that handles record updates. |
Source code in auto_rest/handlers.py
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 |
|
create_post_record_handler(engine, table)
¶
Create a function for handling POST requests against a record in the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine
|
DBEngine
|
Database engine to use when executing queries. |
required |
table
|
Table
|
The database table to query against. |
required |
Returns:
Type | Description |
---|---|
Callable[..., Awaitable[BaseModel]]
|
An async function that handles record creation. |
Source code in auto_rest/handlers.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
|
create_put_record_handler(engine, table)
¶
Create a function for handling PUT requests against a record in the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine
|
DBEngine
|
Database engine to use when executing queries. |
required |
table
|
Table
|
The database table to query against. |
required |
Returns:
Type | Description |
---|---|
Callable[..., Awaitable[BaseModel]]
|
An async function that handles record updates. |
Source code in auto_rest/handlers.py
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 |
|
create_schema_handler(metadata)
¶
Create an endpoint handler that returns the database schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
metadata
|
MetaData
|
Metadata object containing the database schema. |
required |
Returns:
Type | Description |
---|---|
Callable[[], Awaitable[BaseModel]]
|
An async function that returns the database schema. |
Source code in auto_rest/handlers.py
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 |
|
create_session_iterator(engine)
¶
Create a generator for database sessions.
Returns a synchronous or asynchronous function depending on whether
the database engine supports async operations. The type of session
returned also depends on the underlying database engine, and will
either be a Session
or AsyncSession
instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine
|
DBEngine
|
Database engine to use when generating new sessions. |
required |
Returns:
Type | Description |
---|---|
Callable[[], DBSession]
|
A function that yields a single new database session. |
Source code in auto_rest/models.py
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 |
|
create_welcome_handler()
¶
Create an endpoint handler that returns an application welcome message.
Returns:
Type | Description |
---|---|
Callable[[], Awaitable[BaseModel]]
|
An async function that returns a welcome message. |
Source code in auto_rest/handlers.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
delete_session_record(session, record)
async
¶
Delete a record from the database using an existing session.
Does not automatically commit the session. Supports synchronous and asynchronous sessions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session
|
DBSession
|
The session to use for deletion. |
required |
record
|
Result
|
The record to be deleted. |
required |
Source code in auto_rest/queries.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
execute_session_query(session, query)
async
¶
Execute a query in the given session and return the result.
Supports synchronous and asynchronous sessions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session
|
DBSession
|
The SQLAlchemy session to use for executing the query. |
required |
query
|
Executable
|
The query to be executed. |
required |
Returns:
Type | Description |
---|---|
Result
|
The result of the executed query. |
Source code in auto_rest/queries.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
|
get_record_or_404(result)
¶
Retrieve a scalar record from a query result or raise a 404 error.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
result
|
Result
|
The query result to extract the scalar record from. |
required |
Returns:
Type | Description |
---|---|
any
|
The scalar record if it exists. |
Raises:
Type | Description |
---|---|
HTTPException
|
If the record is not found. |
Source code in auto_rest/queries.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
parse_db_settings(path)
¶
Parse engine configuration settings from a given file path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Path | None
|
Path to the configuration file. |
required |
Returns:
Type | Description |
---|---|
dict[str, any]
|
Engine configuration settings. |
Source code in auto_rest/models.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|