兼容新的mongo方法格式,并且修复client的apitoken覆盖问题

This commit is contained in:
Cold
2026-01-12 17:36:14 +08:00
committed by 张斌
parent 65dc3d525d
commit bf8b220a99
4 changed files with 94 additions and 15 deletions

View File

@@ -7,9 +7,10 @@ import (
"sync"
"sync/atomic"
commonHttp "gitee.com/red-future---jilin-g/common/http"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gclient"
)
var (
@@ -116,7 +117,23 @@ func (r *CommonResponse) IsSuccess() bool {
return r.Code == 0
}
// request 发送 HTTP 请求使用统一的common/http包支持负载均衡
// request 发送 HTTP 请求
//
// 为什么不使用 common/http 包:
// 1. common/http/http.go:61 会用内部请求的Authorization覆盖RAGFlow API key
// Httpclient.SetHeader("Authorization", g.RequestFromCtx(ctx).GetHeader("Authorization"))
// 这会导致RAGFlow API认证失败因为内部token不是RAGFlow的API key
//
// 2. common/http/http.go:69-74 强制解析为内部API响应格式ghttp.DefaultHandlerResponse
// resultStrut := &ghttp.DefaultHandlerResponse{}
// if err = gconv.Struct(result, &resultStrut); err != nil {
// err = errors.New(resultStrut.Message)
// } else if resultStrut.Code == 200 || resultStrut.Code == 0 {
// gconv.Struct(resultStrut.Data, target)
// }
// RAGFlow API返回格式与内部API不同会导致解析失败
//
// 因此直接使用 g.Client() 调用第三方API避免上述问题
func (c *Client) request(ctx context.Context, method, path string, body interface{}, result interface{}) (err error) {
endpoint := c.getNextEndpoint()
if endpoint == "" {
@@ -124,26 +141,51 @@ func (c *Client) request(ctx context.Context, method, path string, body interfac
}
fullURL := endpoint + path
headers := map[string]string{
"Authorization": "Bearer " + c.APIKey,
"Content-Type": "application/json",
// 添加详细日志:请求信息
g.Log().Infof(ctx, "RAGFlow请求: %s %s", method, fullURL)
if body != nil {
bodyJSON := g.NewVar(body).String()
g.Log().Infof(ctx, "RAGFlow请求体: %s", bodyJSON)
}
// 创建新的HTTP客户端实例避免共享状态
client := g.Client()
client.SetHeader("Authorization", "Bearer "+c.APIKey)
client.SetHeader("Content-Type", "application/json")
var response *gclient.Response
switch method {
case "GET":
err = commonHttp.Get(ctx, fullURL, headers, result, body)
response, err = client.Get(ctx, fullURL, body)
case "POST":
err = commonHttp.Post(ctx, fullURL, headers, result, body)
response, err = client.Post(ctx, fullURL, body)
case "PUT":
err = commonHttp.Put(ctx, fullURL, headers, result, body)
response, err = client.Put(ctx, fullURL, body)
case "DELETE":
err = commonHttp.Delete(ctx, fullURL, headers, result, body)
response, err = client.Delete(ctx, fullURL, body)
default:
return gerror.Newf("unsupported method: %s", method)
}
if err != nil {
return gerror.Newf("RAGFlow API request failed: %v", err)
g.Log().Errorf(ctx, "RAGFlow HTTP请求失败: %v", err)
return gerror.Wrapf(err, "HTTP request to RAGFlow failed")
}
if response == nil {
return gerror.New("HTTP response is nil")
}
defer response.Close()
// 读取响应体
respBytes := response.ReadAll()
g.Log().Infof(ctx, "RAGFlow响应: %s", string(respBytes))
// 解析JSON到result
if err = gjson.DecodeTo(respBytes, result); err != nil {
g.Log().Errorf(ctx, "RAGFlow响应解析失败: %v, 原始响应: %s", err, string(respBytes))
return gerror.Wrapf(err, "failed to decode RAGFlow response")
}
return