Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix the defect of interface parameter transmission failure #1879

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/dataset/views/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def get(self, request: Request, dataset_id: str):
tags=["知识库"])
@has_permissions(PermissionConstants.DATASET_READ, compare=CompareConstants.AND)
def get(self, request: Request):
d = DataSetSerializers.Query(data={**request.query_params, 'user_id': str(request.user.id)})
data = {key: str(value) for key, value in request.query_params.items()}
d = DataSetSerializers.Query(data={**data, 'user_id': str(request.user.id)})
d.is_valid()
return result.success(d.list())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code has several issues:

  1. Code Duplication: The get method is defined twice with different function signatures.
  2. Error Handling: There's no error handling implemented for exceptions that might occur during serialization or query processing.

Suggestions

  • Combine the two functions into one using overloading or default arguments to handle both use cases (without parameters and with specific parameters).

  • Implement exception/error handling:

    try:
        d = DataSetSerializers.Query(data=data)
        d.is_valid()
        return result.success(d.list())
    except Exception as e:
        # Handle errors appropriately, such as logging them or returning an error response
        raise Exception(f"Failed to retrieve dataset: {str(e)}")
  • Simplify accessing values from request.query_params: You can convert all values directly to strings if there are no special requirements regarding type conversion.

Here's the revised version of the code incorporating these suggestions:

@@ -103,7 +105,6 @@ def get(self, request: Request, dataset_id: str):
                          tags=["知识库"])
     @has_permissions(PermissionConstants.DATASET_READ, compare=CompareConstants.AND)
     def get(self, request: Request = None):
-        if request is not None:
-            data = {key: str(value) for key, value in request.query_params.items()}
+            data = {key: str(value) for key, value in request.query_params}
             d = DataSetSerializers.Query(data={**data, 'user_id': str(request.user.id)})
             d.is_valid()
         else:
             q_params = request.query_params.copy()  # Assuming without a specific parameter this is the same behavior
             q_params['user_id'] = str(request.user.id)
             d = DataSetSerializers.Query(data=q_params)
             d.is_valid()

         return result.success(d.list())

This should make the code more efficient and maintainable while addressing some common issues.

Expand Down
Loading