correct the import impl of TestAPI
[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, sort_keys=True, indent=4, separators=(',', ': ')) \
21         if pretty else json.dumps(obj)
22
23
24 class SwaggerUIHandler(tornado.web.RequestHandler):
25     def initialize(self, static_path, **kwds):
26         self.static_path = static_path
27
28     def get_template_path(self):
29         return self.static_path
30
31     def get(self):
32         discovery_url = os.path.join(
33             settings.basePath(),
34             self.reverse_url(settings.SWAGGER_RESOURCE_LISTING))
35         self.render('index.html', discovery_url=discovery_url)
36
37
38 class SwaggerResourcesHandler(tornado.web.RequestHandler):
39     def initialize(self, api_version, exclude_namespaces, **kwds):
40         self.api_version = api_version
41         self.exclude_namespaces = exclude_namespaces
42
43     def get(self):
44         self.set_header('content-type', 'application/json')
45         resources = {
46             'apiVersion': self.api_version,
47             'swaggerVersion': settings.SWAGGER_VERSION,
48             'basePath': settings.basePath(),
49             'produces': ["application/json"],
50             'description': 'Test Api Spec',
51             'apis': [{
52                 'path': self.reverse_url(settings.SWAGGER_API_DECLARATION),
53                 'description': 'Test Api Spec'
54             }]
55         }
56
57         self.finish(json_dumps(resources, self.get_arguments('pretty')))
58
59
60 class SwaggerApiHandler(tornado.web.RequestHandler):
61     def initialize(self, api_version, base_url, **kwds):
62         self.api_version = api_version
63         self.base_url = base_url
64
65     def get(self):
66         self.set_header('content-type', 'application/json')
67         apis = self.find_api(self.application.handlers)
68         if apis is None:
69             raise tornado.web.HTTPError(404)
70
71         specs = {
72             'apiVersion': self.api_version,
73             'swaggerVersion': settings.SWAGGER_VERSION,
74             'basePath': settings.basePath(),
75             'apis': [self.__get_api_spec__(path, spec, operations)
76                      for path, spec, operations in apis],
77             'models': self.__get_models_spec(settings.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