Skip to content

Commit 0499fd7

Browse files
committed
Move handlers to separate package
1 parent 667fb01 commit 0499fd7

File tree

7 files changed

+105
-47
lines changed

7 files changed

+105
-47
lines changed

openapi_spec_validator/handlers.py

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from openapi_spec_validator.handlers.file import FileObjectHandler
2+
try:
3+
from openapi_spec_validator.handlers.requests import (
4+
UrlRequestsHandler as UrlHandler,
5+
)
6+
except ImportError:
7+
from openapi_spec_validator.handlers.urllib import (
8+
UrllibHandler as UrlHandler,
9+
)
10+
11+
__all__ = ['FileObjectHandler', 'UrlHandler']
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""OpenAPI spec validator handlers file module."""
2+
from openapi_spec_validator.loaders import ExtendedSafeLoader
3+
4+
5+
class BaseHandler(object):
6+
"""OpenAPI spec validator base handler."""
7+
8+
def __init__(self, **options):
9+
self.options = options
10+
11+
@property
12+
def loader(self):
13+
return self.options.get('loader', ExtendedSafeLoader)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""OpenAPI spec validator handlers file module."""
2+
from six import StringIO
3+
from yaml import load
4+
5+
from openapi_spec_validator.handlers.base import BaseHandler
6+
7+
8+
class FileObjectHandler(BaseHandler):
9+
"""OpenAPI spec validator file-like object handler."""
10+
11+
def __call__(self, f):
12+
return load(f, self.loader)
13+
14+
15+
class FileHandler(FileObjectHandler):
16+
"""OpenAPI spec validator file path handler."""
17+
18+
def __call__(self, f):
19+
if isinstance(f, StringIO):
20+
return super(FileHandler, self).__call__(f)
21+
22+
assert f.startswith("file")
23+
24+
filename = f[7:]
25+
with open(filename) as fh:
26+
return super(FileHandler, self).__call__(fh)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""OpenAPI spec validator handlers requests module."""
2+
from __future__ import absolute_import
3+
import contextlib
4+
5+
from six import StringIO
6+
from six.moves.urllib.parse import urlparse
7+
import requests
8+
9+
from openapi_spec_validator.handlers.file import FileHandler
10+
11+
12+
class UrlRequestsHandler(FileHandler):
13+
"""OpenAPI spec validator URL (requests) scheme handler."""
14+
15+
def __init__(self, *allowed_schemes, **options):
16+
super(UrlRequestsHandler, self).__init__(**options)
17+
self.allowed_schemes = allowed_schemes
18+
19+
def __call__(self, url, timeout=1):
20+
scheme = urlparse(url).scheme
21+
assert scheme in self.allowed_schemes
22+
23+
if scheme == "file":
24+
return super(UrlRequestsHandler, self).__call__(url)
25+
26+
response = requests.get(url, timeout=timeout)
27+
response.raise_for_status()
28+
29+
data = StringIO(response.text)
30+
with contextlib.closing(data) as fh:
31+
return super(UrlRequestsHandler, self).__call__(fh)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""OpenAPI spec validator handlers requests module."""
2+
import contextlib
3+
4+
from six.moves.urllib.parse import urlparse
5+
from six.moves.urllib.request import urlopen
6+
7+
from openapi_spec_validator.handlers.file import FileObjectHandler
8+
9+
10+
class UrllibHandler(FileObjectHandler):
11+
"""OpenAPI spec validator URL (urllib) scheme handler."""
12+
13+
def __init__(self, *allowed_schemes, **options):
14+
super(UrllibHandler, self).__init__(**options)
15+
self.allowed_schemes = allowed_schemes
16+
17+
def __call__(self, url, timeout=1):
18+
assert urlparse(url).scheme in self.allowed_schemes
19+
20+
f = urlopen(url, timeout=timeout)
21+
22+
with contextlib.closing(f) as fh:
23+
return super(UrllibHandler, self).__call__(fh)

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ install_requires =
2929
PyYAML>=5.1
3030
six
3131
pathlib2; python_version<"3.0"
32-
requests
3332
tests_require =
3433
mock; python_version<"3.0"
3534
pytest
@@ -47,6 +46,7 @@ exclude =
4746

4847
[options.extras_require]
4948
dev = pre-commit
49+
requests = requests
5050

5151
[tool:pytest]
5252
addopts = -sv --flake8 --junitxml reports/junit.xml --cov openapi_spec_validator --cov-report term-missing --cov-report xml:reports/coverage.xml

0 commit comments

Comments
 (0)