Git Product home page Git Product logo

responser's Introduction

Responser

PRs Welcome made-with-python License

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 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": "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 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 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

GokulSrinivas/Sangria

Contributors

Bharath Kumar Ravichandran

License

MIT

Built with love

responser's People

Contributors

bharathkumarravichandran avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.