Vitor-Avila commented on code in PR #30721:
URL: https://github.com/apache/superset/pull/30721#discussion_r1817218701
##########
superset/datasets/api.py:
##########
@@ -1056,3 +1062,123 @@ def warm_up_cache(self) -> Response:
return self.response(200, result=result)
except CommandException as ex:
return self.response(ex.status, message=ex.message)
+
+ @expose("/<int:pk>", methods=("GET",))
+ @protect()
+ @safe
+ @rison(get_item_schema)
+ @statsd_metrics
+ @event_logger.log_this_with_context(
+ action=lambda self, *args, **kwargs: f"{self.__class__.__name__}"
f".get",
+ log_to_statsd=False,
+ )
+ def get(self, pk: int, **kwargs: Any) -> Response:
+ """Get a dataset.
+ ---
+ get:
+ summary: Get a dataset
+ description: Get a dataset by ID
+ parameters:
+ - in: path
+ schema:
+ type: integer
+ description: The dataset ID
+ name: pk
+ - in: query
+ name: q
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/get_item_schema'
+ - in: query
+ name: render
+ description: Should Jinja macros from sql, metrics and columns be
rendered
+ schema:
+ type: boolean
+ responses:
+ 200:
+ description: Dataset object has been returned.
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ id:
+ description: The item id
+ type: string
+ result:
+ $ref:
'#/components/schemas/{{self.__class__.__name__}}.get'
+ 400:
+ $ref: '#/components/responses/400'
+ 401:
+ $ref: '#/components/responses/401'
+ 422:
+ $ref: '#/components/responses/422'
+ 500:
+ $ref: '#/components/responses/500'
+ """
+ item: Optional[SqlaTable] = self.datamodel.get(
+ pk,
+ self._base_filters,
+ self.show_select_columns,
+ self.show_outer_default_load,
+ )
+ if not item:
+ return self.response_404()
+
+ response: dict[str, Any] = {}
+ args = kwargs.get("rison", {})
+ select_cols = args.get(API_SELECT_COLUMNS_RIS_KEY, [])
+ pruned_select_cols = [col for col in select_cols if col in
self.show_columns]
+ self.set_response_key_mappings(
+ response,
+ self.get,
+ args,
+ **{API_SELECT_COLUMNS_RIS_KEY: pruned_select_cols},
+ )
+ if pruned_select_cols:
+ show_model_schema =
self.model2schemaconverter.convert(pruned_select_cols)
+ else:
+ show_model_schema = self.show_model_schema
+
+ response["id"] = pk
+ response[API_RESULT_RES_KEY] = show_model_schema.dump(item, many=False)
Review Comment:
This is FAB's default implementation for `get_headless` -
[ref](https://github.com/dpgaspar/Flask-AppBuilder/blob/master/flask_appbuilder/api/__init__.py#L1453-L1485).
I decided to not simply do `self.get_headless()` and then modify the
response for two reasons:
* `get_headless` returns a response object, so while we could just modify
the `response.json` it feels a little hacky.
* Superset's `get_headless` implementation includes a call to
`send_stast_metrics`
([ref](https://github.com/apache/superset/blob/master/superset/views/base_api.py#L450-L451)),
which wouldn't include the full duration of the API call as the response would
be further modified by this method before concluding.
I also thought about just overwriting FAB's `pre_get` method which is
available in case the response needs to be modified as part of `get_headless`,
but we need the `Database` associated with this dataset, and if the user
chooses to not include the `database.id` in the response then `pre_get`
wouldn't have this info.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]