From 9b05d7b27109509c35fbc64a9a046c3598b158be Mon Sep 17 00:00:00 2001 From: Artur Maciag Date: Tue, 17 Apr 2018 15:37:52 +0100 Subject: [PATCH] Wrapper restructure --- openapi_core/wrappers.py | 142 --------------------------- openapi_core/wrappers/__init__.py | 0 openapi_core/wrappers/base.py | 49 +++++++++ openapi_core/wrappers/flask.py | 62 ++++++++++++ openapi_core/wrappers/mock.py | 36 +++++++ tests/integration/test_minimal.py | 2 +- tests/integration/test_petstore.py | 2 +- tests/integration/test_validators.py | 2 +- tests/integration/test_wrappers.py | 4 +- 9 files changed, 153 insertions(+), 146 deletions(-) delete mode 100644 openapi_core/wrappers.py create mode 100644 openapi_core/wrappers/__init__.py create mode 100644 openapi_core/wrappers/base.py create mode 100644 openapi_core/wrappers/flask.py create mode 100644 openapi_core/wrappers/mock.py diff --git a/openapi_core/wrappers.py b/openapi_core/wrappers.py deleted file mode 100644 index 019e242..0000000 --- a/openapi_core/wrappers.py +++ /dev/null @@ -1,142 +0,0 @@ -"""OpenAPI core wrappers module""" -import warnings - -from six.moves.urllib.parse import urljoin -from werkzeug.datastructures import ImmutableMultiDict - - -class BaseOpenAPIRequest(object): - - host_url = NotImplemented - path = NotImplemented - path_pattern = NotImplemented - method = NotImplemented - - parameters = NotImplemented - body = NotImplemented - - mimetype = NotImplemented - - @property - def full_url_pattern(self): - return urljoin(self.host_url, self.path_pattern) - - def get_body(self, spec): - warnings.warn( - "`get_body` method is deprecated. " - "Use RequestValidator instead.", - DeprecationWarning, - ) - # backward compatibility - from openapi_core.shortcuts import validate_body - return validate_body(spec, self, wrapper_class=None) - - def get_parameters(self, spec): - warnings.warn( - "`get_parameters` method is deprecated. " - "Use RequestValidator instead.", - DeprecationWarning, - ) - # backward compatibility - from openapi_core.shortcuts import validate_parameters - return validate_parameters(spec, self, wrapper_class=None) - - -class MockRequest(BaseOpenAPIRequest): - - def __init__( - self, host_url, method, path, path_pattern=None, args=None, - view_args=None, headers=None, cookies=None, data=None, - mimetype='application/json'): - self.host_url = host_url - self.path = path - self.path_pattern = path_pattern or path - self.method = method.lower() - - self.parameters = { - 'path': view_args or {}, - 'query': ImmutableMultiDict(args or []), - 'header': headers or {}, - 'cookie': cookies or {}, - } - - self.body = data or '' - - self.mimetype = mimetype - - -class FlaskOpenAPIRequest(BaseOpenAPIRequest): - - def __init__(self, request): - self.request = request - - @property - def host_url(self): - return self.request.host_url - - @property - def path(self): - return self.request.path - - @property - def method(self): - return self.request.method.lower() - - @property - def path_pattern(self): - if self.request.url_rule is None: - return self.path - - return self.request.url_rule.rule - - @property - def parameters(self): - return { - 'path': self.request.view_args, - 'query': self.request.args, - 'headers': self.request.headers, - 'cookies': self.request.cookies, - } - - @property - def body(self): - return self.request.data - - @property - def mimetype(self): - return self.request.mimetype - - -class BaseOpenAPIResponse(object): - - body = NotImplemented - status_code = NotImplemented - - mimetype = NotImplemented - - -class MockResponse(BaseOpenAPIRequest): - - def __init__(self, data, status_code=200, mimetype='application/json'): - self.data = data - - self.status_code = status_code - self.mimetype = mimetype - - -class FlaskOpenAPIResponse(BaseOpenAPIResponse): - - def __init__(self, response): - self.response = response - - @property - def data(self): - return self.response.data - - @property - def status_code(self): - return self.response._status_code - - @property - def mimetype(self): - return self.response.mimetype diff --git a/openapi_core/wrappers/__init__.py b/openapi_core/wrappers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/openapi_core/wrappers/base.py b/openapi_core/wrappers/base.py new file mode 100644 index 0000000..395d16e --- /dev/null +++ b/openapi_core/wrappers/base.py @@ -0,0 +1,49 @@ +"""OpenAPI core wrappers module""" +import warnings + +from six.moves.urllib.parse import urljoin + + +class BaseOpenAPIRequest(object): + + host_url = NotImplemented + path = NotImplemented + path_pattern = NotImplemented + method = NotImplemented + + parameters = NotImplemented + body = NotImplemented + + mimetype = NotImplemented + + @property + def full_url_pattern(self): + return urljoin(self.host_url, self.path_pattern) + + def get_body(self, spec): + warnings.warn( + "`get_body` method is deprecated. " + "Use RequestValidator instead.", + DeprecationWarning, + ) + # backward compatibility + from openapi_core.shortcuts import validate_body + return validate_body(spec, self, wrapper_class=None) + + def get_parameters(self, spec): + warnings.warn( + "`get_parameters` method is deprecated. " + "Use RequestValidator instead.", + DeprecationWarning, + ) + # backward compatibility + from openapi_core.shortcuts import validate_parameters + return validate_parameters(spec, self, wrapper_class=None) + + +class BaseOpenAPIResponse(object): + + body = NotImplemented + status_code = NotImplemented + + mimetype = NotImplemented diff --git a/openapi_core/wrappers/flask.py b/openapi_core/wrappers/flask.py new file mode 100644 index 0000000..84bf093 --- /dev/null +++ b/openapi_core/wrappers/flask.py @@ -0,0 +1,62 @@ +"""OpenAPI core wrappers module""" +from openapi_core.wrappers.base import BaseOpenAPIRequest, BaseOpenAPIResponse + + +class FlaskOpenAPIRequest(BaseOpenAPIRequest): + + def __init__(self, request): + self.request = request + + @property + def host_url(self): + return self.request.host_url + + @property + def path(self): + return self.request.path + + @property + def method(self): + return self.request.method.lower() + + @property + def path_pattern(self): + if self.request.url_rule is None: + return self.path + + return self.request.url_rule.rule + + @property + def parameters(self): + return { + 'path': self.request.view_args, + 'query': self.request.args, + 'headers': self.request.headers, + 'cookies': self.request.cookies, + } + + @property + def body(self): + return self.request.data + + @property + def mimetype(self): + return self.request.mimetype + + +class FlaskOpenAPIResponse(BaseOpenAPIResponse): + + def __init__(self, response): + self.response = response + + @property + def data(self): + return self.response.data + + @property + def status_code(self): + return self.response._status_code + + @property + def mimetype(self): + return self.response.mimetype diff --git a/openapi_core/wrappers/mock.py b/openapi_core/wrappers/mock.py new file mode 100644 index 0000000..9b03429 --- /dev/null +++ b/openapi_core/wrappers/mock.py @@ -0,0 +1,36 @@ +"""OpenAPI core wrappers module""" +from werkzeug.datastructures import ImmutableMultiDict + +from openapi_core.wrappers.base import BaseOpenAPIRequest, BaseOpenAPIResponse + + +class MockRequest(BaseOpenAPIRequest): + + def __init__( + self, host_url, method, path, path_pattern=None, args=None, + view_args=None, headers=None, cookies=None, data=None, + mimetype='application/json'): + self.host_url = host_url + self.path = path + self.path_pattern = path_pattern or path + self.method = method.lower() + + self.parameters = { + 'path': view_args or {}, + 'query': ImmutableMultiDict(args or []), + 'header': headers or {}, + 'cookie': cookies or {}, + } + + self.body = data or '' + + self.mimetype = mimetype + + +class MockResponse(BaseOpenAPIResponse): + + def __init__(self, data, status_code=200, mimetype='application/json'): + self.data = data + + self.status_code = status_code + self.mimetype = mimetype diff --git a/tests/integration/test_minimal.py b/tests/integration/test_minimal.py index 28806ab..7f88fb4 100644 --- a/tests/integration/test_minimal.py +++ b/tests/integration/test_minimal.py @@ -3,7 +3,7 @@ import pytest from openapi_core.exceptions import InvalidOperation from openapi_core.shortcuts import create_spec from openapi_core.validation.request.validators import RequestValidator -from openapi_core.wrappers import MockRequest +from openapi_core.wrappers.mock import MockRequest class TestMinimal(object): diff --git a/tests/integration/test_petstore.py b/tests/integration/test_petstore.py index f0898d4..844ada4 100644 --- a/tests/integration/test_petstore.py +++ b/tests/integration/test_petstore.py @@ -18,7 +18,7 @@ from openapi_core.schema.servers.models import Server, ServerVariable from openapi_core.shortcuts import create_spec from openapi_core.validation.request.validators import RequestValidator from openapi_core.validation.response.validators import ResponseValidator -from openapi_core.wrappers import MockRequest, MockResponse +from openapi_core.wrappers.mock import MockRequest, MockResponse class TestPetstore(object): diff --git a/tests/integration/test_validators.py b/tests/integration/test_validators.py index fd3d7d3..5d5b697 100644 --- a/tests/integration/test_validators.py +++ b/tests/integration/test_validators.py @@ -9,7 +9,7 @@ from openapi_core.exceptions import ( from openapi_core.shortcuts import create_spec from openapi_core.validation.request.validators import RequestValidator from openapi_core.validation.response.validators import ResponseValidator -from openapi_core.wrappers import MockRequest, MockResponse +from openapi_core.wrappers.mock import MockRequest, MockResponse class TestRequestValidator(object): diff --git a/tests/integration/test_wrappers.py b/tests/integration/test_wrappers.py index b33505e..29a357d 100644 --- a/tests/integration/test_wrappers.py +++ b/tests/integration/test_wrappers.py @@ -5,7 +5,9 @@ from werkzeug.datastructures import EnvironHeaders, ImmutableMultiDict from werkzeug.routing import Map, Rule, Subdomain from werkzeug.test import create_environ -from openapi_core.wrappers import FlaskOpenAPIRequest, FlaskOpenAPIResponse +from openapi_core.wrappers.flask import ( + FlaskOpenAPIRequest, FlaskOpenAPIResponse, +) class TestFlaskOpenAPIRequest(object):