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

To install PyResponse-paradox, simply run:

pip install pyresponse-paradox

🚀 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"

##📁 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.1.tar.gz (4.6 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.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyresponse_paradox-0.2.1.tar.gz
  • Upload date:
  • Size: 4.6 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.1.tar.gz
Algorithm Hash digest
SHA256 a25d30fe1a116838d29a5567485b05066677d978d5e4ce2b7e863392aed5dca2
MD5 260384f45dafbf72fb00c79b5d7788fb
BLAKE2b-256 d819bebf43bfa331ebb5b9fff983d10cb76639bd4c526b87d870854e7d06d8ff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pyresponse_paradox-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7bde0c481fe47c71b7a495009fec1fa91249ec1ec1e79fa4361fe48ba3c30720
MD5 6b3077ca79ec4f54404552eeb409b4d5
BLAKE2b-256 9ec39c9dbcdbc98c161bd8e91659d416f4fb547d60f4cd87983f112dc3c674dc

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