-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapispec_restful.py
More file actions
36 lines (26 loc) · 946 Bytes
/
Copy pathapispec_restful.py
File metadata and controls
36 lines (26 loc) · 946 Bytes
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
import re
from textwrap import dedent
import yaml
def path_from_resource(spec, api, resource, **kwargs):
"""Extracts swagger spec from `resource` methods."""
from apispec import Path
assert resource is not None
for endpoint, view in api.app.view_functions.iteritems():
if getattr(view, 'view_class', None) == resource:
break
else:
raise RuntimeError
for rule in api.app.url_map.iter_rules():
if rule.endpoint == endpoint:
break
else:
raise RuntimeError
path = re.sub(r'<(?:[^:<>]+:)?([^<>]+)>', r'{\1}', rule.rule)
operations = {}
for method in map(str.lower, resource.methods):
doc = re.sub('^.*(?=---)', '', getattr(resource, method).__doc__)
doc = dedent(doc)
operations[method] = yaml.load(doc)
return Path(path=path, operations=operations)
def setup(spec):
spec.register_path_helper(path_from_resource)