Responser is a python package to convert normal strings, objects and other data to REST API response convention and in JSON format.
Project description
Responser
Responser is a python package to convert normal strings, objects and other data to REST API response convention and in JSON format.
Usage
Responser
Responser 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" }
JSONResponser
- Import the function with
from responser import JSONResponser
- Definition
JSONResponser(status_code=400, data=None, message=None, strict_mode=false)
- The
status_code
can be HTTP status codes or your own custom status codes. - If the
status_code
is a HTTP status code and thedata
isNone
, a default reason phrase is added. (Ifstrict_mode
isFalse
(default) ) - If the
status_code
is a HTTP status code and themessage
isNone
, a default reason phrase is added. (Even ifstrict_mode
isTrue
(default) ). - If the
status_code
is not a HTTP status code and thedata
isNone
, an empty data is added. - If the
status_code
is not a HTTP status code and themessage
isNone
, an empty message is added. - If the
strict_mode
is set toTrue
, the data given asdata
is encoded. - The
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 response
Output
{ "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." }
JSONResponserDecorator
- Import the decorator with
from responser import JSONResponserDecorator
- Wrap function with decorator
@JSONResponserDecorator
JSONResponserDecorator
is built on top of JSONResponser, so it follows the same convention asJSONResponser
.
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 data
Returned Data
{ "status_code": 200, "data": { "name": "Bharath Kumar Ravichandran", "alma_mater": "NIT Trichy", "languages_known": [ "Python", "PHP", "JS", "C++" ], "cool_guy": "yes", }, "message": "OK" }
Inspired from
Contributors
License
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.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size responser-1.0.0-py2-none-any.whl (4.9 kB) | File type Wheel | Python version py2 | Upload date | Hashes View |
Close
Hashes for responser-1.0.0-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b853f28953b94e9dd2f3833acfa0ac82212dae554d5adf6be670925f63a59722 |
|
MD5 | 4883b0c32e311f11a9f6ead178fbd55d |
|
BLAKE2-256 | e3fc3a191c4ff5d5ff416097c1b882b30c5b50503cb4c06fd40e84765b5688ba |