CreateAPIKey 创建API密钥 @Summary 创建API密钥 @Description 创建一个新的API密钥用于外部系统集成。仅限管理员或有知识库管理权限的用户调用。 @Tags API密钥管理 @Accept json @Produce json @Security BearerAuth @Param request body CreateAPIKeyRequest true "创建API密钥请求" @Param library_id path int false "知识库ID(通过路径传递)" @Success 200 {object} model.CommonRespon
(c *gin.Context)
| 70 | // @Router /api/api-keys [post] |
| 71 | // @Router /api/libraries/{library_id}/api-keys [post] # 知识库相关路由 |
| 72 | func (ctrl *APIKeyController) CreateAPIKey(c *gin.Context) { |
| 73 | var req CreateAPIKeyRequest |
| 74 | if err := c.ShouldBindJSON(&req); err != nil { |
| 75 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(err)) |
| 76 | return |
| 77 | } |
| 78 | |
| 79 | eid := config.GetEID(c) |
| 80 | creatorID := config.GetUserId(c) |
| 81 | role := config.GetUserRole(c) |
| 82 | |
| 83 | var libraryID *int64 |
| 84 | libraryIDParam := c.Param("library_id") |
| 85 | |
| 86 | if libraryIDParam != "" { |
| 87 | id, err := strconv.ParseInt(libraryIDParam, 10, 64) |
| 88 | if err != nil { |
| 89 | c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("无效的知识库ID参数")) |
| 90 | return |
| 91 | } |
| 92 | libraryID = &id |
| 93 | } |
| 94 | |
| 95 | apiKeyService := mcpsvc.NewAPIKeyService() |
| 96 | apiKey, apiKeyStr, err := apiKeyService.CreateAPIKey(c.Request.Context(), eid, creatorID, role, req.Name, req.Description, libraryID) |
| 97 | if err != nil { |
| 98 | if isPermissionRelatedError(err) { |
| 99 | c.JSON(http.StatusForbidden, model.ForbiddenError.ToResponse(err)) |
| 100 | return |
| 101 | } |
| 102 | c.JSON(http.StatusInternalServerError, model.DBError.ToResponse(err)) |
| 103 | return |
| 104 | } |
| 105 | |
| 106 | c.JSON(http.StatusOK, model.Success.ToResponse(CreateAPIKeyResponse{Key: apiKeyStr, ID: apiKey.ID})) |
| 107 | } |
| 108 | |
| 109 | // GetAPIKeys 获取API密钥列表 |
| 110 | // @Summary 获取API密钥列表 |
nothing calls this directly
no test coverage detected