This repository was archived by the owner on Mar 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathencode.py
221 lines (181 loc) · 6.51 KB
/
encode.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import coreschema
from collections import OrderedDict
from coreapi.compat import urlparse
from openapi_codec.utils import get_method, get_encoding, get_location, get_links_from_document
def generate_swagger_object(document):
"""
Generates root of the Swagger spec.
"""
parsed_url = urlparse.urlparse(document.url)
swagger = OrderedDict()
swagger['swagger'] = '2.0'
swagger['info'] = OrderedDict()
swagger['info']['title'] = document.title
swagger['info']['description'] = document.description
swagger['info']['version'] = '' # Required by the spec
if parsed_url.netloc:
swagger['host'] = parsed_url.netloc
if parsed_url.scheme:
swagger['schemes'] = [parsed_url.scheme]
swagger['paths'] = _get_paths_object(document)
return swagger
def _add_tag_prefix(item):
operation_id, link, tags = item
if tags:
operation_id = tags[0] + '_' + operation_id
return (operation_id, link, tags)
def _get_links(document):
"""
Return a list of (operation_id, link, [tags])
"""
# Extract all the links from the first or second level of the document.
links = []
for keys, link in get_links_from_document(document):
if len(keys) > 1:
operation_id = '_'.join(keys[1:])
tags = [keys[0]]
else:
operation_id = keys[0]
tags = []
links.append((operation_id, link, tags))
# Determine if the operation ids each have unique names or not.
operation_ids = [item[0] for item in links]
unique = len(set(operation_ids)) == len(links)
# If the operation ids are not unique, then prefix them with the tag.
if not unique:
return [_add_tag_prefix(item) for item in links]
return links
def _get_paths_object(document):
paths = OrderedDict()
links = _get_links(document)
for operation_id, link, tags in links:
if link.url not in paths:
paths[link.url] = OrderedDict()
method = get_method(link)
operation = _get_operation(operation_id, link, tags)
paths[link.url].update({method: operation})
return paths
def _get_operation(operation_id, link, tags):
encoding = get_encoding(link)
description = link.description.strip()
summary = description.splitlines()[0] if description else None
operation = {
'operationId': operation_id,
'responses': _get_responses(link),
'parameters': _get_parameters(link, encoding)
}
if description:
operation['description'] = description
if summary:
operation['summary'] = summary
if encoding:
operation['consumes'] = [encoding]
if tags:
operation['tags'] = tags
return operation
def _get_field_description(field):
if getattr(field, 'description', None) is not None:
# Deprecated
return field.description
if field.schema is None:
return ''
return field.schema.description
def _get_field_type(field):
if getattr(field, 'type', None) is not None:
# Deprecated
return field.type
if field.schema is None:
return 'string'
return {
coreschema.String: 'string',
coreschema.Integer: 'integer',
coreschema.Number: 'number',
coreschema.Boolean: 'boolean',
coreschema.Array: 'array',
coreschema.Object: 'object',
}.get(field.schema.__class__, 'string')
def _get_parameters(link, encoding):
"""
Generates Swagger Parameter Item object.
"""
parameters = []
properties = {}
required = []
for field in link.fields:
location = get_location(link, field)
field_description = _get_field_description(field)
field_type = _get_field_type(field)
if location == 'form':
if encoding in ('multipart/form-data', 'application/x-www-form-urlencoded'):
# 'formData' in swagger MUST be one of these media types.
parameter = {
'name': field.name,
'required': field.required,
'in': 'formData',
'description': field_description,
'type': field_type,
}
if field_type == 'array':
parameter['items'] = {'type': 'string'}
parameters.append(parameter)
else:
# Expand coreapi fields with location='form' into a single swagger
# parameter, with a schema containing multiple properties.
schema_property = {
'description': field_description,
'type': field_type,
}
if field_type == 'array':
schema_property['items'] = {'type': 'string'}
properties[field.name] = schema_property
if field.required:
required.append(field.name)
elif location == 'body':
if encoding == 'application/octet-stream':
# https://github.com/OAI/OpenAPI-Specification/issues/50#issuecomment-112063782
schema = {'type': 'string', 'format': 'binary'}
else:
schema = getattr(field, 'schema', {})
parameter = {
'name': field.name,
'required': field.required,
'in': location,
'description': field_description,
'schema': schema
}
parameters.append(parameter)
else:
parameter = {
'name': field.name,
'required': field.required,
'in': location,
'description': field_description,
'type': field_type or 'string',
}
if field_type == 'array':
parameter['items'] = {'type': 'string'}
parameters.append(parameter)
if properties:
parameter = {
'name': 'data',
'in': 'body',
'schema': {
'type': 'object',
'properties': properties
}
}
if required:
parameter['schema']['required'] = required
parameters.append(parameter)
return parameters
def _get_responses(link):
"""
Returns minimally acceptable responses object based
on action / method type.
"""
template = {'description': ''}
if link.action.lower() == 'post':
return {'201': template}
if link.action.lower() == 'delete':
return {'204': template}
return {'200': template}