mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-14 08:16:56 +08:00
Fix equity-history-batch API to support POST JSON requests
- Change route from GET to POST for equity-history-batch endpoint - Update handleEquityHistoryBatch to parse JSON body from POST requests - Maintain backward compatibility with GET query parameters - Ensure public access without authentication as required 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: tinkle-community <tinklefund@gmail.com>
This commit is contained in:
@@ -94,7 +94,7 @@ func (s *Server) setupRoutes() {
|
|||||||
api.GET("/competition", s.handlePublicCompetition)
|
api.GET("/competition", s.handlePublicCompetition)
|
||||||
api.GET("/top-traders", s.handleTopTraders)
|
api.GET("/top-traders", s.handleTopTraders)
|
||||||
api.GET("/equity-history", s.handleEquityHistory)
|
api.GET("/equity-history", s.handleEquityHistory)
|
||||||
api.GET("/equity-history-batch", s.handleEquityHistoryBatch)
|
api.POST("/equity-history-batch", s.handleEquityHistoryBatch)
|
||||||
api.GET("/traders/:id/public-config", s.handleGetPublicTraderConfig)
|
api.GET("/traders/:id/public-config", s.handleGetPublicTraderConfig)
|
||||||
|
|
||||||
// 需要认证的路由
|
// 需要认证的路由
|
||||||
@@ -1602,49 +1602,56 @@ func (s *Server) handleTopTraders(c *gin.Context) {
|
|||||||
|
|
||||||
// handleEquityHistoryBatch 批量获取多个交易员的收益率历史数据(无需认证,用于表现对比)
|
// handleEquityHistoryBatch 批量获取多个交易员的收益率历史数据(无需认证,用于表现对比)
|
||||||
func (s *Server) handleEquityHistoryBatch(c *gin.Context) {
|
func (s *Server) handleEquityHistoryBatch(c *gin.Context) {
|
||||||
// 获取trader_ids参数,支持逗号分隔的多个ID
|
var requestBody struct {
|
||||||
traderIDsParam := c.Query("trader_ids")
|
TraderIDs []string `json:"trader_ids"`
|
||||||
if traderIDsParam == "" {
|
|
||||||
// 如果没有指定trader_ids,则返回前10名的历史数据
|
|
||||||
topTraders, err := s.traderManager.GetTopTradersData()
|
|
||||||
if err != nil {
|
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
|
||||||
"error": fmt.Sprintf("获取前10名交易员失败: %v", err),
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
traders, ok := topTraders["traders"].([]map[string]interface{})
|
|
||||||
if !ok {
|
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "交易员数据格式错误"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 提取trader IDs
|
|
||||||
traderIDs := make([]string, 0, len(traders))
|
|
||||||
for _, trader := range traders {
|
|
||||||
if traderID, ok := trader["trader_id"].(string); ok {
|
|
||||||
traderIDs = append(traderIDs, traderID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result := s.getEquityHistoryForTraders(traderIDs)
|
|
||||||
c.JSON(http.StatusOK, result)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解析逗号分隔的trader IDs
|
// 尝试解析POST请求的JSON body
|
||||||
traderIDs := strings.Split(traderIDsParam, ",")
|
if err := c.ShouldBindJSON(&requestBody); err != nil {
|
||||||
for i := range traderIDs {
|
// 如果JSON解析失败,尝试从query参数获取(兼容GET请求)
|
||||||
traderIDs[i] = strings.TrimSpace(traderIDs[i])
|
traderIDsParam := c.Query("trader_ids")
|
||||||
|
if traderIDsParam == "" {
|
||||||
|
// 如果没有指定trader_ids,则返回前10名的历史数据
|
||||||
|
topTraders, err := s.traderManager.GetTopTradersData()
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
|
"error": fmt.Sprintf("获取前10名交易员失败: %v", err),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
traders, ok := topTraders["traders"].([]map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "交易员数据格式错误"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提取trader IDs
|
||||||
|
traderIDs := make([]string, 0, len(traders))
|
||||||
|
for _, trader := range traders {
|
||||||
|
if traderID, ok := trader["trader_id"].(string); ok {
|
||||||
|
traderIDs = append(traderIDs, traderID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result := s.getEquityHistoryForTraders(traderIDs)
|
||||||
|
c.JSON(http.StatusOK, result)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析逗号分隔的trader IDs
|
||||||
|
requestBody.TraderIDs = strings.Split(traderIDsParam, ",")
|
||||||
|
for i := range requestBody.TraderIDs {
|
||||||
|
requestBody.TraderIDs[i] = strings.TrimSpace(requestBody.TraderIDs[i])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 限制最多20个交易员,防止请求过大
|
// 限制最多20个交易员,防止请求过大
|
||||||
if len(traderIDs) > 20 {
|
if len(requestBody.TraderIDs) > 20 {
|
||||||
traderIDs = traderIDs[:20]
|
requestBody.TraderIDs = requestBody.TraderIDs[:20]
|
||||||
}
|
}
|
||||||
|
|
||||||
result := s.getEquityHistoryForTraders(traderIDs)
|
result := s.getEquityHistoryForTraders(requestBody.TraderIDs)
|
||||||
c.JSON(http.StatusOK, result)
|
c.JSON(http.StatusOK, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user