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 ##############################################################################
13 import tornado.template
16 from opnfv_testapi.tornado_swagger import settings
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)
24 class SwaggerUIHandler(tornado.web.RequestHandler):
25 def initialize(self, static_path, **kwds):
26 self.static_path = static_path
28 def get_template_path(self):
29 return self.static_path
32 discovery_url = os.path.join(
34 self.reverse_url(settings.SWAGGER_RESOURCE_LISTING))
35 self.render('index.html', discovery_url=discovery_url)
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
44 self.set_header('content-type', 'application/json')
46 'apiVersion': self.api_version,
47 'swaggerVersion': settings.SWAGGER_VERSION,
48 'basePath': settings.basePath(),
49 'produces': ["application/json"],
50 'description': 'Test Api Spec',
52 'path': self.reverse_url(settings.SWAGGER_API_DECLARATION),
53 'description': 'Test Api Spec'
57 self.finish(json_dumps(resources, self.get_arguments('pretty')))
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
66 self.set_header('content-type', 'application/json')
67 apis = self.find_api(self.application.handlers)
69 raise tornado.web.HTTPError(404)
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)
79 self.finish(json_dumps(specs, self.get_arguments('pretty')))
81 def __get_models_spec(self, models):
84 models_spec.setdefault(model.id, self.__get_model_spec(model))
88 def __get_model_spec(model):
90 'description': model.summary,
93 'properties': model.properties,
94 'required': model.required
98 def __get_api_spec__(path, spec, operations):
101 'description': spec.handler_class.__doc__,
103 'httpMethod': api.func.__name__.upper(),
104 'nickname': api.nickname,
105 'parameters': api.params.values(),
106 'summary': api.summary,
108 'responseClass': api.responseClass,
109 'responseMessages': api.responseMessages,
110 } for api in operations]
114 def find_api(host_handlers):
115 def get_path(url, args):
116 return url % tuple(['{%s}' % arg for arg in args])
118 def get_operations(cls):
119 return [member.rest_api
120 for (_, member) in inspect.getmembers(cls)
121 if hasattr(member, 'rest_api')]
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