Package: pyresponse
PyResponsePyResponse is a Python package that provides utility functions to create success and error responses in various Python frameworks.PurposeThe 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()andcreate_error_response(), which can be used to generate standardized response structures.InstallationTo install PyResponse, you can use pip:pipinstallpyresponseUsagePyResponse can be used with different web frameworks, including Django, FastAPI, and Flask. Here's how you can use PyResponse in each framework:# Example usagestatus_code = HTTPStatus.OK # or status_code = 200DjangoInstall PyResponse using pip as shown in the installation section.Import the necessary functions from PyResponse in your Django views or API handlers.Use thecreate_success_response()andcreate_error_response()functions to generate the desired responses.frompyresponse.responseimportcreate_success_response,create_error_responsedefmy_view(request):# Process the request and generate the necessary datadata=...# Generate a success responseresponse=create_success_response(data=data,message='Success',status_code=200)returnresponsedefmy_api_view(request):try:# Process the request and generate the necessary datadata=...# Generate a success response with a custom serializerserializer=MySerializer()response=create_success_response(data=data,message='Success',serializer=serializer,status_code=200)returnresponseexceptExceptionase:# Generate an error responseresponse=create_error_response(message=str(e),status_code=500)returnresponseFastAPIInstall PyResponse using pip as shown in the installation section.Import the necessary functions from PyResponse in your FastAPI routes.Use thecreate_success_response()andcreate_error_response()functions to generate the desired responses.fromfastapiimportFastAPIfrompyresponse.responseimportcreate_success_response,create_error_responseapp=FastAPI()@app.get("/success")defsuccess():data={'key':'value'}message='Success message'status_code=200response=create_success_response(data=data,message=message,status_code=status_code)returnresponse@app.get("/error")deferror():message='Error message'status_code=400response=create_error_response(message=message,status_code=status_code)returnresponseFlaskInstall PyResponse.response using pip as shown in the installation section.Import the necessary functions from PyResponse in your Flask routes.Use thecreate_success_response()andcreate_error_response()functions to generate the desired responses.fromflaskimportFlask,jsonifyfrompyresponseimportcreate_success_response,create_error_responseapp=Flask(__name__)@app.route("/success")defsuccess():data={'key':'value'}message='Success message'status_code=200response=create_success_response(data=data,message=message,status_code=status_code)returnjsonify(response)@app.route("/error")deferror():message='Error message'status_code=400response=create_error_response(message=message,status_code=status_code)returnjsonify(response)Serializer SupportPyResponse supports different serializers like Pydantic and marshmallow. Thecreate_success_response()andcreate_error_response()functions accept an additionalserializerparameter, 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'sdict()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:PydanticfrompydanticimportBaseModelfrompyresponseimportcreate_success_responseclassMyModel(BaseModel):success:boolmessage:strdata:dict# Example usageserializer=MyModelresponse=create_success_response(data={'foo':'bar'},message='Success',serializer=serializer)MarshmallowfrommarshmallowimportSchema,fieldsfrompyresponseimportcreate_success_responseclassMySchema(Schema):success=fields.Boolean()message=fields.String()data=fields.Dict()# Example usageserializer=MySchema()response=create_success_response(data={'foo':'bar'},message='Success',serializer=serializer)LicenseThis project is licensed under the MIT License - see theLICENSEfile for details.