Responses¶
Define stable response contracts so frontend and integrators can rely on one predictable envelope.
Prerequisites¶
- API Class scaffolded and routed.
config/responses.csvexists in the project.
Implementation¶
Below are practical examples and usage instructions for each response helper function in the Response Kit. These functions create standardized JSON responses with consistent status codes, messages, and data envelopes across Django Rest API endpoints.
1. Json Response¶
Return standardized JSON API envelope using response-code metadata.
Usage:
from django_mindoff.components.response_kit import mo_response_kit
return mo_response_kit.json_response(
code="SUCCESS",
category="success",
data={"id": "abc123"},
)
Parameters:
code: Response code defined inconfig/responses.csv.category: UI/semantic category for clients. One of:danger,warning,info,success.data: Response payload body.exception: Optional exception object for debug/log context.
Behavior:
- HTTP status and message metadata are resolved from
responses.csv. - Unknown/empty
codefalls back toUNEXPECTED_ERR.
2. File Response¶
Return downloadable file response from file path or in-memory bytes.
Usage:
from django_mindoff.components.response_kit import mo_response_kit
import io
return mo_response_kit.file_response("exports/orders.csv")
# OR
buffer = io.BytesIO(b"id,total\n1,30\n")
return mo_response_kit.file_response(
buffer,
filename="orders.csv",
content_type="text/csv",
)
Parameters:
file_obj: File path (str) or in-memory bytes (io.BytesIO).filename: Optional download filename. Auto-generated for BytesIO if omitted.content_type: Optional MIME type override.
3. Text Response¶
Return plain text HTTP response.
Usage:
from django_mindoff.components.response_kit import mo_response_kit
return mo_response_kit.text_response("ok", status_code=200)
4. Html Response¶
Return HTML HTTP response.
Usage:
from django_mindoff.components.response_kit import mo_response_kit
return mo_response_kit.html_response("<h1>Ready</h1>", status_code=200)
Core Concepts¶
1. Standard JSON Envelope¶
json_response(...) returns a consistent envelope:
{
"status": "ok|fail|exception",
"message": {
"code": "SUCCESS",
"title": "Success",
"description": "Operation completed successfully.",
"category": "success"
},
"data": {}
}
Envelope fields:
status: Derived from response code HTTP status (2xx -> ok,4xx -> fail,5xx -> exception).message.code: Stable response code fromresponses.csv.message.titleandmessage.description: Human-readable values loaded fromresponses.csv.message.category: One ofdanger,warning,info,success.data: Payload body (object/list; defaults to[]when empty).
2. Response Codes¶
Response code definitions are loaded from:
Required headers:
codetitledescriptionhttp_status
Practical rules:
codeis normalized to uppercase.- Duplicate codes are rejected during load.
http_statusmust be within200..599.- If a code is missing/unknown, the handler falls back to
UNEXPECTED_ERR.
Keep response codes centralized
The django-mindoff project ships with a standard set of response codes. Do not remove or alter the default codes as they are used by Mindoff's Core features. However, feel free to update their title, description, and http_status as the developer feels fit for the project.
Do not hardcode title/description in endpoint code. Add or update codes in config/responses.csv so behavior stays consistent across APIs.
Troubleshooting¶
Unknown Response code: '<CODE>'
Add the code toconfig/responses.csvor use an existing code.- Startup/load failures for responses file
Ensureconfig/responses.csvexists and includes required headers. - Unexpected
statusin JSON envelope
Verifyhttp_statusconfigured for the code inresponses.csv.