ca4f01cfc6b7e9ceb2e539862e8f501bed22f513
[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 import os.path
12
13 import tornado.template
14 import tornado.web
15
16 from opnfv_testapi.tornado_swagger import settings
17
18
19 def json_dumps(obj, pretty=False):
20     return json.dumps(obj,
21                       sort_keys=True,
22                       indent=4,
23                       separators=(',', ': ')) if pretty else json.dumps(obj)
24
25
26 class SwaggerUIHandler(tornado.web.RequestHandler):
27     def initialize(self, **kwargs):
28         self.static_path = kwargs.get('static_path')
29         self.base_url = kwargs.get('base_url')
30
31     def get_template_path(self):
32         return self.static_path
33
34     def get(self):
35         discovery_url = os.path.join(
36             self.base_url,
37             self.reverse_url(settings.RESOURCE_LISTING_NAME))
38         self.render('index.html', discovery_url=discovery_url)
39
40
41 class SwaggerResourcesHandler(tornado.web.RequestHandler):
42     def initialize(self, **kwargs):
43         self.api_version = kwargs.get('api_version')
44         self.swagger_version = kwargs.get('swagger_version')
45         self.base_url = kwargs.get('base_url')
46         self.exclude_namespaces = kwargs.get('exclude_namespaces')
47
48     def get(self):
49         self.set_header('content-type', 'application/json')
50         resources = {
51             'apiVersion': self.api_version,
52             'swaggerVersion': self.swagger_version,
53             'basePath': self.base_url,
54             'apis': [{
55                 'path': self.reverse_url(settings.API_DECLARATION_NAME),
56                 'description': 'Restful APIs Specification'
57             }]
58         }
59
60         self.finish(json_dumps(resources, self.get_arguments('pretty')))
61
62
63 class SwaggerApiHandler(tornado.web.RequestHandler):
64     def initialize(self, **kwargs):
65         self.api_version = kwargs.get('api_version')
66         self.swagger_version = kwargs.get('swagger_version')
67         self.base_url = kwargs.get('base_url')
68
69     def get(self):
70         self.set_header('content-type', 'application/json')
71         apis = self.find_api(self.application.handlers)
72         if apis is None:
73             raise tornado.web.HTTPError(404)
74
75         specs = {
76             'apiVersion': self.api_version,
77             'swaggerVersion': self.swagger_version,
78             'basePath': self.base_url,
79             'resourcePath': '/',
80             'produces': ["application/json"],
81             'apis': [self.__get_api_spec__(path, spec, operations)
82                      for path, spec, operations in apis],
83             'models': self.__get_models_spec(settings.models)
84         }
85         self.finish(json_dumps(specs, self.get_arguments('pretty')))
86
87     def __get_models_spec(self, models):
88         models_spec = {}
89         for model in models:
90             models_spec.setdefault(model.id, self.__get_model_spec(model))
91         return models_spec
92
93     @staticmethod
94     def __get_model_spec(model):
95         return {
96             'description': model.summary,
97             'id': model.id,
98             'notes': model.notes,
99             'properties': model.properties,
100             'required': model.required
101         }
102
103     @staticmethod
104     def __get_api_spec__(path, spec, operations):
105         return {
106             'path': path,
107             'description': spec.handler_class.__doc__,
108             'operations': [{
109                 'httpMethod': api.func.__name__.upper(),
110                 'nickname': api.nickname,
111                 'parameters': api.params.values(),
112                 'summary': api.summary,
113                 'notes': api.notes,
114                 'responseClass': api.responseClass,
115                 'responseMessages': api.responseMessages,
116             } for api in operations]
117         }
118
119     @staticmethod
120     def find_api(host_handlers):
121         def get_path(url, args):
122             return url % tuple(['{%s}' % arg for arg in args])
123
124         def get_operations(cls):
125             return [member.rest_api
126                     for (_, member) in inspect.getmembers(cls)
127                     if hasattr(member, 'rest_api')]
128
129         for host, handlers in host_handlers:
130             for spec in handlers:
131                 for (_, mbr) in inspect.getmembers(spec.handler_class):
132                     if inspect.ismethod(mbr) and hasattr(mbr, 'rest_api'):
133                         path = get_path(spec._path, mbr.rest_api.func_args)
134                         operations = get_operations(spec.handler_class)
135                         yield path, spec, operations
136                         break