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