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

Conversation

shaohuzhang1
Copy link
Contributor

fix: fix the defect of interface parameter transmission failure --bug=1050500 --user=王孝刚 【接口文档】接口文档里的获取知识库列表(不分页)接口,带参数查询无数据 https://www.tapd.cn/57709429/s/1634918

--bug=1050500 --user=王孝刚 【接口文档】接口文档里的获取知识库列表(不分页)接口,带参数查询无数据 https://www.tapd.cn/57709429/s/1634918
Copy link

f2c-ci-robot bot commented Dec 20, 2024

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link

f2c-ci-robot bot commented Dec 20, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@@ -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.

@wxg0103 wxg0103 merged commit 64fb771 into main Dec 20, 2024
4 checks passed
@wxg0103 wxg0103 deleted the pr@main@fix_1050500 branch December 20, 2024 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants