Skip to main content

A simple Python package for standardized API responses.

Project description

PyResponse-paradox 🚀

A lightweight and structured response builder for Flask and FastAPI applications.

📌 Overview

PyResponse-paradox provides a consistent and structured way to return API responses in Flask and FastAPI applications.
It helps standardize response formats while supporting custom messages, error codes, and flexible status handling.

📖 Features

✅ Standardized JSON response format
✅ Supports both Flask and FastAPI frameworks
✅ Predefined response helpers for common HTTP status codes
✅ Customizable messages, data, and error codes
✅ Fully tested with pytest

📦 Installation

pip install -r requirements.txt

pip install flask fastapi pydantic pytest

🚀 Usage

🔹 Import & Use in Flask

from flask import Flask
from responses import Ok, BadRequest, NotFound

app = Flask(__name__)

@app.route("/success")
def success():
    return Ok({"user": "John Doe"}, message="Request successful!")

@app.route("/error")
def error():
    return BadRequest(message="Invalid request")

if __name__ == "__main__":
    app.run(debug=True)

🔹 Import & Use in FastAPI

from fastapi import FastAPI
from responses import Ok, NotFound

app = FastAPI()

@app.get("/data")
def get_data():
    return Ok({"id": 1, "name": "Alice"})

@app.get("/missing")
def missing():
    return NotFound(message="Item not found")

##🔧 Response Structure

Every response follows a structured JSON format:

{
    "status": 200,
    "message": "Success",
    "data": {...},
    "error_code": "OPTIONAL"
}

##🛠 Response Builder Function

def build_response(status_code, data=None, message=None, error_code=None):
    response_body = {"status": status_code}

    if message:
        response_body["message"] = message
    if data is not None:
        response_body["data"] = data
    if error_code:
        response_body["error_code"] = error_code

    return JSONResponse(content=response_body, status_code=status_code)

##📡 Available Response Helpers

✅ Success Responses

Function HTTP Code Default Message Example
Ok(data, message) 200 "Success" Ok({"id": 1})
Created(data, message) 201 "Created successfully" Created({"user": "Alice"})
NoContent(message) 204 "No content" NoContent()

❌ Error Responses

Function HTTP Code Default Message Error Code
BadRequest(message, error_code) 400 "Bad request" "BAD_REQUEST"
Unauthorized(message, error_code) 401 "Unauthorized" "UNAUTHORIZED"
Forbidden(message, error_code) 403 "Forbidden" "FORBIDDEN"
NotFound(message, error_code) 404 "Not found" "NOT_FOUND"
InternalServerError(message, error_code) 500 "Internal server error" "INTERNAL_SERVER_ERROR"
ServiceUnavailable(message, error_code) 503 "Service unavailable" "SERVICE_UNAVAILABLE"

🧪 Running Tests

Ensure all tests pass before deploying:

pytest

##Sample output when successful:

================================================== test session starts ===================================================
collected 12 items
tests/test_responses.py ............                                                                                [100%]
=================================================== 12 passed in 1.06s ===================================================

##📁 Project Structure

PyResponse/
│── responses.py               # Main response helper functions
│── tests/
│   ├── test_responses.py      # Unit tests for response functions
│── .github/workflows/         # GitHub CI/CD pipeline (if applicable)
│── requirements.txt           # Dependencies
│── README.md                  # Documentation

##🤝 Contribution

###Got ideas? Found a bug? Feel free to contribute!

  • Fork the repository
  • Create a new branch
  • Commit changes
  • Push to your fork
  • Create a Pull Request

##🛠️ Future Enhancements ✅ Add logging for response tracking ✅ Support for more status codes ✅ Async support for FastAPI

##📜 License MIT License. Use it however you like! 🚀

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyresponse_paradox-0.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyresponse_paradox-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file pyresponse_paradox-0.1.0.tar.gz.

File metadata

  • Download URL: pyresponse_paradox-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for pyresponse_paradox-0.1.0.tar.gz
Algorithm Hash digest
SHA256 28a4464b4e051900918b05b9f4eed92f275fdec15c5e1971c483b5ed9922d1e4
MD5 867f248a88f3f62a16db0d1bdbfcdbff
BLAKE2b-256 f95b7476a2cc6a853ea76b313e116dbec7f38745cfb6c271a5734935cabdccb3

See more details on using hashes here.

File details

Details for the file pyresponse_paradox-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pyresponse_paradox-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 241009a31f9c59aa8bf74104501820195e456f2552efb79e988e63568df70f51
MD5 91aa10de350dc08e6d8ce7947b991cb6
BLAKE2b-256 feef8f9a28782cf1f3ab2d21317c199c10698ffc020717d41a4d43f7f5f32bb2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page