responser
1.0.0
Responser is a python package to convert normal strings, objects and other data to REST API response convention and in JSON format.
ResponserResponser is a python package to convert normal strings, objects and other data to REST API response convention and in JSON format.
This function follows the following JSON structure.
{
"status_code": 200,
"data": {
"name": "Bharath Kumar Ravichandran",
"alma_mater": "NIT Trichy",
"languages_known": [
"Python",
"PHP",
"JS",
"C++"
],
"cool_guy": "yes",
},
"message": "OK"
}JSONResponserfrom responser import JSONResponserJSONResponser(status_code=400, data=None, message=None, strict_mode=false)status_code can be HTTP status codes or your own custom status codes.status_code is a HTTP status code and the data is None, a default
reason phrase is added. (If strict_mode is False (default) )status_code is a HTTP status code and the message is None, a default
reason phrase is added. (Even if strict_mode is True (default) ).status_code is not a HTTP status code and the data is None, an empty data is added.status_code is not a HTTP status code and the message is None, an empty message is added.strict_mode is set to True, the data given as data is encoded.status_code defaults to 400.Sample Code
from responser import JSONResponser
status_code = 200
data = {
"name": "Bharath Kumar Ravichandran",
"alma_mater": "NIT Trichy",
"languages_known": [
"Python",
"PHP",
"JS",
"C++"
],
"cool_guy": "yes",
}
message = "User details returned."
response = JSONResponser(status_code, data, message)
print responseOutput
{
"status_code": 200,
"data": {
"name": "Bharath Kumar Ravichandran",
"alma_mater": "NIT Trichy",
"languages_known": [
"Python",
"PHP",
"JS",
"C++"
],
"cool_guy": "yes",
},
"message": "User details returned."
}JSONResponserDecoratorfrom responser import JSONResponserDecorator@JSONResponserDecoratorJSONResponserDecorator is built on top of JSONResponser, so it follows the same convention as JSONResponser.Sample Code
from responser import JSONResponserDecorator
@JSONResponserDecorator
def sample_function():
data = {
"name": "Bharath Kumar Ravichandran",
"alma_mater": "NIT Trichy",
"languages_known": [
"Python",
"PHP",
"JS",
"C++"
],
"cool_guy": "yes",
}
return dataReturned Data
{
"status_code": 200,
"data": {
"name": "Bharath Kumar Ravichandran",
"alma_mater": "NIT Trichy",
"languages_known": [
"Python",
"PHP",
"JS",
"C++"
],
"cool_guy": "yes",
},
"message": "OK"
}GokulSrinivas/Sangria
Bharath Kumar Ravichandran
MIT