PyResponse is a Python package that provides utility functions to create success and error responses in various Python frameworks.
The purpose of PyResponse is to simplify the process of generating success and error responses in web applications. It provides two main functions, create_success_response()
and create_error_response()
, which can be used to generate standardized response structures.
To install PyResponse, you can use pip:
pip install pyresponse
PyResponse can be used with different web frameworks, including Django, FastAPI, and Flask. Here's how you can use PyResponse in each framework:
status_code = HTTPStatus.OK # or status_code = 200
from pyresponse.response import create_success_response, create_error_response
def main():
# Example usage of PyResponse
name = input("Enter your name: ")
age = input("Enter your age: ")
try:
age = int(age)
if age >= 18:
message = "Success"
data = {'name': name, 'age': age}
success_response = create_success_response(
data=data, message=message, status_code=200)
print(success_response.data) # Access the response data
# Access the response status code
print(success_response.status_code)
else:
message = "Error: Age must be 18 or older."
error_response = create_error_response(
message=message, status_code=400)
print(error_response.data) # Access the response data
# Access the response status code
print(error_response.status_code)
except ValueError:
message = "Error: Invalid age entered."
error_response = create_error_response(
message=message, status_code=400)
print(error_response.data) # Access the response data
print(error_response.status_code) # Access the response status code
if __name__ == '__main__':
main()
- Install PyResponse using pip as shown in the installation section.
- Import the necessary functions from PyResponse in your Django views or API handlers.
- Use the
create_success_response()
andcreate_error_response()
functions to generate the desired responses.
from pyresponse.response import create_success_response, create_error_response
def my_view(request):
# Process the request and generate the necessary data
data = ...
# Generate a success response
response = create_success_response(data=data, message='Success', status_code=200)
return response
def my_api_view(request):
try:
# Process the request and generate the necessary data
data = ...
# Generate a success response with a custom serializer
serializer = MySerializer()
response = create_success_response(data=data, message='Success', serializer=serializer, status_code=200)
return response
except Exception as e:
# Generate an error response
response = create_error_response(message=str(e), status_code=500)
return response
- Install PyResponse using pip as shown in the installation section.
- Import the necessary functions from PyResponse in your FastAPI routes.
- Use the
create_success_response()
andcreate_error_response()
functions to generate the desired responses.
from fastapi import FastAPI
from pyresponse.response import create_success_response, create_error_response
app = FastAPI()
@app.get("/success")
def success():
data = {'key': 'value'}
message = 'Success message'
status_code = 200
response = create_success_response(data=data, message=message, status_code=status_code)
return response
@app.get("/error")
def error():
message = 'Error message'
status_code = 400
response = create_error_response(message=message, status_code=status_code)
return response
- Install PyResponse.response using pip as shown in the installation section.
- Import the necessary functions from PyResponse in your Flask routes.
- Use the
create_success_response()
andcreate_error_response()
functions to generate the desired responses.
from flask import Flask, jsonify
from pyresponse import create_success_response, create_error_response
app = Flask(__name__)
@app.route("/success")
def success():
data = {'key': 'value'}
message = 'Success message'
status_code = 200
response = create_success_response(data=data, message=message, status_code=status_code)
return jsonify(response)
@app.route("/error")
def error():
message = 'Error message'
status_code = 400
response = create_error_response(message=message, status_code=status_code)
return jsonify(response)
PyResponse supports different serializers like Pydantic and marshmallow. The create_success_response()
and create_error_response()
functions accept an additional serializer
parameter, which allows users to pass their chosen serializer for serializing the response data.
If a serializer is provided, the response data is serialized using that serializer's dict()
method. Otherwise, the response data is returned as is.
This modification enables users to use different serializers based on their requirements. They can pass their serializer instance to the response functions when needed.
Here are examples using Pydantic and marshmallow serializers:
from pydantic import BaseModel
from pyresponse import create_success_response
class MyModel(BaseModel):
success: bool
message: str
data: dict
# Example usage
serializer = MyModel
response = create_success_response(data={'foo': 'bar'}, message='Success', serializer=serializer)
from marshmallow import Schema, fields
from pyresponse import create_success_response
class MySchema(Schema):
success = fields.Boolean()
message = fields.String()
data = fields.Dict()
# Example usage
serializer = MySchema()
response = create_success_response(data={'foo': 'bar'}, message='Success', serializer=serializer)
This project is licensed under the MIT License - see the LICENSE file for details.