Package for creating success and error responses in Python Projects
Project description
PyResponse
PyResponse is a Python package that provides utility functions to create success and error responses in various Python frameworks.
Purpose
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.
Installation
To install PyResponse, you can use pip:
pip install pyresponse
Usage
PyResponse can be used with different web frameworks, including Django, FastAPI, and Flask. Here's how you can use PyResponse in each framework:
# Example usage
status_code = HTTPStatus.OK # or status_code = 200
Django
- 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
FastAPI
- 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
Flask
- 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)
Serializer Support
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:
Pydantic
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)
Marshmallow
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)
License
This project is licensed under the MIT License - see the LICENSE file for details.
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
File details
Details for the file pyresponse-0.0.1.tar.gz
.
File metadata
- Download URL: pyresponse-0.0.1.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8920dd38222d6937a7c971a7a3e55f8bdbb5b435282c32620478ad1642317663 |
|
MD5 | d9140d946bf0504b7f2d05b5b47f9b66 |
|
BLAKE2b-256 | 86c8b4c2129b17095b8c8f92a4965243af71eb8e09d945d17853b21369bcc9e5 |
File details
Details for the file pyresponse-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: pyresponse-0.0.1-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9752acd5eac5fa26a4cebd8157eaa7f4b6e5b761a0455f28b83bb493d35fa70 |
|
MD5 | 1086b3f1c1030af1211b9c59af37ce1f |
|
BLAKE2b-256 | 3b5de1878f73a34c4afd3d1cfee18639c41410ae4a2a92c48ad47bec95c427f7 |