-
Notifications
You must be signed in to change notification settings - Fork 31
/
falcon.py
71 lines (56 loc) · 2.37 KB
/
falcon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import json
from packaging.version import Version
class FalconInterface(object):
def __init__(self, use_async=False):
self.use_async = use_async
def handler(self, doc):
class Handler(object):
async def on_get_async(self, req, resp):
self.on_get(req, resp)
class SwaggerDocHandler(Handler):
def on_get(self, req, resp):
resp.content_type = 'text/html'
resp.body = doc.doc_html
class SwaggerEditorHandler(Handler):
def on_get(self, req, resp):
resp.content_type = 'text/html'
resp.body = doc.editor_html
class SwaggerConfigHandler(Handler):
def on_get(self, req, resp):
resp.content_type = 'application/json'
resp.body = json.dumps(doc.get_config(f'{req.host}:{req.port}'))
suffix = 'async' if self.use_async else None
doc.app.add_route(doc.root_uri_absolute(slashes=True),
SwaggerDocHandler(), suffix=suffix)
doc.app.add_route(doc.root_uri_absolute(slashes=False),
SwaggerDocHandler(), suffix=suffix)
if doc.editor:
doc.app.add_route(doc.editor_uri_absolute(slashes=True),
SwaggerEditorHandler(), suffix=suffix)
doc.app.add_route(doc.editor_uri_absolute(slashes=False),
SwaggerEditorHandler(), suffix=suffix)
if doc.config_rel_url is None:
doc.app.add_route(doc.swagger_json_uri_absolute, SwaggerConfigHandler(), suffix=suffix)
doc.app.add_static_route(
prefix=doc.static_uri_absolute,
directory='{}/'.format(doc.static_dir),
downloadable=True,
)
def match(doc):
try:
import falcon
interface = None
if Version(falcon.__version__) >= Version('3.0.0'):
import falcon.asgi
if isinstance(doc.app, falcon.asgi.App):
interface = FalconInterface(use_async=True)
elif isinstance(doc.app, falcon.App):
interface = FalconInterface(use_async=False)
else:
if isinstance(doc.app, falcon.API):
interface = FalconInterface(use_async=False)
if interface:
return interface.handler
except ImportError:
pass
return None