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.2.0.tar.gz (4.8 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.2.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyresponse_paradox-0.2.0.tar.gz
  • Upload date:
  • Size: 4.8 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.2.0.tar.gz
Algorithm Hash digest
SHA256 70fa7e0e041cbaed2d984048f66364f5ecdf0484e493eb80ab615b24bdd6dee2
MD5 677135f55d2a94773dffeee772559acf
BLAKE2b-256 5fe14b9919fea32fb0337828d1eb9d613f400b497de22a1b44d03ab72ca1116b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyresponse_paradox-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5ebf96bdb67ee0a522c0fd288c91bf2f45c2f79fa93ebfd9922f43741faace15
MD5 6a1cd1d71bc2439ecab5bb3f0c3fa1dd
BLAKE2b-256 0aca75c63eee1d23b3cd64fcc5707b24c83c79b972770ba6c015cba5d5566d8d

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