Skip to content
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

Switched to using requests rather than direct use of urllib3 #93

Merged
merged 3 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 0 additions & 37 deletions openapi_spec_validator/handlers.py

This file was deleted.

11 changes: 11 additions & 0 deletions openapi_spec_validator/handlers/__init__.py
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']
13 changes: 13 additions & 0 deletions openapi_spec_validator/handlers/base.py
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)
26 changes: 26 additions & 0 deletions openapi_spec_validator/handlers/file.py
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)
31 changes: 31 additions & 0 deletions openapi_spec_validator/handlers/requests.py
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)
23 changes: 23 additions & 0 deletions openapi_spec_validator/handlers/urllib.py
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)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
jsonschema
PyYAML==4.2b4
six==1.12.0
requests
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ exclude =

[options.extras_require]
dev = pre-commit
requests = requests

[tool:pytest]
addopts = -sv --flake8 --junitxml reports/junit.xml --cov openapi_spec_validator --cov-report term-missing --cov-report xml:reports/coverage.xml
56 changes: 45 additions & 11 deletions tests/integration/test_shortcuts.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import pytest

from openapi_spec_validator import validate_spec, validate_spec_url
from openapi_spec_validator import validate_v2_spec, validate_v2_spec_url
from openapi_spec_validator import (
validate_spec, validate_spec_url,
validate_v2_spec, validate_v2_spec_url,
validate_spec_url_factory,
openapi_v2_spec_validator, openapi_v3_spec_validator,
)
from openapi_spec_validator.exceptions import OpenAPIValidationError
from openapi_spec_validator.handlers.urllib import UrllibHandler


class BaseTestValidValidteV2Spec:
Expand Down Expand Up @@ -31,11 +36,32 @@ def test_failed(self, spec):
validate_spec(spec)


class BaseTestValidValidteV2SpecUrl:
class BaseTestValidValidateSpecUrl:

@pytest.fixture
def urllib_handlers(self):
all_urls_handler = UrllibHandler('http', 'https', 'file')
return {
'<all_urls>': all_urls_handler,
'http': UrllibHandler('http'),
'https': UrllibHandler('https'),
'file': UrllibHandler('file'),
}


class BaseTestValidValidateV2SpecUrl(BaseTestValidValidateSpecUrl):

@pytest.fixture
def validate_spec_url_callable(self, urllib_handlers):
return validate_spec_url_factory(
openapi_v2_spec_validator.validate, urllib_handlers)

def test_valid(self, spec_url):
validate_v2_spec_url(spec_url)

def test_urllib_valid(self, validate_spec_url_callable, spec_url):
validate_spec_url_callable(spec_url)


class BaseTestFaliedValidateV2SpecUrl:

Expand All @@ -44,11 +70,19 @@ def test_failed(self, spec_url):
validate_v2_spec_url(spec_url)


class BaseTestValidValidteSpecUrl:
class BaseTestValidValidateV3SpecUrl(BaseTestValidValidateSpecUrl):

def test_valid(self, spec_url):
@pytest.fixture
def validate_spec_url_callable(self, urllib_handlers):
return validate_spec_url_factory(
openapi_v3_spec_validator.validate, urllib_handlers)

def test_default_valid(self, spec_url):
validate_spec_url(spec_url)

def test_urllib_valid(self, validate_spec_url_callable, spec_url):
validate_spec_url_callable(spec_url)


class BaseTestFaliedValidateSpecUrl:

Expand Down Expand Up @@ -78,7 +112,7 @@ def spec(self, factory):
return factory.spec_from_file("data/v3.0/petstore.yaml")


class TestPetstoreV2Example(BaseTestValidValidteV2SpecUrl):
class TestPetstoreV2Example(BaseTestValidValidateV2SpecUrl):

@pytest.fixture
def spec_url(self):
Expand All @@ -89,7 +123,7 @@ def spec_url(self):
)


class TestApiV2WithExampe(BaseTestValidValidteV2SpecUrl):
class TestApiV2WithExampe(BaseTestValidValidateV2SpecUrl):

@pytest.fixture
def spec_url(self):
Expand All @@ -100,7 +134,7 @@ def spec_url(self):
)


class TestPetstoreV2ExpandedExample(BaseTestValidValidteV2SpecUrl):
class TestPetstoreV2ExpandedExample(BaseTestValidValidateV2SpecUrl):

@pytest.fixture
def spec_url(self):
Expand All @@ -111,7 +145,7 @@ def spec_url(self):
)


class TestPetstoreExample(BaseTestValidValidteSpecUrl):
class TestPetstoreExample(BaseTestValidValidateV3SpecUrl):

@pytest.fixture
def spec_url(self):
Expand All @@ -122,7 +156,7 @@ def spec_url(self):
)


class TestApiWithExampe(BaseTestValidValidteSpecUrl):
class TestApiWithExampe(BaseTestValidValidateV3SpecUrl):

@pytest.fixture
def spec_url(self):
Expand All @@ -133,7 +167,7 @@ def spec_url(self):
)


class TestPetstoreExpandedExample(BaseTestValidValidteSpecUrl):
class TestPetstoreExpandedExample(BaseTestValidValidateV3SpecUrl):

@pytest.fixture
def spec_url(self):
Expand Down