33 lines
927 B
Python
33 lines
927 B
Python
from typing import Any
|
|
|
|
import flask
|
|
from flask.typing import RouteCallable
|
|
from flask.views import MethodView
|
|
from validataclass.validators import Validator
|
|
|
|
|
|
class BaseMethodView(MethodView):
|
|
"""
|
|
Base class for REST API views.
|
|
"""
|
|
|
|
@property
|
|
def request(self) -> flask.Request:
|
|
return flask.request
|
|
|
|
@classmethod
|
|
def as_view(cls, *args, **kwargs) -> RouteCallable:
|
|
return super().as_view(cls.__name__, *args, **kwargs)
|
|
|
|
@staticmethod
|
|
def empty_response(code: int = 204) -> tuple[str, int]:
|
|
return '', code
|
|
|
|
def validate_request_data(self, validator: Validator) -> Any:
|
|
"""
|
|
Parses request data as JSON and validates it using a validataclass validator.
|
|
"""
|
|
# TODO error handling: wrong content type; empty body; invalid json; validation errors
|
|
parsed_json = self.request.json
|
|
return validator.validate(parsed_json)
|