-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from Beirdo/master
Switched to using requests rather than direct use of urllib3
- Loading branch information
Showing
9 changed files
with
151 additions
and
48 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from openapi_spec_validator.handlers.file import FileObjectHandler | ||
try: | ||
from openapi_spec_validator.handlers.requests import ( | ||
UrlRequestsHandler as UrlHandler, | ||
) | ||
except ImportError: | ||
from openapi_spec_validator.handlers.urllib import ( | ||
UrllibHandler as UrlHandler, | ||
) | ||
|
||
__all__ = ['FileObjectHandler', 'UrlHandler'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""OpenAPI spec validator handlers file module.""" | ||
from openapi_spec_validator.loaders import ExtendedSafeLoader | ||
|
||
|
||
class BaseHandler(object): | ||
"""OpenAPI spec validator base handler.""" | ||
|
||
def __init__(self, **options): | ||
self.options = options | ||
|
||
@property | ||
def loader(self): | ||
return self.options.get('loader', ExtendedSafeLoader) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""OpenAPI spec validator handlers file module.""" | ||
from six import StringIO | ||
from yaml import load | ||
|
||
from openapi_spec_validator.handlers.base import BaseHandler | ||
|
||
|
||
class FileObjectHandler(BaseHandler): | ||
"""OpenAPI spec validator file-like object handler.""" | ||
|
||
def __call__(self, f): | ||
return load(f, self.loader) | ||
|
||
|
||
class FileHandler(FileObjectHandler): | ||
"""OpenAPI spec validator file path handler.""" | ||
|
||
def __call__(self, f): | ||
if isinstance(f, StringIO): | ||
return super(FileHandler, self).__call__(f) | ||
|
||
assert f.startswith("file") | ||
|
||
filename = f[7:] | ||
with open(filename) as fh: | ||
return super(FileHandler, self).__call__(fh) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""OpenAPI spec validator handlers requests module.""" | ||
from __future__ import absolute_import | ||
import contextlib | ||
|
||
from six import StringIO | ||
from six.moves.urllib.parse import urlparse | ||
import requests | ||
|
||
from openapi_spec_validator.handlers.file import FileHandler | ||
|
||
|
||
class UrlRequestsHandler(FileHandler): | ||
"""OpenAPI spec validator URL (requests) scheme handler.""" | ||
|
||
def __init__(self, *allowed_schemes, **options): | ||
super(UrlRequestsHandler, self).__init__(**options) | ||
self.allowed_schemes = allowed_schemes | ||
|
||
def __call__(self, url, timeout=1): | ||
scheme = urlparse(url).scheme | ||
assert scheme in self.allowed_schemes | ||
|
||
if scheme == "file": | ||
return super(UrlRequestsHandler, self).__call__(url) | ||
|
||
response = requests.get(url, timeout=timeout) | ||
response.raise_for_status() | ||
|
||
data = StringIO(response.text) | ||
with contextlib.closing(data) as fh: | ||
return super(UrlRequestsHandler, self).__call__(fh) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"""OpenAPI spec validator handlers requests module.""" | ||
import contextlib | ||
|
||
from six.moves.urllib.parse import urlparse | ||
from six.moves.urllib.request import urlopen | ||
|
||
from openapi_spec_validator.handlers.file import FileObjectHandler | ||
|
||
|
||
class UrllibHandler(FileObjectHandler): | ||
"""OpenAPI spec validator URL (urllib) scheme handler.""" | ||
|
||
def __init__(self, *allowed_schemes, **options): | ||
super(UrllibHandler, self).__init__(**options) | ||
self.allowed_schemes = allowed_schemes | ||
|
||
def __call__(self, url, timeout=1): | ||
assert urlparse(url).scheme in self.allowed_schemes | ||
|
||
f = urlopen(url, timeout=timeout) | ||
|
||
with contextlib.closing(f) as fh: | ||
return super(UrllibHandler, self).__call__(fh) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
jsonschema | ||
PyYAML==4.2b4 | ||
six==1.12.0 | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters