Skip to content

Instantly share code, notes, and snippets.

@Alkass
Last active June 9, 2016 05:40
Show Gist options
  • Save Alkass/491c138cb744b78b51a14e4c870613c9 to your computer and use it in GitHub Desktop.
Save Alkass/491c138cb744b78b51a14e4c870613c9 to your computer and use it in GitHub Desktop.

Revisions

  1. Alkass renamed this gist Jun 9, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Alkass created this gist Jun 9, 2016.
    58 changes: 58 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    from falcon import API as FalconAPI
    from falcon import HTTP_200
    from json import dumps as encodeJSON, loads as decodeJSON
    from sys import argv

    class IndexPage:
    apiDictList = None
    def __init__(self, apiDictList):
    self.apiDictList = apiDictList
    def on_get(self, req, resp):
    resp.body = encodeJSON({"classes":self.apiDictList})

    class FalconObject(object):
    def getJSON(self, req):
    rawInput = str(req.stream.read())
    if len(rawInput) == 0:
    return {}
    else:
    return decodeJSON(rawInput)

    # Beginning of your FalconObject-inherited class(es)
    class SquareArea(FalconObject):
    def on_get(self, req, resp):
    resp.body, resp.status = encodeJSON({"x":None}), HTTP_200
    def on_post(self, req, resp):
    # Obtain a copy of the data input in the form of a JSON object
    json = self.getJSON(req)

    # Compute the results
    x = json.get("x", 0)
    json["area"] = x * x

    # Embed a stringified resulting JSON object
    resp.body, resp.status = encodeJSON(json), HTTP_200

    class SquarePerimeter(FalconObject):
    def on_get(self, req, resp):
    resp.body, resp.status = encodeJSON({"x":None}), HTTP_200
    def on_post(self, req, resp):
    json = self.getJSON(req)
    json["perimeter"] = abs(json.get("x", 0)) * 2
    resp.body, resp.status = encodeJSON(json), HTTP_200
    # End of your FalconObject-inherited class(es)


    """
    D O N O T M E S S W I T H T H E F O L L O W I N G
    B L O C K O F C O D E
    """
    app = FalconAPI()
    apiClassList = []
    for _class_ in FalconObject.__subclasses__():
    classInstance = _class_()
    className = type(classInstance).__name__
    app.add_route("/%s/" % className, classInstance)
    apiClassList.append(className)
    print("{0} Providing {1} at /{1}/".format(argv[0], className))
    app.add_route("/", IndexPage(apiClassList))