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 pathutils.py
More file actions
54 lines (44 loc) · 1.43 KB
/
utils.py
File metadata and controls
54 lines (44 loc) · 1.43 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
def link_sorting_key(link_item):
keys, link = link_item
action_priority = {
'': 0, 'get': 0,
'post': 1,
'put': 2,
'patch': 3,
'delete': 4
}.get(link.action, 5)
return (link.url, action_priority)
def get_links_from_document(node, keys=()):
links = []
for key, link in getattr(node, 'links', {}).items():
# Get all the resources at this level
index = keys + (key,)
links.append((index, link))
for key, child in getattr(node, 'data', {}).items():
# Descend into any nested structures.
index = keys + (key,)
links.extend(get_links_from_document(child, index))
return sorted(links, key=link_sorting_key)
def get_method(link):
method = link.action.lower()
if not method:
method = 'get'
return method
def get_parameter_name(link):
return link.title.title() + link.action.title() + "Params"
def get_encoding(link):
encoding = link.encoding
has_body = any([get_location(link, field) in ('form', 'body') for field in link.fields])
if not encoding and has_body:
encoding = 'application/json'
elif encoding and not has_body:
encoding = ''
return encoding
def get_location(link, field):
location = field.location
if not location:
if get_method(link) in ('get', 'delete'):
location = 'query'
else:
location = 'form'
return location