feat[Go]: implement create_connector API (#15285)

### What problem does this PR solve?

implement create_connector API

### Type of change

- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
Haruko386
2026-05-27 15:54:11 +08:00
committed by GitHub
parent 2c099bbb95
commit 82318dee5d
5 changed files with 193 additions and 7 deletions

View File

@@ -20,6 +20,7 @@ import (
"net/http"
"ragflow/internal/common"
"ragflow/internal/entity"
"strings"
"github.com/gin-gonic/gin"
@@ -28,6 +29,7 @@ import (
type connectorService interface {
ListConnectors(userID string) (*service.ListConnectorsResponse, error)
CreateConnector(userID string, req *service.CreateConnectorRequest) (*entity.Connector, error)
GetConnector(connectorID string, userID string) (*entity.Connector, common.ErrorCode, error)
}
@@ -105,3 +107,70 @@ func (h *ConnectorHandler) GetConnector(c *gin.Context) {
"message": "success",
})
}
// CreateConnector create connector
// @Summary create Connectors
// @Description create a connectors for the current user
// @Tags connector
// @Accept json
// @Produce json
// @Success 200 {object} service.ListConnectorsResponse
// @Router /connector/ [post]
func (h *ConnectorHandler) CreateConnector(c *gin.Context) {
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
jsonError(c, errorCode, errorMessage)
return
}
var req service.CreateConnectorRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": common.CodeBadRequest,
"data": nil,
"message": "Invalid request body: " + err.Error(),
})
return
}
if strings.TrimSpace(req.Name) == "" {
c.JSON(http.StatusOK, gin.H{
"code": common.CodeDataError,
"data": nil,
"message": "name is required",
})
return
}
if strings.TrimSpace(req.Source) == "" {
c.JSON(http.StatusOK, gin.H{
"code": common.CodeDataError,
"data": nil,
"message": "source is required",
})
return
}
if req.Config == nil {
c.JSON(http.StatusOK, gin.H{
"code": common.CodeDataError,
"data": nil,
"message": "config is required",
})
return
}
connector, err := h.connectorService.CreateConnector(user.ID, &req)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"code": common.CodeServerError,
"data": nil,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": common.CodeSuccess,
"data": connector,
"message": "success",
})
}

View File

@@ -25,6 +25,10 @@ func (s fakeConnectorService) ListConnectors(string) (*service.ListConnectorsRes
return &service.ListConnectorsResponse{Connectors: []*dao.ConnectorListItem{}}, nil
}
func (s fakeConnectorService) CreateConnector(string, *service.CreateConnectorRequest) (*entity.Connector, error) {
return s.connector, s.err
}
func (s fakeConnectorService) GetConnector(string, string) (*entity.Connector, common.ErrorCode, error) {
if s.err != nil {
return nil, s.code, s.err
@@ -32,6 +36,13 @@ func (s fakeConnectorService) GetConnector(string, string) (*entity.Connector, c
return s.connector, common.CodeSuccess, nil
}
func (s fakeConnectorService) UpdateConnector(string, string, *service.UpdateConnectorRequest) (*entity.Connector, common.ErrorCode, error) {
if s.err != nil {
return nil, s.code, s.err
}
return s.connector, common.CodeSuccess, nil
}
func TestConnectorHandlerGetConnector(t *testing.T) {
gin.SetMode(gin.TestMode)