更新一下http.go

This commit is contained in:
Cold
2026-01-27 17:54:58 +08:00
committed by 张斌
parent 17d6de4ffd
commit e502aeea53
2 changed files with 42 additions and 39 deletions

View File

@@ -2,14 +2,15 @@ package ragflow
import (
"context"
"encoding/json"
"net/url"
"strings"
"sync"
"sync/atomic"
"gitee.com/red-future---jilin-g/common/http"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gclient"
)
var (
@@ -119,21 +120,9 @@ func (r *CommonResponse) IsSuccess() bool {
// 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避免上述问题
// common/http包统一处理内部API响应格式ghttp.DefaultHandlerResponse
// RAGFlow API返回格式为{code,data,message}一层结构与内部API不同。
// 因此直接使用 g.Client() 调用第三方API在此处理RAGFlow特有的响应格式。
func (c *Client) request(ctx context.Context, method, path string, body interface{}, result interface{}) (err error) {
endpoint := c.getNextEndpoint()
if endpoint == "" {
@@ -142,26 +131,53 @@ func (c *Client) request(ctx context.Context, method, path string, body interfac
fullURL := endpoint + path
// 使用common/http包
var headers = make(map[string]string)
headers["Authorization"] = "Bearer " + c.APIKey
headers["Content-Type"] = "application/json"
// 创建HTTP客户端并设置RAGFlow专用请求头
client := g.Client()
client.SetHeader("Authorization", "Bearer "+c.APIKey)
client.SetHeader("Content-Type", "application/json")
// 发送HTTP请求避免data展开导致的双重包装
var response *gclient.Response
switch method {
case "GET":
err = http.Get(ctx, fullURL, headers, result, body)
if body != nil {
response, err = client.Get(ctx, fullURL, body)
} else {
response, err = client.Get(ctx, fullURL)
}
case "POST":
err = http.Post(ctx, fullURL, headers, result, body)
if body != nil {
response, err = client.Post(ctx, fullURL, body)
} else {
response, err = client.Post(ctx, fullURL)
}
case "PUT":
err = http.Put(ctx, fullURL, headers, result, body)
if body != nil {
response, err = client.Put(ctx, fullURL, body)
} else {
response, err = client.Put(ctx, fullURL)
}
case "DELETE":
if body != nil {
err = http.Delete(ctx, fullURL, headers, result, body)
response, err = client.Delete(ctx, fullURL, body)
} else {
err = http.Delete(ctx, fullURL, headers, result)
response, err = client.Delete(ctx, fullURL)
}
default:
return gerror.Newf("unsupported method: %s", method)
}
if err != nil {
return
}
defer response.Close()
// RAGFlow API响应格式{code,data,message}一层结构,直接解析
responseBody := response.ReadAll()
if err = json.Unmarshal(responseBody, result); err != nil {
return gerror.Newf("RAGFlow响应解析失败: %v, 原始响应: %s", err, string(responseBody))
}
return
}