fuel: Enable concurrency for fuel-deploy job
[releng.git] / utils / test / result_collection_api / tornado_swagger_ui / tornado_swagger / views.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 import urlparse
4 import json
5 import inspect
6 import tornado.web
7 import tornado.template
8 from settings import SWAGGER_VERSION, \
9     SWAGGER_API_LIST, \
10     SWAGGER_API_SPEC
11 from settings import models
12
13 __author__ = 'serena'
14
15
16 def json_dumps(obj, pretty=False):
17     return json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': ')) \
18         if pretty else json.dumps(obj)
19
20
21 class SwaggerUIHandler(tornado.web.RequestHandler):
22     def initialize(self, static_path, **kwds):
23         self.static_path = static_path
24
25     def get_template_path(self):
26         return self.static_path
27
28     def get(self):
29         discovery_url = \
30             urlparse.urljoin(self.request.full_url(),
31                              self.reverse_url(SWAGGER_API_LIST))
32         self.render('index.html', discovery_url=discovery_url)
33
34
35 class SwaggerResourcesHandler(tornado.web.RequestHandler):
36     def initialize(self, api_version, exclude_namespaces, **kwds):
37         self.api_version = api_version
38         self.exclude_namespaces = exclude_namespaces
39
40     def get(self):
41         self.set_header('content-type', 'application/json')
42         u = urlparse.urlparse(self.request.full_url())
43         resources = {
44             'apiVersion': self.api_version,
45             'swaggerVersion': SWAGGER_VERSION,
46             'basePath': '%s://%s' % (u.scheme, u.netloc),
47             'produces': ["application/json"],
48             'description': 'Test Api Spec',
49             'apis': [{
50                 'path': self.reverse_url(SWAGGER_API_SPEC),
51                 'description': 'Test Api Spec'
52             }]
53         }
54
55         self.finish(json_dumps(resources, self.get_arguments('pretty')))
56
57
58 class SwaggerApiHandler(tornado.web.RequestHandler):
59     def initialize(self, api_version, base_url, **kwds):
60         self.api_version = api_version
61         self.base_url = base_url
62
63     def get(self):
64         self.set_header('content-type', 'application/json')
65         apis = self.find_api(self.application.handlers)
66         if apis is None:
67             raise tornado.web.HTTPError(404)
68
69         base_path = urlparse.urljoin(self.request.full_url(),
70                                      self.base_url)[:-1]
71         specs = {
72             'apiVersion': self.api_version,
73             'swaggerVersion': SWAGGER_VERSION,
74             'basePath': base_path,
75             'apis': [self.__get_api_spec__(path, spec, operations)
76                      for path, spec, operations in apis],
77             'models': self.__get_models_spec(models)
78         }
79         self.finish(json_dumps(specs, self.get_arguments('pretty')))
80
81     def __get_models_spec(self, models):
82         models_spec = {}
83         for model in models:
84             models_spec.setdefault(model.id, self.__get_model_spec(model))
85         return models_spec
86
87     @staticmethod
88     def __get_model_spec(model):
89         return {
90             'description': model.summary,
91             'id': model.id,
92             'notes': model.notes,
93             'properties': model.properties,
94             'required': model.required
95         }
96
97     @staticmethod
98     def __get_api_spec__(path, spec, operations):
99         return {
100             'path': path,
101             'description': spec.handler_class.__doc__,
102             'operations': [{
103                 'httpMethod': api.func.__name__.upper(),
104                 'nickname': api.nickname,
105                 'parameters': api.params.values(),
106                 'summary': api.summary,
107                 'notes': api.notes,
108                 'responseClass': api.responseClass,
109                 'responseMessages': api.responseMessages,
110             } for api in operations]
111         }
112
113     @staticmethod
114     def find_api(host_handlers):
115         def get_path(url, args):
116             return url % tuple(['{%s}' % arg for arg in args])
117
118         def get_operations(cls):
119             return [member.rest_api
120                     for (_, member) in inspect.getmembers(cls)
121                     if hasattr(member, 'rest_api')]
122
123         for host, handlers in host_handlers:
124             for spec in handlers:
125                 for (_, mbr) in inspect.getmembers(spec.handler_class):
126                     if inspect.ismethod(mbr) and hasattr(mbr, 'rest_api'):
127                         path = get_path(spec._path, mbr.rest_api.func_args)
128                         operations = get_operations(spec.handler_class)
129                         yield path, spec, operations
130                         break