3 ## What is tornado-swagger?
4 tornado is a wrapper for tornado which enables swagger-ui support.
6 In essense, you just need to wrap the Api instance and add a few python decorators to get full swagger support.
12 python setup.py install
14 (This installs tornado and epydoc as well)
17 And in your program, where you'd usually just use tornado, add just a little bit of sauce and get a swagger spec out.
21 from tornado.web import RequestHandler, HTTPError
22 from tornado_swagger import swagger
26 # You may decorate your operation with @swagger.operation and use docs to inform information
27 class ItemNoParamHandler(GenericApiHandler):
28 @swagger.operation(nickname='create')
31 @param body: create test results for a item.
33 @return 200: item is created.
34 @raise 400: invalid input
37 # Operations not decorated with @swagger.operation do not get added to the swagger docs
39 class ItemNoParamHandler(GenericApiHandler):
42 I'm not visible in the swagger docs
47 # Then you use swagger.Application instead of tornado.web.Application
48 # and do other operations as usual
51 return swagger.Application([
52 (r"/items", ItemNoParamHandler),
53 (r"/items/([^/]+)", ItemHandler),
54 (r"/items/([^/]+)/cases/([^/]+)", ItemOptionParamHandler),
57 # You define models like this:
62 This is an example of a model class that has parameters in its constructor
63 and the fields in the swagger spec are derived from the parameters to __init__.
65 In this case we would have property1, property2 as required parameters and property3 as optional parameter.
66 @property property3: Item decription
67 @ptype property3: L{PropertySubclass}
69 def __init__(self, property1, property2=None):
70 self.property1 = property1
71 self.property2 = property2
76 "description": "A description...",
93 # If you declare an __init__ method with meaningful arguments
94 # then those args could be used to deduce the swagger model fields.
97 # if you declare an @property in docs, this property property2 will also be used to deduce the swagger model fields
100 @property property3: Item description
102 def __init__(self, property1, property2):
103 self.property1 = property1
104 self.property2 = property2
109 "description": "A description...",
128 # if you declare an argument with @ptype, the type of this argument will be specified rather than the default 'string'
131 @ptype property3: L{PropertySubclass}
133 def __init__(self, property1, property2, property3=None):
134 self.property1 = property1
135 self.property2 = property2
136 self.property3 = property3
141 "description": "A description...",
154 "type": "PropertySubclass"
161 # if you want to declare an list property, you can do it like this:
164 @ptype property3: L{PropertySubclass}
165 @ptype property4: C{list} of L{PropertySubclass}
167 def __init__(self, property1, property2, property3, property4=None):
168 self.property1 = property1
169 self.property2 = property2
170 self.property3 = property3
171 self.property4 = property4
176 "description": "A description...",
189 "type": "PropertySubclass"
195 "type": "PropertySubclass"},
204 class ItemQueryHandler(GenericApiHandler):
205 @swagger.operation(nickname='query')
209 @type property1: L{string}
211 @required property1: False
214 @type property2: L{string}
216 @required property2: True
219 @notes: GET /item?property1=1&property2=1
230 "dataType": "string",
231 "paramType": "query",
236 "dataType": "string",
237 "paramType": "query",
242 "responseClass": "Item",
244 "responseMessages": [],
257 # Running and testing
259 Now run your tornado app
268 curl http://localhost:711/swagger/spec
273 http://localhost:711/swagger/spec.html
276 # Passing more metadata to swagger
277 customized arguments used in creating the 'swagger.docs' object will be supported later