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, SmarterHelperMixin

An 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, and json_dumps_params are also supported.

Example:

The response of a describe of a Guardrail, beside the view’s own data:

{
    "data": {...},
    "api": "smarter.sh/v1",
    "metadata": {
        "command": "describe",
        "thing": "Guardrail",
        "key": "4f1c...e9a2"
    }
}

The metadata’s key is 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:

Parameters:
  • request (HttpRequest) – The original Django request object.

  • data (Any) – The response data. A dict gets the api and metadata keys. A list is sent as it is, and anything else is wrapped as {"response": data} first.

  • encoder (type[JSONEncoder]) – JSON encoder class. Defaults to smarter.lib.json.SmarterJSONEncoder.

  • safe (bool) – Controls if only dict objects may be serialized. Defaults to True, and only applies when data is 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 for json.dumps().

  • status (int) – The HTTP status code. Defaults to 200.

  • journal_sources (Sequence[SAMJournalSource]) – The parts of data that 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. See smarter.lib.journal.sources.

  • kwargs – Of these, only content_type, status, headers and reason are passed to django.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_failed is sent, whose receiver logs the gap in the journal, and the response is returned without a key.

Parameters:
Return type:

str | None

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:

dict[str, Any]

Returns:

The serialized request, or an empty dict if it could not be serialized, e.g. an ASGIRequest without GET.