将 bookmark 和 user_np 服务的生成配置分离为 server 和 client 包, 并更新了相应的导入路径。同时更新了 OpenAPI 配置文件中的包名、输出路径及 启用 strict-server 模式以增强类型安全。 此外,同步更新了 VFS 服务的客户端和服务端生成配置,并完善了其 OpenAPI 错误响应定义与二进制内容支持,增强了 API 的规范性和健壮性。 修复了部分权限校验逻辑,并调整了中间件注册方式以适配新的严格模式接口。
167 lines
3.8 KiB
Go
167 lines
3.8 KiB
Go
// internal/handlers/vfs_driver/vfs_bookmark.go
|
|
|
|
package vfsdriver
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
api "git.zzyxyz.com/zzy/zzyxyz_go_api/gen/bookmarks_client"
|
|
"git.zzyxyz.com/zzy/zzyxyz_go_api/internal/vfs"
|
|
"git.zzyxyz.com/zzy/zzyxyz_go_api/internal/vfs/models"
|
|
)
|
|
|
|
type VfsBookMarkService struct {
|
|
client *api.ClientWithResponses
|
|
}
|
|
|
|
func NewVfsBookMarkService(serverURL string) (*vfs.ProxyEntry, error) {
|
|
client, err := api.NewClientWithResponses(serverURL)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ret := vfs.ProxyEntry{
|
|
Name: "bookmark",
|
|
MatchExt: "bk",
|
|
Proxy: &VfsBookMarkService{client: client},
|
|
}
|
|
return &ret, nil
|
|
}
|
|
|
|
// Create implements ServiceProxy.
|
|
func (v *VfsBookMarkService) Create(servicePath string, node *models.VfsNode, data []byte) ([]byte, error) {
|
|
ctx := context.Background()
|
|
|
|
// 解析传入的数据为 BookmarkRequest
|
|
var req api.BookmarkRequest
|
|
if err := json.Unmarshal(data, &req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 调用 bookmark 服务创建书签
|
|
resp, err := v.client.CreateBookmarkWithResponse(ctx, int64(node.ID), req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 处理响应
|
|
if resp.JSON201 != nil {
|
|
data, err := json.Marshal(resp.JSON201)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("marshal response error: %w", err)
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
// 处理错误情况
|
|
if resp.JSON400 != nil {
|
|
return nil, fmt.Errorf("bad request: %s", resp.JSON400.Message)
|
|
}
|
|
|
|
if resp.JSON500 != nil {
|
|
return nil, fmt.Errorf("server error: %s", resp.JSON500.Message)
|
|
}
|
|
|
|
return nil, fmt.Errorf("unknown error")
|
|
}
|
|
|
|
// Delete implements ServiceProxy.
|
|
func (v *VfsBookMarkService) Delete(servicePath string, node *models.VfsNode) error {
|
|
ctx := context.Background()
|
|
|
|
// 调用 bookmark 服务删除书签
|
|
resp, err := v.client.DeleteBookmarkWithResponse(ctx, int64(node.ID))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 处理响应
|
|
if resp.StatusCode() == 204 {
|
|
return nil
|
|
}
|
|
|
|
// 处理错误情况
|
|
if resp.JSON404 != nil {
|
|
return fmt.Errorf("not found: %s", resp.JSON404.Message)
|
|
}
|
|
|
|
if resp.JSON500 != nil {
|
|
return fmt.Errorf("server error: %s", resp.JSON500.Message)
|
|
}
|
|
|
|
return fmt.Errorf("unknown error")
|
|
}
|
|
|
|
// Get implements ServiceProxy.
|
|
func (v *VfsBookMarkService) Get(servicePath string, node *models.VfsNode) ([]byte, error) {
|
|
ctx := context.Background()
|
|
|
|
// 调用 bookmark 服务获取书签
|
|
resp, err := v.client.GetBookmarkWithResponse(ctx, int64(node.ID))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 处理响应
|
|
if resp.JSON200 != nil {
|
|
data, err := json.Marshal(resp.JSON200)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("marshal response error: %w", err)
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
// 处理错误情况
|
|
if resp.JSON404 != nil {
|
|
return nil, fmt.Errorf("not found: %s", resp.JSON404.Message)
|
|
}
|
|
|
|
return nil, fmt.Errorf("unknown error")
|
|
}
|
|
|
|
// GetName implements ServiceProxy.
|
|
func (v *VfsBookMarkService) GetName() string {
|
|
return "bookmark"
|
|
}
|
|
|
|
// Update implements ServiceProxy.
|
|
func (v *VfsBookMarkService) Update(servicePath string, node *models.VfsNode, data []byte) error {
|
|
ctx := context.Background()
|
|
|
|
// 解析传入的数据为 BookmarkRequest
|
|
var req api.BookmarkRequest
|
|
if err := json.Unmarshal(data, &req); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 调用 bookmark 服务更新书签
|
|
resp, err := v.client.UpdateBookmarkWithResponse(ctx, int64(node.ID), req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 处理响应
|
|
if resp.JSON200 != nil {
|
|
return nil
|
|
}
|
|
|
|
// 处理错误情况
|
|
if resp.JSON400 != nil {
|
|
return fmt.Errorf("bad request: %s", resp.JSON400.Message)
|
|
}
|
|
|
|
if resp.JSON404 != nil {
|
|
return fmt.Errorf("not found: %s", resp.JSON404.Message)
|
|
}
|
|
|
|
if resp.JSON500 != nil {
|
|
return fmt.Errorf("server error: %s", resp.JSON500.Message)
|
|
}
|
|
|
|
return fmt.Errorf("unknown error")
|
|
}
|
|
|
|
var _ vfs.ServiceProxy = (*VfsBookMarkService)(nil)
|