Implement task API with input validation; implement error handling; refactoring
This commit is contained in:
parent
19d264a03b
commit
50aff05614
24 changed files with 612 additions and 131 deletions
32
tofu_api/common/rest/base_method_view.py
Normal file
32
tofu_api/common/rest/base_method_view.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue