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_codecan be HTTP status codes or your own custom status codes. - If the
status_codeis a HTTP status code and thedataisNone, a default reason phrase is added. (Ifstrict_modeisFalse(default) ) - If the
status_codeis a HTTP status code and themessageisNone, a default reason phrase is added. (Even ifstrict_modeisTrue(default) ). - If the
status_codeis not a HTTP status code and thedataisNone, an empty data is added. - If the
status_codeis not a HTTP status code and themessageisNone, an empty message is added. - If the
strict_modeis set toTrue, the data given asdatais encoded. - The
status_codedefaults 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 JSONResponserDecoratoris 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
Release files for responser 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| responser-1.0.0-py2-none-any.whl | Python 2 | none | any | Details |
Release files / responser-1.0.0-py2-none-any.whl
| Download URL | responser-1.0.0-py2-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 2 |
|
SHA-256 checksum How to use checksums |
b853f28953b94e9dd2f3833acfa0ac82212dae554d5adf6be670925f63a59722
|
|
BLAKE2b-256 checksum How to use checksums |
e3fc3a191c4ff5d5ff416097c1b882b30c5b50503cb4c06fd40e84765b5688ba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7
|