dpgaspar commented on code in PR #20399:
URL: https://github.com/apache/superset/pull/20399#discussion_r902603773


##########
superset/explore/api.py:
##########
@@ -0,0 +1,126 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import logging
+
+from flask import g, request, Response
+from flask_appbuilder.api import BaseApi, expose, protect, safe
+
+from superset.charts.commands.exceptions import ChartNotFoundError
+from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP, RouteMethod
+from superset.explore.commands.get import GetExploreCommand
+from superset.explore.commands.parameters import CommandParameters
+from superset.explore.exceptions import DatasetAccessDeniedError, 
WrongEndpointError
+from superset.explore.permalink.exceptions import 
ExplorePermalinkGetFailedError
+from superset.explore.schemas import ExploreContextSchema
+from superset.extensions import event_logger
+from superset.temporary_cache.commands.exceptions import (
+    TemporaryCacheAccessDeniedError,
+    TemporaryCacheResourceNotFoundError,
+)
+
+logger = logging.getLogger(__name__)
+
+
+class ExploreRestApi(BaseApi):
+    method_permission_name = MODEL_API_RW_METHOD_PERMISSION_MAP
+    include_route_methods = {RouteMethod.GET}
+    allow_browser_login = True
+    class_permission_name = "ExploreRestApi"
+    resource_name = "explore"
+    openapi_spec_tag = "Explore"
+    openapi_spec_component_schemas = (ExploreContextSchema,)
+
+    @expose("/", methods=["GET"])
+    @protect()
+    @safe
+    @event_logger.log_this_with_context(
+        action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.get",
+        log_to_statsd=True,
+    )
+    def get(self) -> Response:
+        """Retrives Explore initial context.
+        ---
+        get:
+          description: >-
+            Retrives Explore initial context.
+          parameters:
+          - in: query
+            schema:
+              type: string
+            name: form_data_key
+          - in: query
+            schema:
+              type: string
+            name: permalink_key
+          - in: query
+            schema:
+              type: integer
+            name: slice_id
+          - in: query
+            schema:
+              type: integer
+            name: dataset_id
+          - in: query
+            schema:
+              type: string
+            name: dataset_type
+          responses:
+            200:
+              description: Returns the initial context.
+              content:
+                application/json:
+                  schema:
+                    $ref: '#/components/schemas/ExploreContextSchema'
+            400:
+              $ref: '#/components/responses/400'
+            401:
+              $ref: '#/components/responses/401'
+            404:
+              $ref: '#/components/responses/404'
+            422:
+              $ref: '#/components/responses/422'
+            500:
+              $ref: '#/components/responses/500'
+        """
+        try:
+            params = CommandParameters(
+                actor=g.user,
+                permalink_key=request.args.get("permalink_key"),
+                form_data_key=request.args.get("form_data_key"),
+                dataset_id=request.args.get("dataset_id"),

Review Comment:
   `dataset_id` can be a string, is it validated?



##########
superset/explore/commands/get.py:
##########
@@ -0,0 +1,172 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import logging
+from abc import ABC
+from typing import Any, cast, Dict, Optional
+
+import simplejson as json
+from flask import current_app as app
+from flask_babel import gettext as __, lazy_gettext as _
+from sqlalchemy.exc import SQLAlchemyError
+
+from superset import db, security_manager
+from superset.commands.base import BaseCommand
+from superset.connectors.base.models import BaseDatasource
+from superset.connectors.connector_registry import ConnectorRegistry
+from superset.connectors.sqla.models import SqlaTable
+from superset.datasets.commands.exceptions import DatasetNotFoundError
+from superset.exceptions import SupersetException
+from superset.explore.commands.parameters import CommandParameters
+from superset.explore.exceptions import DatasetAccessDeniedError, 
WrongEndpointError
+from superset.explore.form_data.commands.get import GetFormDataCommand
+from superset.explore.form_data.commands.parameters import (
+    CommandParameters as FormDataCommandParameters,
+)
+from superset.explore.permalink.commands.get import GetExplorePermalinkCommand
+from superset.explore.permalink.exceptions import 
ExplorePermalinkGetFailedError
+from superset.models.sql_lab import Query
+from superset.utils import core as utils
+from superset.views.utils import (
+    get_datasource_info,
+    get_form_data,
+    sanitize_datasource_data,
+)
+
+logger = logging.getLogger(__name__)
+
+
+class GetExploreCommand(BaseCommand, ABC):
+    def __init__(
+        self,
+        params: CommandParameters,
+    ) -> None:
+        self._actor = params.actor
+        self._permalink_key = params.permalink_key
+        self._form_data_key = params.form_data_key
+        self._dataset_id = params.dataset_id
+        self._dataset_type = params.dataset_type
+        self._slice_id = params.slice_id
+
+    # pylint: disable=too-many-locals,too-many-branches,too-many-statements
+    def run(self) -> Optional[Dict[str, Any]]:
+        initial_form_data = {}
+
+        if self._permalink_key is not None:
+            command = GetExplorePermalinkCommand(self._actor, 
self._permalink_key)
+            permalink_value = command.run()
+            if permalink_value:
+                state = permalink_value["state"]
+                initial_form_data = state["formData"]
+                url_params = state.get("urlParams")
+                if url_params:
+                    initial_form_data["url_params"] = dict(url_params)
+            else:
+                raise ExplorePermalinkGetFailedError()
+        elif self._form_data_key:
+            parameters = FormDataCommandParameters(
+                actor=self._actor, key=self._form_data_key
+            )
+            value = GetFormDataCommand(parameters).run()
+            initial_form_data = json.loads(value) if value else {}
+
+        message = None

Review Comment:
   I agree with @zhaoyongjie



##########
superset/explore/api.py:
##########
@@ -0,0 +1,126 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import logging
+
+from flask import g, request, Response
+from flask_appbuilder.api import BaseApi, expose, protect, safe
+
+from superset.charts.commands.exceptions import ChartNotFoundError
+from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP, RouteMethod
+from superset.explore.commands.get import GetExploreCommand
+from superset.explore.commands.parameters import CommandParameters
+from superset.explore.exceptions import DatasetAccessDeniedError, 
WrongEndpointError
+from superset.explore.permalink.exceptions import 
ExplorePermalinkGetFailedError
+from superset.explore.schemas import ExploreContextSchema
+from superset.extensions import event_logger
+from superset.temporary_cache.commands.exceptions import (
+    TemporaryCacheAccessDeniedError,
+    TemporaryCacheResourceNotFoundError,
+)
+
+logger = logging.getLogger(__name__)
+
+
+class ExploreRestApi(BaseApi):
+    method_permission_name = MODEL_API_RW_METHOD_PERMISSION_MAP
+    include_route_methods = {RouteMethod.GET}
+    allow_browser_login = True
+    class_permission_name = "ExploreRestApi"

Review Comment:
   We were getting out of the suffix `..RestApi`, that's why we were overriding 
the default permission class name using `class_permission_name` on this case 
the default is exactly this. Can you set this to `Explore`?



##########
superset/explore/api.py:
##########
@@ -0,0 +1,126 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import logging
+
+from flask import g, request, Response
+from flask_appbuilder.api import BaseApi, expose, protect, safe
+
+from superset.charts.commands.exceptions import ChartNotFoundError
+from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP, RouteMethod
+from superset.explore.commands.get import GetExploreCommand
+from superset.explore.commands.parameters import CommandParameters
+from superset.explore.exceptions import DatasetAccessDeniedError, 
WrongEndpointError
+from superset.explore.permalink.exceptions import 
ExplorePermalinkGetFailedError
+from superset.explore.schemas import ExploreContextSchema
+from superset.extensions import event_logger
+from superset.temporary_cache.commands.exceptions import (
+    TemporaryCacheAccessDeniedError,
+    TemporaryCacheResourceNotFoundError,
+)
+
+logger = logging.getLogger(__name__)
+
+
+class ExploreRestApi(BaseApi):
+    method_permission_name = MODEL_API_RW_METHOD_PERMISSION_MAP
+    include_route_methods = {RouteMethod.GET}
+    allow_browser_login = True
+    class_permission_name = "ExploreRestApi"
+    resource_name = "explore"
+    openapi_spec_tag = "Explore"
+    openapi_spec_component_schemas = (ExploreContextSchema,)
+
+    @expose("/", methods=["GET"])
+    @protect()
+    @safe
+    @event_logger.log_this_with_context(
+        action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.get",
+        log_to_statsd=True,
+    )
+    def get(self) -> Response:
+        """Retrives Explore initial context.
+        ---
+        get:
+          description: >-

Review Comment:
   can you change this to `summary` and add a more detailed description?



##########
superset/explore/schemas.py:
##########
@@ -0,0 +1,80 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+#  License ); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+#  AS IS  BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from marshmallow import fields, Schema
+
+
+class DatasetSchema(Schema):
+    cache_timeout = fields.Int()
+    column_formats = fields.Dict()
+    columns = fields.List(fields.Dict())
+    database = fields.Dict()

Review Comment:
   is it possible to define these `fields.Dict` has schemas also? are their 
keys dynamic?



##########
superset/explore/schemas.py:
##########
@@ -0,0 +1,80 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+#  License ); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+#  AS IS  BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from marshmallow import fields, Schema
+
+
+class DatasetSchema(Schema):
+    cache_timeout = fields.Int()
+    column_formats = fields.Dict()
+    columns = fields.List(fields.Dict())
+    database = fields.Dict()
+    datasource_name = fields.Str()
+    default_endpoint = fields.Str()
+    description = fields.Str()
+    edit_url = fields.Str()
+    extra = fields.Dict()
+    fetch_values_predicate = fields.Str()
+    filter_select = fields.Bool()
+    filter_select_enabled = fields.Bool()
+    granularity_sqla = fields.List(fields.List(fields.Dict()))
+    health_check_message = fields.Str()
+    id = fields.Int()
+    is_sqllab_view = fields.Bool()
+    main_dttm_col = fields.Str()
+    metrics = fields.List(fields.Dict())
+    name = fields.Str()
+    offset = fields.Int()
+    order_by_choices = fields.List(fields.List(fields.Str()))
+    owners = fields.List(fields.Number)
+    params = fields.Dict()
+    perm = fields.Str()
+    schema = fields.Str()
+    select_star = fields.Str()
+    sql = fields.Str()
+    table_name = fields.Str()
+    template_params = fields.Dict()
+    time_grain_sqla = fields.List(fields.List(fields.Str()))
+    type = fields.Str()
+    uid = fields.Str()
+    verbose_map = fields.Dict()

Review Comment:
   It would be super awesome to add descriptions to these fields, would help to 
add more context to the OpenAPI spec/doc



##########
superset/explore/api.py:
##########
@@ -0,0 +1,126 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import logging
+
+from flask import g, request, Response
+from flask_appbuilder.api import BaseApi, expose, protect, safe
+
+from superset.charts.commands.exceptions import ChartNotFoundError
+from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP, RouteMethod
+from superset.explore.commands.get import GetExploreCommand
+from superset.explore.commands.parameters import CommandParameters
+from superset.explore.exceptions import DatasetAccessDeniedError, 
WrongEndpointError
+from superset.explore.permalink.exceptions import 
ExplorePermalinkGetFailedError
+from superset.explore.schemas import ExploreContextSchema
+from superset.extensions import event_logger
+from superset.temporary_cache.commands.exceptions import (
+    TemporaryCacheAccessDeniedError,
+    TemporaryCacheResourceNotFoundError,
+)
+
+logger = logging.getLogger(__name__)
+
+
+class ExploreRestApi(BaseApi):
+    method_permission_name = MODEL_API_RW_METHOD_PERMISSION_MAP
+    include_route_methods = {RouteMethod.GET}
+    allow_browser_login = True
+    class_permission_name = "ExploreRestApi"
+    resource_name = "explore"
+    openapi_spec_tag = "Explore"
+    openapi_spec_component_schemas = (ExploreContextSchema,)
+
+    @expose("/", methods=["GET"])
+    @protect()
+    @safe
+    @event_logger.log_this_with_context(
+        action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.get",
+        log_to_statsd=True,
+    )
+    def get(self) -> Response:
+        """Retrives Explore initial context.
+        ---
+        get:
+          description: >-
+            Retrives Explore initial context.
+          parameters:
+          - in: query
+            schema:
+              type: string
+            name: form_data_key
+          - in: query
+            schema:
+              type: string
+            name: permalink_key
+          - in: query
+            schema:
+              type: integer
+            name: slice_id
+          - in: query
+            schema:
+              type: integer
+            name: dataset_id
+          - in: query
+            schema:
+              type: string
+            name: dataset_type
+          responses:
+            200:
+              description: Returns the initial context.
+              content:
+                application/json:
+                  schema:
+                    $ref: '#/components/schemas/ExploreContextSchema'
+            400:
+              $ref: '#/components/responses/400'
+            401:
+              $ref: '#/components/responses/401'
+            404:
+              $ref: '#/components/responses/404'
+            422:
+              $ref: '#/components/responses/422'
+            500:
+              $ref: '#/components/responses/500'
+        """
+        try:
+            params = CommandParameters(
+                actor=g.user,
+                permalink_key=request.args.get("permalink_key"),
+                form_data_key=request.args.get("form_data_key"),
+                dataset_id=request.args.get("dataset_id"),
+                dataset_type=request.args.get("dataset_type"),
+                slice_id=request.args.get("slice_id"),

Review Comment:
   `slice_id` can be a string is it validated?



-- 
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: notifications-unsubscr...@superset.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org
For additional commands, e-mail: notifications-h...@superset.apache.org

Reply via email to