2 # -*- coding: utf-8 -*-
7 import tornado.template
8 from tornado_swagger.settings import SWAGGER_VERSION, URL_SWAGGER_API_LIST, URL_SWAGGER_API_SPEC, models
13 def json_dumps(obj, pretty=False):
14 return json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': ')) if pretty else json.dumps(obj)
17 class SwaggerUIHandler(tornado.web.RequestHandler):
18 def initialize(self, static_path, **kwds):
19 self.static_path = static_path
21 def get_template_path(self):
22 return self.static_path
25 discovery_url = urlparse.urljoin(self.request.full_url(), self.reverse_url(URL_SWAGGER_API_LIST))
26 self.render('index.html', discovery_url=discovery_url)
29 class SwaggerResourcesHandler(tornado.web.RequestHandler):
30 def initialize(self, api_version, exclude_namespaces, **kwds):
31 self.api_version = api_version
32 self.exclude_namespaces = exclude_namespaces
35 self.set_header('content-type', 'application/json')
36 u = urlparse.urlparse(self.request.full_url())
38 'apiVersion': self.api_version,
39 'swaggerVersion': SWAGGER_VERSION,
40 'basePath': '%s://%s' % (u.scheme, u.netloc),
41 'produces': ["application/json"],
42 'description': 'Test Api Spec',
44 'path': self.reverse_url(URL_SWAGGER_API_SPEC),
45 'description': 'Test Api Spec'
49 self.finish(json_dumps(resources, self.get_arguments('pretty')))
52 class SwaggerApiHandler(tornado.web.RequestHandler):
53 def initialize(self, api_version, base_url, **kwds):
54 self.api_version = api_version
55 self.base_url = base_url
58 self.set_header('content-type', 'application/json')
59 apis = self.find_api(self.application.handlers)
61 raise tornado.web.HTTPError(404)
64 'apiVersion': self.api_version,
65 'swaggerVersion': SWAGGER_VERSION,
66 'basePath': urlparse.urljoin(self.request.full_url(), self.base_url)[:-1],
67 'apis': [self.__get_api_spec__(path, spec, operations) for path, spec, operations in apis],
68 'models': self.__get_models_spec(models)
70 self.finish(json_dumps(specs, self.get_arguments('pretty')))
72 def __get_models_spec(self, models):
75 models_spec.setdefault(model.id, self.__get_model_spec(model))
79 def __get_model_spec(model):
81 'description': model.summary,
84 'properties': model.properties,
85 'required': model.required
89 def __get_api_spec__(path, spec, operations):
92 'description': spec.handler_class.__doc__,
94 'httpMethod': api.func.__name__.upper(),
95 'nickname': api.nickname,
96 'parameters': api.params.values(),
97 'summary': api.summary,
99 'responseClass': api.responseClass,
100 'responseMessages': api.responseMessages,
101 } for api in operations]
105 def find_api(host_handlers):
106 for host, handlers in host_handlers:
107 for spec in handlers:
108 for (name, member) in inspect.getmembers(spec.handler_class):
109 if inspect.ismethod(member) and hasattr(member, 'rest_api'):
110 spec_path = spec._path % tuple(['{%s}' % arg for arg in member.rest_api.func_args])
111 operations = [member.rest_api for (name, member) in inspect.getmembers(spec.handler_class)
112 if hasattr(member, 'rest_api')]
113 yield spec_path, spec, operations