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 pod.
33 @return 200: pod 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"/pods", ItemNoParamHandler),
53 (r"/pods/([^/]+)", ItemHandler),
54 (r"/projects/([^/]+)/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"
162 # Running and testing
164 Now run your tornado app
173 curl http://localhost:711/swagger/spec
178 http://localhost:711/swagger/spec.html
181 # Passing more metadata to swagger
182 customized arguments used in creating the 'swagger.docs' object will be supported later