Httpapi Module API Reference¶
This section documents the internals of the httpapi
module in Bijux CLI.
bijux_cli.httpapi ¶
Provides a self-contained FastAPI application for a CRUD API.
This module defines a complete HTTP API for managing "Item" resources using the FastAPI framework. It includes all necessary components for a functional web service.
Services
- Pydantic Models:
ItemIn
,Item
, and response models for data validation and serialization. - Storage Layer: A formal
ItemStoreProtocol
and a concrete, thread-safeInMemoryItemStore
implementation. - API Endpoints: A FastAPI
APIRouter
with path operations for all CRUD (Create, Read, Update, Delete) actions. - Application Lifecycle: A
lifespan
manager to prepopulate and clear the data store on startup and shutdown.
InMemoryItemStore ¶
Bases: ItemStoreProtocol
A thread-safe, in-memory implementation of the ItemStoreProtocol
.
Attributes:
-
_items
(dict
) –The main dictionary storing items by their ID.
-
_name_index
(dict
) –An index to enforce unique item names.
-
_lock
(RLock
) –A lock to ensure thread-safe operations.
-
_next_id
(int
) –A counter for generating new item IDs.
Initializes the in-memory item store.
Source code in src/bijux_cli/httpapi.py
create ¶
Creates a new item.
Parameters:
-
data
(ItemIn
) –The data for the new item.
Returns:
-
Item
–The newly created item, including its generated ID.
Raises:
-
HTTPException
–With status 409 if an item with the same name exists.
Source code in src/bijux_cli/httpapi.py
delete ¶
Delete an item by its unique ID.
Parameters:
-
item_id
(int
) –The unique ID of the item to delete.
Source code in src/bijux_cli/httpapi.py
find_by_name ¶
find_by_name(name: str) -> Item | None
Lookup an item by its name (case-insensitive, trimmed).
Source code in src/bijux_cli/httpapi.py
get ¶
get(item_id: int) -> Item
Gets an item by its unique ID.
Parameters:
-
item_id
(int
) –The ID of the item to retrieve.
Returns:
-
Item
–The requested item.
Raises:
-
HTTPException
–With status 404 if the item is not found.
Source code in src/bijux_cli/httpapi.py
list_items ¶
list_items(
limit: int, offset: int
) -> tuple[list[Item], int]
Lists items with pagination in a thread-safe manner.
Parameters:
-
limit
(int
) –The maximum number of items to return.
-
offset
(int
) –The starting index for the items to return.
Returns:
-
tuple[list[Item], int]
–A tuple containing the list of items and the total number of items.
Source code in src/bijux_cli/httpapi.py
prepopulate ¶
Prepopulates the store with a list of items.
Parameters:
-
data
(list[dict[str, Any]]
) –A list of dictionaries, where each dictionary contains the data for a new item.
Source code in src/bijux_cli/httpapi.py
reset ¶
update ¶
Update an existing item.
Parameters:
-
item_id
(int
) –The unique identifier of the item to update.
-
data
(ItemIn
) –The new values for the item.
Returns:
-
Item
–The updated item.
Raises:
-
HTTPException
–If the item does not exist (HTTP 404) or if the new name conflicts with another item (HTTP 409).
Source code in src/bijux_cli/httpapi.py
Item ¶
Bases: ItemIn
Defines the full item model, including its unique identifier.
Attributes:
-
id
(int
) –The unique identifier for the item.
-
name
(str
) –The name of the item.
-
description
(str | None
) –An optional description for the item.
ItemIn ¶
Bases: BaseModel
Defines the input model for creating or updating an item.
Attributes:
-
name
(str
) –The name of the item.
-
description
(str | None
) –An optional description for the item.
validate_and_normalize_name classmethod
¶
Strips whitespace and ensures the name is not empty.
Parameters:
-
v
(str
) –The input string for the item's name.
Returns:
-
str
–The validated and stripped name.
Raises:
-
ValueError
–If the name is empty or contains only whitespace.
Source code in src/bijux_cli/httpapi.py
ItemListResponse ¶
Bases: BaseModel
Defines the response model for a paginated list of items.
Attributes:
-
items
(list[Item]
) –The list of items on the current page.
-
total
(int
) –The total number of items available.
ItemStoreProtocol ¶
Problem ¶
Bases: BaseModel
Defines a standard RFC 7807 problem details response.
Attributes:
-
type
(AnyUrl
) –A URI reference that identifies the problem type.
-
title
(str
) –A short, human-readable summary of the problem type.
-
status
(int
) –The HTTP status code.
-
detail
(str
) –A human-readable explanation specific to this occurrence.
-
instance
(str
) –A URI reference that identifies the specific occurrence.
allow_only ¶
Create a dependency that rejects unknown query parameters (HTTP 422).
Parameters:
-
*allowed
(str
, default:()
) –The set of query parameter names that are permitted.
Returns:
-
Callable[[Request], Awaitable[None]]
–Callable[[Request], Awaitable[None]]: An async dependency suitable for
-
Callable[[Request], Awaitable[None]]
–FastAPI's
dependencies=[...]
. It raises anHTTPException
with -
Callable[[Request], Awaitable[None]]
–status 422 if the request includes parameters outside the allowlist.
Raises:
-
HTTPException
–Emitted by the returned dependency at request time when unknown query parameters are present (422 Unprocessable Entity).
Source code in src/bijux_cli/httpapi.py
create_item ¶
create_item(
response: Response,
item: ItemIn = Body(...),
store: ItemStoreProtocol = Depends(get_store),
) -> Item
Creates a new item.
Parameters:
-
item
(ItemIn
, default:Body(...)
) –The data for the new item from the request body.
-
store
(ItemStoreProtocol
, default:Depends(get_store)
) –The dependency-injected item store.
Returns:
-
Item
(Item
) –The newly created item, including its server-generated ID.
Source code in src/bijux_cli/httpapi.py
delete_item ¶
delete_item(
item: Annotated[Item, Depends(get_item_or_404)],
store: Annotated[ItemStoreProtocol, Depends(get_store)],
) -> Response
Delete an item by its unique ID.
The target item is resolved by the get_item_or_404
dependency before this handler runs.
Parameters:
-
item
(Annotated[Item, Depends(get_item_or_404)]
) –The item to delete, injected by
get_item_or_404
. -
store
(Annotated[ItemStoreProtocol, Depends(get_store)]
) –The item store implementation, injected.
Returns:
-
Response
(Response
) –Empty body with 204 No Content on successful deletion.
Raises:
-
HTTPException
–404 if the item does not exist (raised by the dependency).
Source code in src/bijux_cli/httpapi.py
get_item ¶
get_item(item: Item = Depends(get_item_or_404)) -> Item
Retrieves a single item by its ID.
This endpoint uses a dependency (get_item_or_404
) to fetch the item, ensuring that a 404 response is returned if the item does not exist.
Parameters:
-
item
(Item
, default:Depends(get_item_or_404)
) –The item retrieved by the
get_item_or_404
dependency.
Returns:
-
Item
(Item
) –The requested item.
Source code in src/bijux_cli/httpapi.py
get_item_or_404 ¶
get_item_or_404(
item_id: int = Path(..., ge=1),
store: ItemStoreProtocol = Depends(get_store),
) -> Item
A dependency that retrieves an item by ID or raises a 404.
get_store ¶
get_store() -> ItemStoreProtocol
health async
¶
http_exception_handler async
¶
A custom exception handler for HTTPException
.
This handler intercepts FastAPI's standard HTTP exceptions and ensures they are logged and returned in the standard JSON error format.
Parameters:
-
request
(Request
) –The incoming request.
-
exc
(HTTPException
) –The HTTP exception.
Returns:
-
JSONResponse
(JSONResponse
) –A JSON response detailing the HTTP error.
Source code in src/bijux_cli/httpapi.py
lifespan async
¶
Manages the application's lifespan events for startup and shutdown.
On startup, this context manager resets and prepopulates the in-memory store with demo data. On shutdown, it resets the store again.
Parameters:
-
app
(FastAPI
) –The FastAPI application instance.
Yields:
-
None
(AsyncIterator[None]
) –Yields control to the application while it is running.
Source code in src/bijux_cli/httpapi.py
list_items ¶
list_items(
limit: int = Query(10, ge=1, le=100),
offset: int = Query(0, ge=0),
store: ItemStoreProtocol = Depends(get_store),
) -> ItemListResponse
Retrieves a paginated list of items.
Parameters:
-
limit
(int
, default:Query(10, ge=1, le=100)
) –The maximum number of items per page.
-
offset
(int
, default:Query(0, ge=0)
) –The starting offset for the item list.
-
store
(ItemStoreProtocol
, default:Depends(get_store)
) –The dependency-injected item store.
Returns:
-
ItemListResponse
(ItemListResponse
) –An object containing the list of items and total count.
Source code in src/bijux_cli/httpapi.py
reject_duplicate_query_params ¶
Create a dependency that rejects duplicate query parameters (HTTP 422).
Parameters:
-
*params
(str
, default:()
) –Names of query parameters that must not appear more than once.
Returns:
-
Depends
–fastapi.params.Depends: A dependency marker that, when executed at
-
Depends
–request time, raises
HTTPException
(422) if any listed parameter -
Depends
–appears more than once.
Raises:
-
HTTPException
–Emitted at request time if duplicates are detected.
Source code in src/bijux_cli/httpapi.py
require_accept_json ¶
Reject requests that don't accept application/json (HTTP 406).
Schemathesis' negative-data checks may send unsupported Accept headers. If the client doesn't accept JSON (and not /), respond with 406.
Source code in src/bijux_cli/httpapi.py
update_item ¶
update_item(
item: Annotated[Item, Depends(get_item_or_404)],
update_data: Annotated[ItemIn, Body(...)],
store: Annotated[ItemStoreProtocol, Depends(get_store)],
) -> Item
Update an existing item.
Parameters:
-
item
(Item
) –The current item resolved from the path parameter, injected by
get_item_or_404
. -
update_data
(ItemIn
) –The new values for the item (request body).
-
store
(ItemStoreProtocol
) –The item store implementation (injected).
Returns:
-
Item
–The updated item.
Raises:
-
HTTPException
–If the item does not exist (404) or if the new name conflicts with another item (409). These are raised by the dependency or the store layer.
-
RequestValidationError
–If the path/body validation fails (422). Handled by the global validation exception handler.
Source code in src/bijux_cli/httpapi.py
validation_exception_handler async
¶
A custom exception handler for RequestValidationError
.
This handler intercepts validation errors from FastAPI and formats them into a standard JSONResponse
with a 422 status code.
Parameters:
-
request
(Request
) –The incoming request.
-
exc
(RequestValidationError
) –The validation exception.
Returns:
-
JSONResponse
(JSONResponse
) –A JSON response detailing the validation error.