-
Notifications
You must be signed in to change notification settings - Fork 2
/
resolver.py
62 lines (48 loc) · 2.06 KB
/
resolver.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
from connexion import Resolver
# see https://github.com/zalando/connexion/issues/206
class ApiResolver(Resolver):
"""
Resolves endpoint functions using REST semantics (unless overridden by specifying operationId)
"""
def __init__(self, default_module_name, collection_endpoint_name='search'):
"""
:param default_module_name: Default module name for operations
:type default_module_name: str
"""
Resolver.__init__(self)
self.default_module_name = default_module_name
self.collection_endpoint_name = collection_endpoint_name
def resolve_operation_id(self, operation):
"""
Resolves the operationId using REST semantics unless explicitly configured in the spec
:type operation: connexion.operations.AbstractOperation
"""
if operation.operation_id:
return Resolver.resolve_operation_id(self, operation)
return self.resolve_operation_id_using_rest_semantics(operation)
def resolve_operation_id_using_rest_semantics(self, operation):
"""
Resolves the operationId using REST semantics
:type operation: connexion.operations.AbstractOperation
"""
elements = operation.path.split('/')[1:]
# prefix part of the function name (i.e. file on disk)
if operation.router_controller:
name = operation.router_controller
else:
name = self.default_module_name
# append all other paths using _ names, except parameters
for path in elements:
if '{' not in path:
if name.count('.') < 2:
name += '.' + path.replace('-', '_')
else:
name += '_' + path.replace('-', '_')
# append method
method = operation.method.lower()
if method == 'get' and '{' not in elements[-1]:
method = self.collection_endpoint_name
if name.count('.') < 2:
return name + '.' + method
else:
return name + '_' + method