Skip to content

Commit

Permalink
Dataclasses refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
p1c2u committed May 29, 2021
1 parent e9e43eb commit 30d0f06
Show file tree
Hide file tree
Showing 29 changed files with 177 additions and 134 deletions.
8 changes: 4 additions & 4 deletions openapi_core/casting/schemas/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import attr
from dataclasses import dataclass

from openapi_core.exceptions import OpenAPIError


@attr.s(hash=True)
@dataclass
class CastError(OpenAPIError):
"""Schema cast operation error"""
value = attr.ib()
type = attr.ib()
value: str
type: str

def __str__(self):
return "Failed to cast value {value} to type {type}".format(
Expand Down
14 changes: 9 additions & 5 deletions openapi_core/contrib/django/requests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""OpenAPI core contrib django requests module"""
import re

from urllib.parse import urljoin

from werkzeug.datastructures import ImmutableMultiDict, Headers

from openapi_core.contrib.django.compat import (
get_request_headers, get_current_scheme_host,
)
Expand Down Expand Up @@ -43,13 +44,16 @@ def create(cls, request):
route = route[:-1]
path_pattern = '/' + route

request_headers = get_request_headers(request)
path = request.resolver_match and request.resolver_match.kwargs or {}
headers = get_request_headers(request)
query = ImmutableMultiDict(request.GET)
header = Headers(request_headers.items())
cookie = ImmutableMultiDict(dict(request.COOKIES))
parameters = RequestParameters(
path=path,
query=request.GET,
header=list(headers.items()),
cookie=request.COOKIES,
query=query,
header=header,
cookie=cookie,
)
current_scheme_host = get_current_scheme_host(request)
full_url_pattern = urljoin(current_scheme_host, path_pattern)
Expand Down
5 changes: 4 additions & 1 deletion openapi_core/contrib/django/responses.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""OpenAPI core contrib django responses module"""
from werkzeug.datastructures import Headers

from openapi_core.contrib.django.compat import get_response_headers
from openapi_core.validation.response.datatypes import OpenAPIResponse

Expand All @@ -9,9 +11,10 @@ class DjangoOpenAPIResponseFactory:
def create(cls, response):
mimetype = response["Content-Type"]
headers = get_response_headers(response)
header = Headers(headers.items())
return OpenAPIResponse(
data=response.content,
status_code=response.status_code,
headers=list(headers.items()),
headers=header,
mimetype=mimetype,
)
5 changes: 3 additions & 2 deletions openapi_core/contrib/falcon/requests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""OpenAPI core contrib falcon responses module"""
from json import dumps

from werkzeug.datastructures import ImmutableMultiDict
from werkzeug.datastructures import ImmutableMultiDict, Headers

from openapi_core.contrib.falcon.compat import get_request_media
from openapi_core.validation.request.datatypes import (
Expand Down Expand Up @@ -29,11 +29,12 @@ def create(cls, request, default_when_empty={}):
mimetype = request.content_type.partition(";")[0]

query = ImmutableMultiDict(list(request.params.items()))
header = Headers(request.headers)

# Path gets deduced by path finder against spec
parameters = RequestParameters(
query=query,
header=request.headers,
header=header,
cookie=request.cookies,
)
url_pattern = request.prefix + request.path
Expand Down
5 changes: 4 additions & 1 deletion openapi_core/contrib/falcon/responses.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""OpenAPI core contrib falcon responses module"""
from werkzeug.datastructures import Headers

from openapi_core.contrib.falcon.compat import get_response_text
from openapi_core.validation.response.datatypes import OpenAPIResponse

Expand All @@ -15,10 +17,11 @@ def create(cls, response):
mimetype = response.options.default_media_type

data = get_response_text(response)
headers = Headers(response.headers)

return OpenAPIResponse(
data=data,
status_code=status_code,
headers=response.headers,
headers=headers,
mimetype=mimetype,
)
6 changes: 4 additions & 2 deletions openapi_core/contrib/flask/requests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""OpenAPI core contrib flask requests module"""
import re

from urllib.parse import urljoin

from werkzeug.datastructures import Headers

from openapi_core.validation.request.datatypes import (
RequestParameters, OpenAPIRequest,
)
Expand All @@ -24,10 +25,11 @@ def create(cls, request):
else:
path_pattern = cls.path_regex.sub(r'{\1}', request.url_rule.rule)

header = Headers(request.headers)
parameters = RequestParameters(
path=request.view_args,
query=request.args,
header=request.headers,
header=header,
cookie=request.cookies,
)
full_url_pattern = urljoin(request.host_url, path_pattern)
Expand Down
5 changes: 4 additions & 1 deletion openapi_core/contrib/flask/responses.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
"""OpenAPI core contrib flask responses module"""
from werkzeug.datastructures import Headers

from openapi_core.validation.response.datatypes import OpenAPIResponse


class FlaskOpenAPIResponseFactory:

@classmethod
def create(cls, response):
header = Headers(response.headers)
return OpenAPIResponse(
data=response.data,
status_code=response._status_code,
headers=response.headers,
headers=header,
mimetype=response.mimetype,
)
6 changes: 3 additions & 3 deletions openapi_core/contrib/requests/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from urllib.parse import urlparse, parse_qs

from werkzeug.datastructures import ImmutableMultiDict
from werkzeug.datastructures import ImmutableMultiDict, Headers
from requests import Request

from openapi_core.validation.request.datatypes import (
Expand Down Expand Up @@ -43,9 +43,9 @@ def create(cls, request):
mimetype = request.headers.get('Content-Type') or \
request.headers.get('Accept')

# Headers - request.headers is not an instance of dict
# Headers - request.headers is not an instance of Headers
# which is expected
header = dict(request.headers)
header = Headers(dict(request.headers))

# Body
# TODO: figure out if request._body_position is relevant
Expand Down
4 changes: 3 additions & 1 deletion openapi_core/contrib/requests/responses.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""OpenAPI core contrib requests responses module"""
from werkzeug.datastructures import Headers

from openapi_core.validation.response.datatypes import OpenAPIResponse


Expand All @@ -7,7 +9,7 @@ class RequestsOpenAPIResponseFactory:
@classmethod
def create(cls, response):
mimetype = response.headers.get('Content-Type')
headers = dict(response.headers)
headers = Headers(dict(response.headers))
return OpenAPIResponse(
data=response.content,
status_code=response.status_code,
Expand Down
8 changes: 4 additions & 4 deletions openapi_core/deserializing/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import attr
from dataclasses import dataclass

from openapi_core.exceptions import OpenAPIError


@attr.s(hash=True)
@dataclass
class DeserializeError(OpenAPIError):
"""Deserialize operation error"""
value = attr.ib()
style = attr.ib()
value: str
style: str

def __str__(self):
return "Failed to deserialize value {value} with style {style}".format(
Expand Down
6 changes: 3 additions & 3 deletions openapi_core/deserializing/parameters/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import attr
from dataclasses import dataclass

from openapi_core.deserializing.exceptions import DeserializeError


@attr.s(hash=True)
@dataclass
class EmptyParameterValue(DeserializeError):
name = attr.ib()
name: str

def __str__(self):
return "Value of parameter cannot be empty: {0}".format(self.name)
33 changes: 18 additions & 15 deletions openapi_core/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""OpenAPI core exceptions module"""
import attr
from dataclasses import dataclass

from openapi_core.validation.request.datatypes import OpenAPIRequest
from openapi_core.validation.response.datatypes import OpenAPIResponse


class OpenAPIError(Exception):
Expand All @@ -15,18 +18,18 @@ class MissingHeaderError(OpenAPIHeaderError):
pass


@attr.s(hash=True)
@dataclass
class MissingHeader(MissingHeaderError):
name = attr.ib()
name: str

def __str__(self):
return "Missing header (without default value): {0}".format(
self.name)


@attr.s(hash=True)
@dataclass
class MissingRequiredHeader(MissingHeaderError):
name = attr.ib()
name: str

def __str__(self):
return "Missing required header: {0}".format(self.name)
Expand All @@ -41,18 +44,18 @@ class MissingParameterError(OpenAPIParameterError):
pass


@attr.s(hash=True)
@dataclass
class MissingParameter(MissingParameterError):
name = attr.ib()
name: str

def __str__(self):
return "Missing parameter (without default value): {0}".format(
self.name)


@attr.s(hash=True)
@dataclass
class MissingRequiredParameter(MissingParameterError):
name = attr.ib()
name: str

def __str__(self):
return "Missing required parameter: {0}".format(self.name)
Expand All @@ -67,17 +70,17 @@ class MissingRequestBodyError(OpenAPIRequestBodyError):
pass


@attr.s(hash=True)
@dataclass
class MissingRequestBody(MissingRequestBodyError):
request = attr.ib()
request: OpenAPIRequest

def __str__(self):
return "Missing request body"


@attr.s(hash=True)
@dataclass
class MissingRequiredRequestBody(MissingRequestBodyError):
request = attr.ib()
request: OpenAPIRequest

def __str__(self):
return "Missing required request body"
Expand All @@ -87,9 +90,9 @@ class OpenAPIResponseError(OpenAPIError):
pass


@attr.s(hash=True)
@dataclass
class MissingResponseContent(OpenAPIResponseError):
response = attr.ib()
response: OpenAPIResponse

def __str__(self):
return "Missing response content"
10 changes: 6 additions & 4 deletions openapi_core/templating/datatypes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import attr
from typing import Dict, Optional

from dataclasses import dataclass

@attr.s

@dataclass
class TemplateResult:
pattern = attr.ib(default=None)
variables = attr.ib(default=None)
pattern: Optional[str] = None
variables: Optional[Dict] = None

@property
def resolved(self):
Expand Down
10 changes: 6 additions & 4 deletions openapi_core/templating/media_types/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import attr
from typing import List

from dataclasses import dataclass

from openapi_core.exceptions import OpenAPIError

Expand All @@ -7,10 +9,10 @@ class MediaTypeFinderError(OpenAPIError):
"""Media type finder error"""


@attr.s(hash=True)
@dataclass
class MediaTypeNotFound(MediaTypeFinderError):
mimetype = attr.ib()
availableMimetypes = attr.ib()
mimetype: str
availableMimetypes: List[str]

def __str__(self):
return (
Expand Down
16 changes: 8 additions & 8 deletions openapi_core/templating/paths/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import attr
from dataclasses import dataclass

from openapi_core.exceptions import OpenAPIError

Expand All @@ -7,30 +7,30 @@ class PathError(OpenAPIError):
"""Path error"""


@attr.s(hash=True)
@dataclass
class PathNotFound(PathError):
"""Find path error"""
url = attr.ib()
url: str

def __str__(self):
return "Path not found for {0}".format(self.url)


@attr.s(hash=True)
@dataclass
class OperationNotFound(PathError):
"""Find path operation error"""
url = attr.ib()
method = attr.ib()
url: str
method: str

def __str__(self):
return "Operation {0} not found for {1}".format(
self.method, self.url)


@attr.s(hash=True)
@dataclass
class ServerNotFound(PathError):
"""Find server error"""
url = attr.ib()
url: str

def __str__(self):
return "Server not found for {0}".format(self.url)
Loading

0 comments on commit 30d0f06

Please sign in to comment.