Skip to main content

Responser

PRs Welcome made-with-python License

Responser is a python package to convert normal strings, objects and other data to REST API response format and to JSON format.

Usage

Responser


Responser is a python package to convert normal strings, objects and other data to REST API response format and to 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": True,
  },
  "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 the data is None, a default reason phrase is added. (If strict_mode is False (default) )
  • If the 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) ).
  • If the status_code is not a HTTP status code and the data is None, an empty data is added.
  • If the status_code is not a HTTP status code and the message is None, an empty message is added.
  • If the strict_mode is set to True, the data given as data 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": True,
}
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": True,
  },
  "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 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": True,
    }

    return data

Returned Data

{
  "status_code": 200,
  "data": {
    "name": "Bharath Kumar Ravichandran",
    "alma-mater": "NIT Trichy",
    "languages_known": [
        "Python",
        "PHP",
        "JS",
        "C++"
    ],
    "cool_guy": True,
  },
  "message": "OK"
}

Inspired from

GokulSrinivas/Sangria

Contributors

Bharath Kumar Ravichandran

License

MIT

Built with love

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

responser-0.1-py2-none-any.whl (5.0 kB view details)

Uploaded Python 2

File details

Details for the file responser-0.1-py2-none-any.whl.

File metadata

  • Download URL: responser-0.1-py2-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for responser-0.1-py2-none-any.whl
Algorithm Hash digest
SHA256 f7af82559dcb9c88bbed2719cfa96eb007cf5cc21236b84ef0122d0b2b4f9874
MD5 47562e8122ca678f76bd6272c515aba0
BLAKE2b-256 1fb6bb20e46a65baf257b946724aa0618322044561516d3ecd5af4e887db57b2

See more details on using hashes here.

Release history Release notifications | RSS feed

1.0.0

1 file

This release

0.1 This release

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page