mirror of
https://github.com/apache/superset.git
synced 2026-04-20 00:24:38 +00:00
chore: Migrate get_or_create_table endpoint to api v1 (#22931)
This commit is contained in:
@@ -61,6 +61,7 @@ from superset.datasets.schemas import (
|
||||
DatasetRelatedObjectsResponse,
|
||||
get_delete_ids_schema,
|
||||
get_export_ids_schema,
|
||||
GetOrCreateDatasetSchema,
|
||||
)
|
||||
from superset.utils.core import parse_boolean_string
|
||||
from superset.views.base import DatasourceFilter, generate_download_headers
|
||||
@@ -93,6 +94,7 @@ class DatasetRestApi(BaseSupersetModelRestApi):
|
||||
"refresh",
|
||||
"related_objects",
|
||||
"duplicate",
|
||||
"get_or_create_dataset",
|
||||
}
|
||||
list_columns = [
|
||||
"id",
|
||||
@@ -240,6 +242,7 @@ class DatasetRestApi(BaseSupersetModelRestApi):
|
||||
openapi_spec_component_schemas = (
|
||||
DatasetRelatedObjectsResponse,
|
||||
DatasetDuplicateSchema,
|
||||
GetOrCreateDatasetSchema,
|
||||
)
|
||||
|
||||
list_outer_default_load = True
|
||||
@@ -877,3 +880,70 @@ class DatasetRestApi(BaseSupersetModelRestApi):
|
||||
)
|
||||
command.run()
|
||||
return self.response(200, message="OK")
|
||||
|
||||
@expose("/get_or_create/", methods=["POST"])
|
||||
@protect()
|
||||
@safe
|
||||
@statsd_metrics
|
||||
@event_logger.log_this_with_context(
|
||||
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}"
|
||||
f".get_or_create_dataset",
|
||||
log_to_statsd=False,
|
||||
)
|
||||
def get_or_create_dataset(self) -> Response:
|
||||
"""Retrieve a dataset by name, or create it if it does not exist
|
||||
---
|
||||
post:
|
||||
summary: Retrieve a table by name, or create it if it does not exist
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/GetOrCreateDatasetSchema'
|
||||
responses:
|
||||
200:
|
||||
description: The ID of the table
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
result:
|
||||
type: object
|
||||
properties:
|
||||
table_id:
|
||||
type: integer
|
||||
400:
|
||||
$ref: '#/components/responses/400'
|
||||
401:
|
||||
$ref: '#/components/responses/401'
|
||||
422:
|
||||
$ref: '#/components/responses/422'
|
||||
500:
|
||||
$ref: '#/components/responses/500'
|
||||
"""
|
||||
try:
|
||||
body = GetOrCreateDatasetSchema().load(request.json)
|
||||
except ValidationError as ex:
|
||||
return self.response(400, message=ex.messages)
|
||||
table_name = body["table_name"]
|
||||
database_id = body["database_id"]
|
||||
table = DatasetDAO.get_table_by_name(database_id, table_name)
|
||||
if table:
|
||||
return self.response(200, result={"table_id": table.id})
|
||||
|
||||
body["database"] = database_id
|
||||
try:
|
||||
tbl = CreateDatasetCommand(body).run()
|
||||
return self.response(200, result={"table_id": tbl.id})
|
||||
except DatasetInvalidError as ex:
|
||||
return self.response_422(message=ex.normalized_messages())
|
||||
except DatasetCreateFailedError as ex:
|
||||
logger.error(
|
||||
"Error creating model %s: %s",
|
||||
self.__class__.__name__,
|
||||
str(ex),
|
||||
exc_info=True,
|
||||
)
|
||||
return self.response_422(message=ex.message)
|
||||
|
||||
Reference in New Issue
Block a user