更新一下http.go
This commit is contained in:
15
http/http.go
15
http/http.go
@@ -89,20 +89,7 @@ func doRequest(ctx context.Context, method string, url string, headers map[strin
|
|||||||
defer response.Close()
|
defer response.Close()
|
||||||
result := response.ReadAll()
|
result := response.ReadAll()
|
||||||
|
|
||||||
// 第三方API特例:RAGFlow等第三方API响应格式为{code,data,message}一层结构,直接解析原始JSON到target
|
// 统一处理内部API响应格式:{code:200,message:"",data:{...}}
|
||||||
// 内部API格式为{code:200,message:"",data:{...}}两层结构,需经过DefaultHandlerResponse二次解析
|
|
||||||
// 判断依据:URL包含/api/v1/(不影响内部API调用)
|
|
||||||
isThirdPartyAPI := strings.Contains(url, "/api/v1/")
|
|
||||||
|
|
||||||
if isThirdPartyAPI {
|
|
||||||
// 第三方API特例:直接解析原始JSON到target,不经过DefaultHandlerResponse
|
|
||||||
if err = gconv.Struct(result, target); err != nil {
|
|
||||||
return errors.New("第三方API响应解析失败: " + err.Error())
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 内部API:保持原有逻辑,先解析为DefaultHandlerResponse,再提取data字段
|
|
||||||
resultStrut := &ghttp.DefaultHandlerResponse{}
|
resultStrut := &ghttp.DefaultHandlerResponse{}
|
||||||
|
|
||||||
if err = gconv.Struct(result, &resultStrut); err != nil { // 修复:增加err检查
|
if err = gconv.Struct(result, &resultStrut); err != nil { // 修复:增加err检查
|
||||||
|
|||||||
@@ -2,14 +2,15 @@ package ragflow
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
"gitee.com/red-future---jilin-g/common/http"
|
|
||||||
"github.com/gogf/gf/v2/errors/gerror"
|
"github.com/gogf/gf/v2/errors/gerror"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
|
"github.com/gogf/gf/v2/net/gclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -119,21 +120,9 @@ func (r *CommonResponse) IsSuccess() bool {
|
|||||||
// request 发送 HTTP 请求
|
// request 发送 HTTP 请求
|
||||||
//
|
//
|
||||||
// 为什么不使用 common/http 包:
|
// 为什么不使用 common/http 包:
|
||||||
//
|
// common/http包统一处理内部API响应格式(ghttp.DefaultHandlerResponse),
|
||||||
// 1. common/http/http.go:61 会用内部请求的Authorization覆盖RAGFlow API key:
|
// RAGFlow API返回格式为{code,data,message}一层结构,与内部API不同。
|
||||||
// Httpclient.SetHeader("Authorization", g.RequestFromCtx(ctx).GetHeader("Authorization"))
|
// 因此直接使用 g.Client() 调用第三方API,在此处理RAGFlow特有的响应格式。
|
||||||
// 这会导致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) {
|
func (c *Client) request(ctx context.Context, method, path string, body interface{}, result interface{}) (err error) {
|
||||||
endpoint := c.getNextEndpoint()
|
endpoint := c.getNextEndpoint()
|
||||||
if endpoint == "" {
|
if endpoint == "" {
|
||||||
@@ -142,26 +131,53 @@ func (c *Client) request(ctx context.Context, method, path string, body interfac
|
|||||||
|
|
||||||
fullURL := endpoint + path
|
fullURL := endpoint + path
|
||||||
|
|
||||||
// 使用common/http包
|
// 创建HTTP客户端并设置RAGFlow专用请求头
|
||||||
var headers = make(map[string]string)
|
client := g.Client()
|
||||||
headers["Authorization"] = "Bearer " + c.APIKey
|
client.SetHeader("Authorization", "Bearer "+c.APIKey)
|
||||||
headers["Content-Type"] = "application/json"
|
client.SetHeader("Content-Type", "application/json")
|
||||||
|
|
||||||
|
// 发送HTTP请求(避免data展开导致的双重包装)
|
||||||
|
var response *gclient.Response
|
||||||
switch method {
|
switch method {
|
||||||
case "GET":
|
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":
|
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":
|
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":
|
case "DELETE":
|
||||||
if body != nil {
|
if body != nil {
|
||||||
err = http.Delete(ctx, fullURL, headers, result, body)
|
response, err = client.Delete(ctx, fullURL, body)
|
||||||
} else {
|
} else {
|
||||||
err = http.Delete(ctx, fullURL, headers, result)
|
response, err = client.Delete(ctx, fullURL)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return gerror.Newf("unsupported method: %s", method)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user