Skip to content

Commit 667fb01

Browse files
Beirdop1c2u
authored andcommitted
Switched to using requests rather than direct use of urllib3
- Use a StringIO buffer as YAML is stupid and won't use a Byte Stream
1 parent 76b4e2a commit 667fb01

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

openapi_spec_validator/handlers.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""OpenAPI spec validator handlers module."""
22
import contextlib
3+
from io import StringIO
34

45
from six.moves.urllib.parse import urlparse
5-
from six.moves.urllib.request import urlopen
66
from yaml import load
7+
import requests
78

89
from openapi_spec_validator.loaders import ExtendedSafeLoader
910

@@ -30,8 +31,16 @@ def __init__(self, *allowed_schemes, **options):
3031
self.allowed_schemes = allowed_schemes
3132

3233
def __call__(self, url, timeout=1):
33-
assert urlparse(url).scheme in self.allowed_schemes
34-
35-
f = urlopen(url, timeout=timeout)
36-
with contextlib.closing(f) as fh:
34+
scheme = urlparse(url).scheme
35+
assert scheme in self.allowed_schemes
36+
37+
if scheme == "file":
38+
filename = url[7:]
39+
with open(filename) as fh:
40+
return super(UrlHandler, self).__call__(fh)
41+
42+
response = requests.get(url, timeout=timeout)
43+
response.raise_for_status()
44+
data = response.text
45+
with contextlib.closing(StringIO(data)) as fh:
3746
return super(UrlHandler, self).__call__(fh)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
jsonschema
22
PyYAML==4.2b4
33
six==1.12.0
4+
requests

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ install_requires =
2929
PyYAML>=5.1
3030
six
3131
pathlib2; python_version<"3.0"
32+
requests
3233
tests_require =
3334
mock; python_version<"3.0"
3435
pytest

0 commit comments

Comments
 (0)