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}
32 def __init__(self, property1, property2=None, property3=None):
33 self.property1 = property1
34 self.property2 = property2
35 self.property3 = property3
40 class GenericApiHandler(RequestHandler):
42 The purpose of this class is to take benefit of inheritance and prepare
43 a set of common functions for
48 """ Prepares the database for the entire class """
52 if not (self.request.method == "GET" or self.request.method == "DELETE"):
53 if self.request.headers.get("Content-Type") is not None:
54 if self.request.headers["Content-Type"].startswith(DEFAULT_REPRESENTATION):
56 self.json_args = json.loads(self.request.body)
57 except (ValueError, KeyError, TypeError) as error:
58 raise HTTPError(HTTP_BAD_REQUEST,
59 "Bad Json format [{}]".
64 def finish_request(self, json_object):
65 self.write(json.dumps(json_object))
66 self.set_header("Content-Type", DEFAULT_REPRESENTATION)
70 class ItemNoParamHandler(GenericApiHandler):
71 @swagger.operation(nickname='create')
74 @param body: create test results for a pod.
76 @return 200: pod is created.
77 @raise 400: invalid input
79 property1 = self.json_args.get('property1')
80 items[property1] = self.json_args
81 self.finish_request(items[property1])
83 @swagger.operation(nickname='list')
88 self.finish_request(items)
92 I'm not visible in the swagger docs
94 self.finish_request("I'm invisible in the swagger docs")
97 class ItemHandler(GenericApiHandler):
98 @swagger.operation(nickname='get')
102 @description: get pod's test results
104 get a pod test results,
106 This will be added to the Implementation Notes.It lets you put very long text in your api.
108 self.finish_request(items[arg])
110 @swagger.operation(nickname='delete')
111 def delete(self, arg):
113 @description: delete pod by pod_id
115 delete test results of a pod
117 This will be added to the Implementation Notes.It lets you put very long text in your api.
120 self.finish_request("success")
123 class ItemOptionParamHandler(GenericApiHandler):
124 @swagger.operation(nickname='create')
125 def post(self, arg1, arg2=''):
127 @return 200: case is created
129 print("ProjectHandler.post: %s -- %s -- %s" % (arg1, arg2, self.request.full_url()))
130 fs = open("/home/swagger/tornado-rest-swagger/%s/%s" % (arg1, arg2), "wb")
131 fs.write(self.request.body)
132 self.write("success")
136 return swagger.Application([
137 (r"/items", ItemNoParamHandler),
138 (r"/items/([^/]+)", ItemHandler),
139 (r"/items/([^/]+)/cases/([^/]+)", ItemOptionParamHandler),
143 if __name__ == "__main__":
146 tornado.ioloop.IOLoop.current().start()