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))