SmarterJournaledJsonResponse
- class smarter.lib.journal.http.SmarterJournaledJsonResponse(request, data, encoder=<class 'smarter.lib.json.SmarterJSONEncoder'>, safe=True, thing=None, command=None, json_dumps_params=None, status=200, journal_sources=(), **kwargs)[source]
Bases:
JsonResponse,SmarterHelperMixinAn enhanced HTTP response class for the Smarter API that augments standard Django JSON responses with additional manifest structure and metadata.
This class is designed to provide a consistent response format for all Smarter API endpoints, embedding contextual information about the request and operation performed. It automatically attaches metadata such as the API version, the entity (“thing”) being operated on, and the command executed. When journaling is enabled, it also queues a corresponding journal entry, capturing the request, response, user, and status code for audit and traceability. The entry’s key is chosen, and returned in the metadata, before a Celery worker saves the entry, so the entry may not exist yet when the client receives the response.
Smarter-specific parameters include the original Django request object, the noun (“thing”) being journaled, the command performed, and the API response data. Standard Django JsonResponse parameters such as
data,encoder,safe, andjson_dumps_paramsare also supported.Example:
The response of a
describeof a Guardrail, beside the view’s owndata:{ "data": {...}, "api": "smarter.sh/v1", "metadata": { "command": "describe", "thing": "Guardrail", "key": "4f1c...e9a2" } }
The metadata’s
keyis the journal entry’s key: it is absent while the journal is disabled, and when the entry could be neither queued nor saved.See also:
SAMJournal for the database model used to store journal entries.
django.http.JsonResponsefor inherited response behavior.smarter.common.utils.hash_factory()for key generation.smarter.lib.django.http.serializersfor request serialization.smarter.lib.journal.contract.models.SAMJournalApiEnvelope, the contract of theapiandmetadatakeys.
- Parameters:
request (
HttpRequest) – The original Django request object.data (
Any) – The response data. A dict gets theapiandmetadatakeys. A list is sent as it is, and anything else is wrapped as{"response": data}first.encoder (
type[JSONEncoder]) – JSON encoder class. Defaults tosmarter.lib.json.SmarterJSONEncoder.safe (
bool) – Controls if onlydictobjects may be serialized. Defaults toTrue, and only applies whendatais a dict.thing (
SmarterJournalThings|str|None) – The resource or entity being operated on (noun).command (
SmarterJournalCliCommands|None) – The CLI command executed on the resource.json_dumps_params (
dict[str,Any] |None) – Additional kwargs forjson.dumps().status (
int) – The HTTP status code. Defaults to 200.journal_sources (
Sequence[SAMJournalSource]) – The parts ofdatathat Smarter has already saved, e.g. a chat thread, which the journal’s worker reads from the database instead of receiving them through the Celery broker. Seesmarter.lib.journal.sources.kwargs – Of these, only
content_type,status,headersandreasonare passed todjango.http.JsonResponse. The rest are ignored.
- __init__(request, data, encoder=<class 'smarter.lib.json.SmarterJSONEncoder'>, safe=True, thing=None, command=None, json_dumps_params=None, status=200, journal_sources=(), **kwargs)[source]
Note: this needs to exist; something in the Python MRO requires it, even if it does nothing.
If you remove this, you will get a mysterious error about something downstream expecting exactly one object.
- journal(request, data, thing, command, status, journal_sources)[source]
Queue the response’s journal entry, to be saved by a Celery worker.
The entry’s key is chosen now, so that the response can return it. If the entry can be neither queued nor saved,
journal_entry_failedis sent, whose receiver logs the gap in the journal, and the response is returned without a key.- Parameters:
request (
HttpRequest) – The request.data (
dict[str,Any] |list[Any]) – The response’s data, with its envelope.thing (
SmarterJournalThings|str|None) – The resource or entity being operated on.command (
SmarterJournalCliCommands|None) – The command executed on the resource.status (
int) – The http status code.journal_sources (
Sequence[SAMJournalSource]) – The parts ofdatathat the worker reads from the database.
- Return type:
- Returns:
The entry’s key, or None if it could not be queued.
- serialized_request(request, is_authenticated)[source]
Return the request, serialized for the journal.
- Parameters:
request (
HttpRequest) – The request.is_authenticated (
bool) – Whether the request’s user is authenticated, whose username is then serialized too.
- Return type:
- Returns:
The serialized request, or an empty dict if it could not be serialized, e.g. an
ASGIRequestwithoutGET.