Skip to content

Handlers timeout setup fix #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions openapi_spec_validator/handlers/base.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
"""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)
def __call__(self, f):
raise NotImplementedError
4 changes: 4 additions & 0 deletions openapi_spec_validator/handlers/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
from yaml import load

from openapi_spec_validator.handlers.base import BaseHandler
from openapi_spec_validator.loaders import ExtendedSafeLoader


class FileObjectHandler(BaseHandler):
"""OpenAPI spec validator file-like object handler."""

def __init__(self, loader=ExtendedSafeLoader):
self.loader = loader

def __call__(self, f):
return load(f, self.loader)

Expand Down
5 changes: 3 additions & 2 deletions openapi_spec_validator/handlers/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ class UrlRequestsHandler(FileHandler):
"""OpenAPI spec validator URL (requests) scheme handler."""

def __init__(self, *allowed_schemes, **options):
self.timeout = options.pop('timeout', 10)
super(UrlRequestsHandler, self).__init__(**options)
self.allowed_schemes = allowed_schemes

def __call__(self, url, timeout=1):
def __call__(self, url):
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 = requests.get(url, timeout=self.timeout)
response.raise_for_status()

data = StringIO(response.text)
Expand Down
5 changes: 3 additions & 2 deletions openapi_spec_validator/handlers/urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ class UrllibHandler(FileObjectHandler):
"""OpenAPI spec validator URL (urllib) scheme handler."""

def __init__(self, *allowed_schemes, **options):
self.timeout = options.pop('timeout', 10)
super(UrllibHandler, self).__init__(**options)
self.allowed_schemes = allowed_schemes

def __call__(self, url, timeout=1):
def __call__(self, url):
assert urlparse(url).scheme in self.allowed_schemes

f = urlopen(url, timeout=timeout)
f = urlopen(url, timeout=self.timeout)

with contextlib.closing(f) as fh:
return super(UrllibHandler, self).__call__(fh)