Skip to content

Conversation

@Bainianzzz
Copy link
Collaborator

Description

优化OpenApi的代码:

  • 删除ApiBase类
  • user类中添加属性is_self
  • 在api请求函数中判断api_keys和用户是否删除/创建成功
  • 优化项目获取性能

- place yield inside the loop of the projects
- check if creating & deleting user or api_keys inside the api func
- add property is_self in user
@Bainianzzz Bainianzzz requested a review from SAKURA-CAT January 20, 2026 03:35
@Bainianzzz Bainianzzz self-assigned this Jan 20, 2026
@Bainianzzz Bainianzzz added 💪 enhancement New feature or request 🔌 api SwanLab OpenAPI labels Jan 20, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Bainianzzz, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在优化OpenAPI模块的代码结构和性能。主要通过移除不必要的基类来简化继承关系,增强 User 类的用户身份管理,并统一API操作的成功判断逻辑。此外,还改进了项目列表的获取机制,以提高数据加载效率。

Highlights

  • 删除 ApiBase 类: 移除了 ApiBase 基类,所有继承自该基类的类(如 Experiment, Experiments, Project, Projects, User)现在直接在构造函数中管理 _client 属性,简化了继承结构。
  • 在 User 类中添加 is_self 属性: 在 User 类中引入了 is_self 属性,用于判断当前 User 实例是否代表登录用户。这使得API密钥管理和用户创建等操作的权限判断更加清晰和安全。
  • 优化 API 请求结果判断: 引入了 check_createdcheck_deleted 工具函数,统一了 create_api_key, delete_api_keycreate_user 等API操作的成功判断逻辑,使其基于HTTP状态码返回布尔值,提高了代码的可读性和一致性。
  • 优化项目获取性能: 重构了 Projects 类的 __iter__ 方法,采用 yield from 语句实现逐页获取项目,而不是一次性加载所有项目,显著提升了项目列表遍历的效率和内存使用。

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

这次的PR主要优化了OpenAPI的代码,包括移除了ApiBase基类,为User类增加了is_self属性,改进了API请求的成功判断逻辑,并优化了项目列表的获取性能。
代码重构做得不错,特别是移除ApiBase使得代码更清晰。项目获取性能的优化通过流式加载避免了一次性加载所有数据,值得称赞。
不过,在审查中发现了一些问题:

  • 新增的check_createdcheck_deleted工具函数被错误地调用,这会导致运行时错误。
  • User.create方法的权限检查逻辑存在问题,可能会错误地阻止root用户创建新用户。
  • 项目迭代器中存在硬编码的魔法数字。

这些问题需要修复以保证代码的正确性和可维护性。

- fix type error in self_hosted.py and user's api func
- add a constant for project page size
- discard constant PAGE_SIZE
- let adapter handle the exception when creating & deleting user info
- opt unit test for projects
@Bainianzzz
Copy link
Collaborator Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

本次 PR 主要优化了 OpenApi 的代码结构,包括:

  • 删除了不再需要的 ApiBase 基类,简化了继承关系。
  • User 类中引入了 is_self 属性,替代了原有的 _is_other_user 字段,使代码更清晰。
  • 优化了 Projects 类的项目获取逻辑,通过分页加载代替一次性加载所有项目,有效降低了内存消耗。
  • 尝试重构 API 请求函数,以更统一的方式处理成功/失败状态。

整体来看,这些改动提升了代码的可读性和性能。但是,在 API 请求函数的重构中,存在一些问题。核心 API 函数(如 create_api_key, delete_api_key, create_user)的返回值被修改为 None,而调用它们的地方期望得到一个布尔值来判断操作是否成功。这会导致功能异常,例如创建 API Key 的逻辑将永远不会成功。我在相关文件中提出了具体的修改建议。请修复这些问题以确保 API 功能正常。

@SAKURA-CAT SAKURA-CAT merged commit c820aab into feature/open-api Jan 21, 2026
5 checks passed
@SAKURA-CAT SAKURA-CAT deleted the opt/open-api branch January 21, 2026 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔌 api SwanLab OpenAPI 💪 enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants