4 from tornado.web import RequestHandler, HTTPError
5 from tornado_swagger import swagger
7 DEFAULT_REPRESENTATION = "application/json"
16 class PropertySubclass:
17 def __init__(self, sub_property=None):
18 self.sub_property = sub_property
25 This is an example of a model class that has parameters in its constructor
26 and the fields in the swagger spec are derived from the parameters to __init__.
28 In this case we would have property1, name as required parameters and property3 as optional parameter.
29 @property property3: Item description
30 @ptype property3: L{PropertySubclass}
31 @ptype property4: C{list} of L{PropertySubclass}
33 def __init__(self, property1, property2=None, property3=None, property4=None):
34 self.property1 = property1
35 self.property2 = property2
36 self.property3 = property3
37 self.property4 = property4
42 class GenericApiHandler(RequestHandler):
44 The purpose of this class is to take benefit of inheritance and prepare
45 a set of common functions for
50 """ Prepares the database for the entire class """
54 if not (self.request.method == "GET" or self.request.method == "DELETE"):
55 if self.request.headers.get("Content-Type") is not None:
56 if self.request.headers["Content-Type"].startswith(DEFAULT_REPRESENTATION):
58 self.json_args = json.loads(self.request.body)
59 except (ValueError, KeyError, TypeError) as error:
60 raise HTTPError(HTTP_BAD_REQUEST,
61 "Bad Json format [{}]".
66 def finish_request(self, json_object):
67 self.write(json.dumps(json_object))
68 self.set_header("Content-Type", DEFAULT_REPRESENTATION)
72 class ItemNoParamHandler(GenericApiHandler):
73 @swagger.operation(nickname='create')
76 @param body: create a item.
78 @return 200: item is created.
79 @raise 400: invalid input
81 property1 = self.json_args.get('property1')
82 items[property1] = self.json_args
83 self.finish_request(items[property1])
85 @swagger.operation(nickname='list')
90 self.finish_request(items)
94 I'm not visible in the swagger docs
96 self.finish_request("I'm invisible in the swagger docs")
99 class ItemHandler(GenericApiHandler):
100 @swagger.operation(nickname='get')
104 @description: get information of a item
108 This will be added to the Implementation Notes.It lets you put very long text in your api.
110 self.finish_request(items[arg])
112 @swagger.operation(nickname='delete')
113 def delete(self, arg):
115 @description: delete a item
117 delete a item in items
119 This will be added to the Implementation Notes.It lets you put very long text in your api.
122 self.finish_request("success")
125 class ItemOptionParamHandler(GenericApiHandler):
126 @swagger.operation(nickname='create')
127 def post(self, arg1, arg2=''):
129 @return 200: case is created
131 print("ProjectHandler.post: %s -- %s -- %s" % (arg1, arg2, self.request.full_url()))
132 fs = open("/home/swagger/tornado-rest-swagger/%s/%s" % (arg1, arg2), "wb")
133 fs.write(self.request.body)
134 self.write("success")
138 return swagger.Application([
139 (r"/items", ItemNoParamHandler),
140 (r"/items/([^/]+)", ItemHandler),
141 (r"/items/([^/]+)/cases/([^/]+)", ItemOptionParamHandler),
145 if __name__ == "__main__":
148 tornado.ioloop.IOLoop.current().start()