10
10
from openapi_core .schema .servers import is_absolute
11
11
from openapi_core .spec import Spec
12
12
from openapi_core .templating .datatypes import TemplateResult
13
- from openapi_core .templating .paths .datatypes import OperationPath
14
13
from openapi_core .templating .paths .datatypes import Path
15
- from openapi_core .templating .paths .datatypes import ServerOperationPath
14
+ from openapi_core .templating .paths .datatypes import PathOperation
15
+ from openapi_core .templating .paths .datatypes import PathOperationServer
16
16
from openapi_core .templating .paths .exceptions import OperationNotFound
17
17
from openapi_core .templating .paths .exceptions import PathNotFound
18
18
from openapi_core .templating .paths .exceptions import ServerNotFound
21
21
from openapi_core .templating .util import search
22
22
23
23
24
- class PathFinder :
24
+ class BasePathFinder :
25
25
def __init__ (self , spec : Spec , base_url : Optional [str ] = None ):
26
26
self .spec = spec
27
27
self .base_url = base_url
28
28
29
- def find (
30
- self ,
31
- method : str ,
32
- full_url : str ,
33
- ) -> ServerOperationPath :
34
- paths_iter = self ._get_paths_iter (full_url )
29
+ def find (self , method : str , name : str ) -> PathOperationServer :
30
+ paths_iter = self ._get_paths_iter (name )
35
31
paths_iter_peek = peekable (paths_iter )
36
32
37
33
if not paths_iter_peek :
38
- raise PathNotFound (full_url )
34
+ raise PathNotFound (name )
39
35
40
- operations_iter = self ._get_operations_iter (paths_iter_peek , method )
36
+ operations_iter = self ._get_operations_iter (method , paths_iter_peek )
41
37
operations_iter_peek = peekable (operations_iter )
42
38
43
39
if not operations_iter_peek :
44
- raise OperationNotFound (full_url , method )
40
+ raise OperationNotFound (name , method )
45
41
46
42
servers_iter = self ._get_servers_iter (
43
+ name ,
47
44
operations_iter_peek ,
48
- full_url ,
49
45
)
50
46
51
47
try :
52
48
return next (servers_iter )
53
49
except StopIteration :
54
- raise ServerNotFound (full_url )
50
+ raise ServerNotFound (name )
55
51
56
- def _get_paths_iter (self , full_url : str ) -> Iterator [Path ]:
52
+ def _get_paths_iter (self , name : str ) -> Iterator [Path ]:
53
+ raise NotImplementedError
54
+
55
+ def _get_operations_iter (
56
+ self , method : str , paths_iter : Iterator [Path ]
57
+ ) -> Iterator [PathOperation ]:
58
+ for path , path_result in paths_iter :
59
+ if method not in path :
60
+ continue
61
+ operation = path / method
62
+ yield PathOperation (path , operation , path_result )
63
+
64
+ def _get_servers_iter (
65
+ self , name : str , operations_iter : Iterator [PathOperation ]
66
+ ) -> Iterator [PathOperationServer ]:
67
+ raise NotImplementedError
68
+
69
+
70
+ class APICallPathFinder (BasePathFinder ):
71
+ def __init__ (self , spec : Spec , base_url : Optional [str ] = None ):
72
+ self .spec = spec
73
+ self .base_url = base_url
74
+
75
+ def _get_paths_iter (self , name : str ) -> Iterator [Path ]:
57
76
template_paths : List [Path ] = []
58
77
paths = self .spec / "paths"
59
78
for path_pattern , path in list (paths .items ()):
60
79
# simple path.
61
80
# Return right away since it is always the most concrete
62
- if full_url .endswith (path_pattern ):
81
+ if name .endswith (path_pattern ):
63
82
path_result = TemplateResult (path_pattern , {})
64
83
yield Path (path , path_result )
65
84
# template path
66
85
else :
67
- result = search (path_pattern , full_url )
86
+ result = search (path_pattern , name )
68
87
if result :
69
88
path_result = TemplateResult (path_pattern , result .named )
70
89
template_paths .append (Path (path , path_result ))
71
90
72
91
# Fewer variables -> more concrete path
73
92
yield from sorted (template_paths , key = template_path_len )
74
93
75
- def _get_operations_iter (
76
- self , paths_iter : Iterator [Path ], request_method : str
77
- ) -> Iterator [OperationPath ]:
78
- for path , path_result in paths_iter :
79
- if request_method not in path :
80
- continue
81
- operation = path / request_method
82
- yield OperationPath (path , operation , path_result )
83
-
84
94
def _get_servers_iter (
85
- self , operations_iter : Iterator [ OperationPath ], full_url : str
86
- ) -> Iterator [ServerOperationPath ]:
95
+ self , name : str , operations_iter : Iterator [ PathOperation ]
96
+ ) -> Iterator [PathOperationServer ]:
87
97
for path , operation , path_result in operations_iter :
88
98
servers = (
89
99
path .get ("servers" , None )
90
100
or operation .get ("servers" , None )
91
101
or self .spec .get ("servers" , [{"url" : "/" }])
92
102
)
93
103
for server in servers :
94
- server_url_pattern = full_url .rsplit (path_result .resolved , 1 )[
95
- 0
96
- ]
104
+ server_url_pattern = name .rsplit (path_result .resolved , 1 )[0 ]
97
105
server_url = server ["url" ]
98
106
if not is_absolute (server_url ):
99
107
# relative to absolute url
@@ -107,7 +115,7 @@ def _get_servers_iter(
107
115
# simple path
108
116
if server_url_pattern == server_url :
109
117
server_result = TemplateResult (server ["url" ], {})
110
- yield ServerOperationPath (
118
+ yield PathOperationServer (
111
119
path ,
112
120
operation ,
113
121
server ,
@@ -121,10 +129,31 @@ def _get_servers_iter(
121
129
server_result = TemplateResult (
122
130
server ["url" ], result .named
123
131
)
124
- yield ServerOperationPath (
132
+ yield PathOperationServer (
125
133
path ,
126
134
operation ,
127
135
server ,
128
136
path_result ,
129
137
server_result ,
130
138
)
139
+
140
+
141
+ class WebhookPathFinder (BasePathFinder ):
142
+ def _get_paths_iter (self , name : str ) -> Iterator [Path ]:
143
+ webhooks = self .spec / "webhooks"
144
+ for webhook_name , path in list (webhooks .items ()):
145
+ if name == webhook_name :
146
+ path_result = TemplateResult (webhook_name , {})
147
+ yield Path (path , path_result )
148
+
149
+ def _get_servers_iter (
150
+ self , name : str , operations_iter : Iterator [PathOperation ]
151
+ ) -> Iterator [PathOperationServer ]:
152
+ for path , operation , path_result in operations_iter :
153
+ yield PathOperationServer (
154
+ path ,
155
+ operation ,
156
+ None ,
157
+ path_result ,
158
+ {},
159
+ )
0 commit comments