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