A simple package to generate structured HTTP responses with trace ID.
Project description
structured-response
A lightweight Python package to generate structured HTTP responses with optional trace IDs. Simplifies API response consistency in Flask-based microservices.
Installation
To install the package, run:
pip install structured-response
Alternatively, install directly from the source:
pip install git+https://github.com/rohaan2614/structured-response.git
Usage
Example 1: Basic Response
from structured_response import generate_response
response = generate_response(
status_code=200,
message="Hello, world!"
)
print(response.get_json()) # {"status": "Success", "message": "Hello, world!"}
Example 2: Custom Trace ID
from structured_response import generate_response
response = generate_response(
status_code=404,
trace_id="abc123",
message="Resource not found",
user="John Doe"
)
print(response.get_json())
# Output:
# {
# "status": "Client Error",
# "message": "Resource not found",
# "user": "John Doe"
# }
Function Reference
generate_response(status_code: int = 200, trace_id: str None, message: str = None, **kwargs) -> Response
Generates a structured HTTP response.
| Parameter | Type | Description |
|---|---|---|
status_code |
int |
HTTP status code (default: 200) |
trace_id |
str |
None |
message |
str |
None |
**kwargs |
dict |
Additional key-value pairs to include in the response body |
Development
Clone the repository:
git clone https://github.com/rohaan2614/structured-response.git
cd structured-response
Install dependencies:
pip install -r requirements.txt
Changelog
v0.1.1
- Updated package to use
pyproject.tomlfor PEP 625 compliance. - No functional changes to the code.
v0.1.0
- Initial release 🎉
- Added
generate_responsefunction with optionaltrace_idsupport. - Categorizes status codes (
Informational,Success, etc.).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file structured_response-0.1.2.tar.gz.
File metadata
- Download URL: structured_response-0.1.2.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
008f2a09b786aa3101b358b1cad35c0a4abb882a6aab26de7e6ff858d847d720
|
|
| MD5 |
f92702816133bf4164a002e4adec2ff1
|
|
| BLAKE2b-256 |
61172d280c3c36637313d262967cb527b9d95a1b5c613aed6f67a84cbaccc389
|
File details
Details for the file structured_response-0.1.2-py3-none-any.whl.
File metadata
- Download URL: structured_response-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cf6cb59040f43356938c4b37ebc6fac590b046d16e7deb2e1d117548a70cfc48
|
|
| MD5 |
afe380eae505e8d57265be30be668a20
|
|
| BLAKE2b-256 |
744d7a813d3aaaaa20d8211aac2966d5b445cb97ee649bca721262ae04605da6
|