forked from core-api/python-openapi-codec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.py
More file actions
82 lines (64 loc) · 1.86 KB
/
encode.py
File metadata and controls
82 lines (64 loc) · 1.86 KB
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
72
73
74
75
76
77
78
79
80
81
82
from coreapi.compat import urlparse
def generate_swagger_object(document):
"""
Generates root of the Swagger spec.
"""
parsed_url = urlparse.urlparse(document.url)
return {
'swagger': '2.0',
'info': _get_info_object(document),
'paths': _get_paths_object(document),
'host': parsed_url.netloc,
}
def _get_info_object(document):
return {
'title': document.title,
'version': '' # Required by the spec
}
def _get_paths_object(document):
paths = {}
for tag, object_ in document.data.items():
if not hasattr(object_, 'links'):
continue
for link in object_.links.values():
if link.url not in paths:
paths[link.url] = {}
operation = _get_operation(tag, link)
paths[link.url].update({link.action: operation})
return paths
def _get_operation(tag, link):
return {
'tags': [tag],
'description': link.description,
'responses': _get_responses(link),
'parameters': _get_parameters(link.fields)
}
def _get_parameters(fields):
"""
Generates Swagger Parameter Item object.
"""
return [
{
'name': field.name,
'required': field.required,
'in': _convert_location_to_in(field),
'description': field.description,
'type': 'string'
}
for field in fields
]
def _convert_location_to_in(field):
if field.location == 'form':
return 'formData'
return field.location
def _get_responses(link):
"""
Returns minimally acceptable responses object based
on action / method type.
"""
template = {'description': ''}
if link.action == 'post':
return {'201': template}
if link.action == 'delete':
return {'204': template}
return {'200': template}