2168c5e915853eb9a8f7866f3b5a99a69aadabf2
[releng.git] / utils / test / testapi / 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 inspect
10 import json
11
12 import tornado.template
13 import tornado.web
14
15 from settings import SWAGGER_VERSION
16 from settings import SWAGGER_RESOURCE_LISTING, SWAGGER_API_DECLARATION
17 from settings import models, basePath
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 = basePath() + self.reverse_url(SWAGGER_RESOURCE_LISTING)
34         self.render('index.html', discovery_url=discovery_url)
35
36
37 class SwaggerResourcesHandler(tornado.web.RequestHandler):
38     def initialize(self, api_version, exclude_namespaces, **kwds):
39         self.api_version = api_version
40         self.exclude_namespaces = exclude_namespaces
41
42     def get(self):
43         self.set_header('content-type', 'application/json')
44         resources = {
45             'apiVersion': self.api_version,
46             'swaggerVersion': SWAGGER_VERSION,
47             'basePath': basePath(),
48             'produces': ["application/json"],
49             'description': 'Test Api Spec',
50             'apis': [{
51                 'path': self.reverse_url(SWAGGER_API_DECLARATION),
52                 'description': 'Test Api Spec'
53             }]
54         }
55
56         self.finish(json_dumps(resources, self.get_arguments('pretty')))
57
58
59 class SwaggerApiHandler(tornado.web.RequestHandler):
60     def initialize(self, api_version, base_url, **kwds):
61         self.api_version = api_version
62         self.base_url = base_url
63
64     def get(self):
65         self.set_header('content-type', 'application/json')
66         apis = self.find_api(self.application.handlers)
67         if apis is None:
68             raise tornado.web.HTTPError(404)
69
70         specs = {
71             'apiVersion': self.api_version,
72             'swaggerVersion': SWAGGER_VERSION,
73             'basePath': basePath(),
74             'apis': [self.__get_api_spec__(path, spec, operations)
75                      for path, spec, operations in apis],
76             'models': self.__get_models_spec(models)
77         }
78         self.finish(json_dumps(specs, self.get_arguments('pretty')))
79
80     def __get_models_spec(self, models):
81         models_spec = {}
82         for model in models:
83             models_spec.setdefault(model.id, self.__get_model_spec(model))
84         return models_spec
85
86     @staticmethod
87     def __get_model_spec(model):
88         return {
89             'description': model.summary,
90             'id': model.id,
91             'notes': model.notes,
92             'properties': model.properties,
93             'required': model.required
94         }
95
96     @staticmethod
97     def __get_api_spec__(path, spec, operations):
98         return {
99             'path': path,
100             'description': spec.handler_class.__doc__,
101             'operations': [{
102                 'httpMethod': api.func.__name__.upper(),
103                 'nickname': api.nickname,
104                 'parameters': api.params.values(),
105                 'summary': api.summary,
106                 'notes': api.notes,
107                 'responseClass': api.responseClass,
108                 'responseMessages': api.responseMessages,
109             } for api in operations]
110         }
111
112     @staticmethod
113     def find_api(host_handlers):
114         def get_path(url, args):
115             return url % tuple(['{%s}' % arg for arg in args])
116
117         def get_operations(cls):
118             return [member.rest_api
119                     for (_, member) in inspect.getmembers(cls)
120                     if hasattr(member, 'rest_api')]
121
122         for host, handlers in host_handlers:
123             for spec in handlers:
124                 for (_, mbr) in inspect.getmembers(spec.handler_class):
125                     if inspect.ismethod(mbr) and hasattr(mbr, 'rest_api'):
126                         path = get_path(spec._path, mbr.rest_api.func_args)
127                         operations = get_operations(spec.handler_class)
128                         yield path, spec, operations
129                         break