2 # -*- coding: utf-8 -*-
7 import tornado.template
8 from settings import SWAGGER_VERSION, \
11 from settings import models
16 def json_dumps(obj, pretty=False):
17 return json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': ')) \
18 if pretty else json.dumps(obj)
21 class SwaggerUIHandler(tornado.web.RequestHandler):
22 def initialize(self, static_path, **kwds):
23 self.static_path = static_path
25 def get_template_path(self):
26 return self.static_path
30 urlparse.urljoin(self.request.full_url(),
31 self.reverse_url(SWAGGER_API_LIST))
32 self.render('index.html', discovery_url=discovery_url)
35 class SwaggerResourcesHandler(tornado.web.RequestHandler):
36 def initialize(self, api_version, exclude_namespaces, **kwds):
37 self.api_version = api_version
38 self.exclude_namespaces = exclude_namespaces
41 self.set_header('content-type', 'application/json')
42 u = urlparse.urlparse(self.request.full_url())
44 'apiVersion': self.api_version,
45 'swaggerVersion': SWAGGER_VERSION,
46 'basePath': '%s://%s' % (u.scheme, u.netloc),
47 'produces': ["application/json"],
48 'description': 'Test Api Spec',
50 'path': self.reverse_url(SWAGGER_API_SPEC),
51 'description': 'Test Api Spec'
55 self.finish(json_dumps(resources, self.get_arguments('pretty')))
58 class SwaggerApiHandler(tornado.web.RequestHandler):
59 def initialize(self, api_version, base_url, **kwds):
60 self.api_version = api_version
61 self.base_url = base_url
64 self.set_header('content-type', 'application/json')
65 apis = self.find_api(self.application.handlers)
67 raise tornado.web.HTTPError(404)
69 base_path = urlparse.urljoin(self.request.full_url(),
72 'apiVersion': self.api_version,
73 'swaggerVersion': SWAGGER_VERSION,
74 'basePath': base_path,
75 'apis': [self.__get_api_spec__(path, spec, operations)
76 for path, spec, operations in apis],
77 'models': self.__get_models_spec(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