craig-rueda commented on a change in pull request #9129: [datasets] new, API 
using command pattern
URL: 
https://github.com/apache/incubator-superset/pull/9129#discussion_r386547819
 
 

 ##########
 File path: superset/datasets/api.py
 ##########
 @@ -0,0 +1,258 @@
+#
+#   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 expose, protect, safe
+from flask_appbuilder.models.sqla.interface import SQLAInterface
+
+from superset.connectors.sqla.models import SqlaTable
+from superset.constants import RouteMethod
+from superset.datasets.commands.create import CreateDatasetCommand
+from superset.datasets.commands.delete import DeleteDatasetCommand
+from superset.datasets.commands.exceptions import (
+    DatasetCreateFailedError,
+    DatasetDeleteFailedError,
+    DatasetForbiddenError,
+    DatasetInvalidError,
+    DatasetNotFoundError,
+    DatasetUpdateFailedError,
+)
+from superset.datasets.commands.update import UpdateDatasetCommand
+from superset.datasets.schemas import DatasetPostSchema, DatasetPutSchema
+from superset.views.base import DatasourceFilter
+from superset.views.base_api import BaseOwnedModelRestApi
+from superset.views.database.filters import DatabaseFilter
+
+logger = logging.getLogger(__name__)
+
+
+class DatasetRestApi(BaseOwnedModelRestApi):
+    datamodel = SQLAInterface(SqlaTable)
+    base_filters = [["id", DatasourceFilter, lambda: []]]
+
+    resource_name = "dataset"
+    allow_browser_login = True
+
+    class_permission_name = "TableModelView"
+    include_route_methods = RouteMethod.REST_MODEL_VIEW_CRUD_SET | 
{RouteMethod.RELATED}
+
+    list_columns = [
+        "database_name",
+        "changed_by.username",
+        "changed_on",
+        "table_name",
+        "schema",
+    ]
+    show_columns = [
+        "database.database_name",
+        "database.id",
+        "table_name",
+        "sql",
+        "filter_select_enabled",
+        "fetch_values_predicate",
+        "schema",
+        "description",
+        "main_dttm_col",
+        "offset",
+        "default_endpoint",
+        "cache_timeout",
+        "is_sqllab_view",
+        "template_params",
+        "owners.id",
+        "owners.username",
+    ]
+    add_model_schema = DatasetPostSchema()
+    edit_model_schema = DatasetPutSchema()
+    add_columns = ["database", "schema", "table_name", "owners"]
+    edit_columns = [
+        "table_name",
+        "sql",
+        "filter_select_enabled",
+        "fetch_values_predicate",
+        "schema",
+        "description",
+        "main_dttm_col",
+        "offset",
+        "default_endpoint",
+        "cache_timeout",
+        "is_sqllab_view",
+        "template_params",
+        "owners",
+    ]
+    openapi_spec_tag = "Datasets"
+
+    filter_rel_fields_field = {"owners": "first_name", "database": 
"database_name"}
+    filter_rel_fields = {"database": [["id", DatabaseFilter, lambda: []]]}
+
+    @expose("/", methods=["POST"])
+    @protect()
+    @safe
+    def post(self) -> Response:
+        """Creates a new Dataset
+        ---
+        post:
+          description: >-
+            Create a new Dataset
+          requestBody:
+            description: Dataset schema
+            required: true
+            content:
+              application/json:
+                schema:
+                  $ref: '#/components/schemas/{{self.__class__.__name__}}.post'
+          responses:
+            201:
+              description: Dataset added
+              content:
+                application/json:
+                  schema:
+                    type: object
+                    properties:
+                      id:
+                        type: number
+                      result:
+                        $ref: 
'#/components/schemas/{{self.__class__.__name__}}.post'
+            400:
+              $ref: '#/components/responses/400'
+            401:
+              $ref: '#/components/responses/401'
+            422:
+              $ref: '#/components/responses/422'
+            500:
+              $ref: '#/components/responses/500'
+        """
+        if not request.is_json:
+            return self.response_400(message="Request is not JSON")
+        item = self.add_model_schema.load(request.json)
+        # This validates custom Schema with custom validations
+        if item.errors:
+            return self.response_400(message=item.errors)
+        try:
+            new_model = CreateDatasetCommand(g.user, item).run()
+            return self.response(201, id=new_model.id, result=item.data)
+        except DatasetInvalidError:
+            return self.response_422(message=item.errors)
+        except DatasetCreateFailedError as e:
+            logger.error(f"Error creating model {self.__class__.__name__}: 
{e}")
+            return self.response_422(message=str(e))
+
+    @expose("/<pk>", methods=["PUT"])
+    @protect()
+    @safe
+    def put(  # pylint: disable=too-many-return-statements, arguments-differ
+        self, pk: int
+    ) -> Response:
+        """Changes a Dataset
+        ---
+        put:
+          description: >-
+            Changes a Dataset
+          parameters:
+          - in: path
+            schema:
+              type: integer
+            name: pk
+          requestBody:
+            description: Dataset schema
+            required: true
+            content:
+              application/json:
+                schema:
+                  $ref: '#/components/schemas/{{self.__class__.__name__}}.put'
+          responses:
+            200:
+              description: Dataset changed
+              content:
+                application/json:
+                  schema:
+                    type: object
+                    properties:
+                      id:
+                        type: number
+                      result:
+                        $ref: 
'#/components/schemas/{{self.__class__.__name__}}.put'
+            400:
+              $ref: '#/components/responses/400'
+            401:
+              $ref: '#/components/responses/401'
+            403:
+              $ref: '#/components/responses/403'
+            404:
+              $ref: '#/components/responses/404'
+            422:
+              $ref: '#/components/responses/422'
+            500:
+              $ref: '#/components/responses/500'
+        """
+        if not request.is_json:
+            return self.response_400(message="Request is not JSON")
+        item = self.edit_model_schema.load(request.json)
+        # This validates custom Schema with custom validations
+        if item.errors:
+            return self.response_400(message=item.errors)
+        try:
+            changed_model = UpdateDatasetCommand(g.user, pk, item).run()
+            return self.response(200, id=changed_model.id, result=item.data)
+        except DatasetNotFoundError:
+            return self.response_404()
+        except DatasetForbiddenError:
+            return self.response_403()
+        except DatasetInvalidError:
+            return self.response_422(message=item.errors)
 
 Review comment:
   Rather than scabbing errors onto the item itself, why not just place the 
errors into the `InvalidError` being handled here. You can then just bubble 
that up and into the API response.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to