mirror of
https://github.com/apache/superset.git
synced 2026-04-19 16:14:52 +00:00
refactor: extract json_required view decorator (#18170)
* refactor: extract json_required view decorator * chore: rename json_required to requires_json * refactor: add requires_form_data decorator and use it * fix: fix lint issue, raise InvalidPayloadFormatError for invalid payload
This commit is contained in:
@@ -68,12 +68,16 @@ from superset.databases.schemas import (
|
||||
from superset.databases.utils import get_table_metadata
|
||||
from superset.db_engine_specs import get_available_engine_specs
|
||||
from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
|
||||
from superset.exceptions import InvalidPayloadFormatError
|
||||
from superset.extensions import security_manager
|
||||
from superset.models.core import Database
|
||||
from superset.typing import FlaskResponse
|
||||
from superset.utils.core import error_msg_from_exception
|
||||
from superset.views.base_api import BaseSupersetModelRestApi, statsd_metrics
|
||||
from superset.views.base_api import (
|
||||
BaseSupersetModelRestApi,
|
||||
requires_form_data,
|
||||
requires_json,
|
||||
statsd_metrics,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -201,6 +205,7 @@ class DatabaseRestApi(BaseSupersetModelRestApi):
|
||||
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.post",
|
||||
log_to_statsd=False,
|
||||
)
|
||||
@requires_json
|
||||
def post(self) -> Response:
|
||||
"""Creates a new Database
|
||||
---
|
||||
@@ -237,9 +242,6 @@ class DatabaseRestApi(BaseSupersetModelRestApi):
|
||||
500:
|
||||
$ref: '#/components/responses/500'
|
||||
"""
|
||||
|
||||
if not request.is_json:
|
||||
return self.response_400(message="Request is not JSON")
|
||||
try:
|
||||
item = self.add_model_schema.load(request.json)
|
||||
# This validates custom Schema with custom validations
|
||||
@@ -277,6 +279,7 @@ class DatabaseRestApi(BaseSupersetModelRestApi):
|
||||
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.put",
|
||||
log_to_statsd=False,
|
||||
)
|
||||
@requires_json
|
||||
def put(self, pk: int) -> Response:
|
||||
"""Changes a Database
|
||||
---
|
||||
@@ -320,8 +323,6 @@ class DatabaseRestApi(BaseSupersetModelRestApi):
|
||||
500:
|
||||
$ref: '#/components/responses/500'
|
||||
"""
|
||||
if not request.is_json:
|
||||
return self.response_400(message="Request is not JSON")
|
||||
try:
|
||||
item = self.edit_model_schema.load(request.json)
|
||||
# This validates custom Schema with custom validations
|
||||
@@ -593,6 +594,7 @@ class DatabaseRestApi(BaseSupersetModelRestApi):
|
||||
f".test_connection",
|
||||
log_to_statsd=False,
|
||||
)
|
||||
@requires_json
|
||||
def test_connection(self) -> FlaskResponse:
|
||||
"""Tests a database connection
|
||||
---
|
||||
@@ -623,8 +625,6 @@ class DatabaseRestApi(BaseSupersetModelRestApi):
|
||||
500:
|
||||
$ref: '#/components/responses/500'
|
||||
"""
|
||||
if not request.is_json:
|
||||
return self.response_400(message="Request is not JSON")
|
||||
try:
|
||||
item = DatabaseTestConnectionSchema().load(request.json)
|
||||
# This validates custom Schema with custom validations
|
||||
@@ -774,6 +774,7 @@ class DatabaseRestApi(BaseSupersetModelRestApi):
|
||||
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.import_",
|
||||
log_to_statsd=False,
|
||||
)
|
||||
@requires_form_data
|
||||
def import_(self) -> Response:
|
||||
"""Import database(s) with associated datasets
|
||||
---
|
||||
@@ -985,6 +986,7 @@ class DatabaseRestApi(BaseSupersetModelRestApi):
|
||||
f".validate_parameters",
|
||||
log_to_statsd=False,
|
||||
)
|
||||
@requires_json
|
||||
def validate_parameters(self) -> FlaskResponse:
|
||||
"""validates database connection parameters
|
||||
---
|
||||
@@ -1015,9 +1017,6 @@ class DatabaseRestApi(BaseSupersetModelRestApi):
|
||||
500:
|
||||
$ref: '#/components/responses/500'
|
||||
"""
|
||||
if not request.is_json:
|
||||
raise InvalidPayloadFormatError("Request is not JSON")
|
||||
|
||||
try:
|
||||
payload = DatabaseValidateParametersSchema().load(request.json)
|
||||
except ValidationError as ex:
|
||||
|
||||
Reference in New Issue
Block a user