Skip to content

Commit f678e24

Browse files
committed
Move path finder to separate templating module
1 parent 25bfd59 commit f678e24

File tree

8 files changed

+39
-19
lines changed

8 files changed

+39
-19
lines changed

openapi_core/templating/__init__.py

Whitespace-only changes.

openapi_core/templating/paths/__init__.py

Whitespace-only changes.
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from parse import parse
2+
from six.moves.urllib.parse import urljoin
3+
4+
from openapi_core.templating.paths.exceptions import PathNotFound
5+
from openapi_core.templating.paths.util import get_operation_pattern
6+
7+
8+
class PathFinder(object):
9+
10+
def __init__(self, spec):
11+
self.spec = spec
12+
13+
def find(self, request):
14+
operation_pattern = self._get_operation_pattern(request)
15+
16+
path = self.spec[operation_pattern]
17+
path_variables = {}
18+
operation = self.spec.get_operation(operation_pattern, request.method)
19+
servers = path.servers or operation.servers or self.spec.servers
20+
server = servers[0]
21+
server_variables = {}
22+
23+
return path, operation, server, path_variables, server_variables
24+
25+
def _get_operation_pattern(self, request):
26+
server = self.spec.get_server(request.full_url_pattern)
27+
28+
return get_operation_pattern(
29+
server.default_url, request.full_url_pattern
30+
)

openapi_core/validation/util.py renamed to openapi_core/templating/paths/util.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
"""OpenAPI core validation util module"""
1+
"""OpenAPI core templating paths util module"""
22
from six.moves.urllib.parse import urlparse
33

44

5-
def is_absolute(url):
6-
return url.startswith('//') or '://' in url
7-
8-
95
def path_qs(url):
106
pr = urlparse(url)
117
result = pr.path
@@ -14,6 +10,10 @@ def path_qs(url):
1410
return result
1511

1612

13+
def is_absolute(url):
14+
return url.startswith('//') or '://' in url
15+
16+
1717
def get_operation_pattern(server_url, request_url_pattern):
1818
"""Return an updated request URL pattern with the server URL removed."""
1919
if server_url[-1] == "/":

openapi_core/validation/request/validators.py

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from openapi_core.validation.request.datatypes import (
2222
RequestParameters, RequestValidationResult,
2323
)
24-
from openapi_core.validation.util import get_operation_pattern
2524
from openapi_core.validation.validators import BaseValidator
2625

2726

openapi_core/validation/response/validators.py

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
UnmarshalError, ValidateError,
1414
)
1515
from openapi_core.validation.response.datatypes import ResponseValidationResult
16-
from openapi_core.validation.util import get_operation_pattern
1716
from openapi_core.validation.validators import BaseValidator
1817

1918

openapi_core/validation/validators.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
UnmarshalError, ValidateError,
1313
)
1414
from openapi_core.validation.response.datatypes import ResponseValidationResult
15-
from openapi_core.validation.util import get_operation_pattern
1615

1716

1817
class BaseValidator(object):
@@ -26,16 +25,9 @@ def __init__(
2625
self.custom_media_type_deserializers = custom_media_type_deserializers
2726

2827
def _find_path(self, request):
29-
operation_pattern = self._get_operation_pattern(request)
30-
31-
path = self.spec[operation_pattern]
32-
path_variables = {}
33-
operation = self.spec.get_operation(operation_pattern, request.method)
34-
servers = path.servers or operation.servers or self.spec.servers
35-
server = servers[0]
36-
server_variables = {}
37-
38-
return path, operation, server, path_variables, server_variables
28+
from openapi_core.templating.paths.finders import PathFinder
29+
finder = PathFinder(self.spec)
30+
return finder.find(request)
3931

4032
def _get_operation_pattern(self, request):
4133
server = self.spec.get_server(request.full_url_pattern)

tests/unit/validation/test_util.py renamed to tests/unit/templating/test_paths_util.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from openapi_core.validation.util import path_qs
1+
from openapi_core.templating.paths.util import path_qs
22

33

44
class TestPathQs(object):

0 commit comments

Comments
 (0)