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
Expand file tree
/
Copy pathencode.py
More file actions
355 lines (288 loc) · 10.7 KB
/
encode.py
File metadata and controls
355 lines (288 loc) · 10.7 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import random
import string
import coreschema
from collections import OrderedDict
from coreapi.document import Field
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']['version'] = '' # Required by the spec
if parsed_url.netloc:
swagger['host'] = parsed_url.netloc
if parsed_url.scheme:
swagger['schemes'] = [parsed_url.scheme]
if not parsed_url.netloc and not parsed_url.scheme:
swagger['host'] = document.url
swagger['definitions'] = _get_definitions(document)
swagger['paths'] = _get_paths_object(document, swagger['definitions'])
return swagger
def _get_or_update_definitions(update_def_data, update_def_name, definitions):
"""
Updates definitions with provided data If definition is not present in map, returns found definition
data in case definition overlaps with existing one.
"""
# Check if there's existing definition with same name or props
clashing_def_names = \
[d for d in definitions.keys() if d.startswith(update_def_name) or definitions.get(d) == update_def_data]
for clashing_def_name in clashing_def_names:
clash_def_data = definitions.get(clashing_def_name)
if clash_def_data == update_def_data:
return clash_def_data
else:
if list(clashing_def_names):
rand_part = ''.join([random.choice(string.ascii_letters + string.digits) for _ in range(5)])
update_def_name = '{}_{}'.format(update_def_name, rand_part)
definitions[update_def_name] = update_def_data
return update_def_data
def _get_field_definition_data(field_item, defs):
"""
Returns dictionary with field definition data.
"""
definition_data = {
'type': 'object',
'properties': {}
}
if isinstance(field_item, coreschema.Object):
props = field_item.properties
elif isinstance(field_item.schema, coreschema.schemas.Array):
props = field_item.schema.items.properties
else:
try:
props = field_item.schema.properties
except AttributeError:
props = OrderedDict()
for f_name, f_schema in iter(props.items()):
if _get_field_type(f_schema) is 'object':
def_data = _get_or_update_definitions(
_get_field_definition_data(f_schema, defs),
'{}_def_item'.format(f_schema.name),
defs
)
if def_data:
return def_data
else:
definition_data['properties'][f_name] = {
'type': _get_field_type(f_schema),
'description': ''
}
return definition_data
def _get_definitions(document):
"""
Returns dictionary with schema definitions.
"""
definitions = OrderedDict()
links = _get_links(document)
for _, link, _ in links:
for field in link.fields:
field_type = _get_field_type(field)
# Get field definition data
if field_type == 'array':
def_data = _get_field_definition_data(field.schema.items, definitions)
else:
def_data = _get_field_definition_data(field, definitions)
_get_or_update_definitions(
def_data,
'{}_def_item'.format(field.name),
definitions
)
return definitions
def _add_tag_prefix(item):
"""
Returns tuple (operation_id, link, tags) with modified operation_id in case of tags.
"""
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, definitions):
"""
Returns dictionary with document paths.
"""
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, definitions)
paths[link.url].update({method: operation})
return paths
def _get_operation(operation_id, link, tags, definitions):
"""
Returns dictionary with operation parameters.
"""
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, definitions)
}
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):
"""
Returns field description.
"""
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):
"""
Returns field string type by the given field schema.
"""
if getattr(field, 'type', None) is not None:
# Deprecated
return field.type
if isinstance(field, Field):
cls = field.schema.__class__
else:
cls = field.__class__
return {
coreschema.String: 'string',
coreschema.Integer: 'integer',
coreschema.Number: 'number',
coreschema.Boolean: 'boolean',
coreschema.Array: 'array',
coreschema.Object: 'object',
}.get(cls, 'string')
def _get_parameters(link, encoding, definitions):
"""
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 in ('object', 'array'):
definition_data = _get_field_definition_data(field, definitions)
definition_data = definition_data.get('properties')
defs = filter(lambda d: definitions.get(d).get('properties') == definition_data, definitions)
if defs:
# Note: Python2.X <-> Python3.X
try:
def_name = defs[0]
except TypeError:
def_name = next(defs)
schema_property = {'$ref': '#/definitions/{}'.format(def_name)}
if field_type == 'array':
schema_property.pop('$ref')
schema_property['type'] = 'array'
schema_property['items'] = {
'$ref': '#/definitions/{}'.format(def_name)
}
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 = {}
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}