提交
This commit is contained in:
@@ -2,17 +2,13 @@ package ragflow
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
commonHttp "gitee.com/red-future---jilin-g/common/http"
|
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/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 (
|
||||||
@@ -38,7 +34,6 @@ func initClient() {
|
|||||||
globalClient = &Client{
|
globalClient = &Client{
|
||||||
BaseURL: strings.TrimSuffix(baseURL, "/"),
|
BaseURL: strings.TrimSuffix(baseURL, "/"),
|
||||||
APIKey: apiKey,
|
APIKey: apiKey,
|
||||||
HTTPClient: commonHttp.Httpclient,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
g.Log().Infof(ctx, "✅ RAGFlow 客户端初始化成功: baseURL=%s", baseURL)
|
g.Log().Infof(ctx, "✅ RAGFlow 客户端初始化成功: baseURL=%s", baseURL)
|
||||||
@@ -47,14 +42,12 @@ func initClient() {
|
|||||||
|
|
||||||
// loadConfig 从配置文件加载 RAGFlow 配置
|
// loadConfig 从配置文件加载 RAGFlow 配置
|
||||||
func loadConfig(ctx context.Context) (baseURL, apiKey string) {
|
func loadConfig(ctx context.Context) (baseURL, apiKey string) {
|
||||||
// 使用 GoFrame 全局配置(从项目的 config.yml 读取)
|
|
||||||
baseURL = g.Cfg().MustGet(ctx, "ragflow.base_url", "").String()
|
baseURL = g.Cfg().MustGet(ctx, "ragflow.base_url", "").String()
|
||||||
apiKey = g.Cfg().MustGet(ctx, "ragflow.api_key", "").String()
|
apiKey = g.Cfg().MustGet(ctx, "ragflow.api_key", "").String()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetGlobalClient 获取全局客户端(延迟初始化)
|
// GetGlobalClient 获取全局客户端(延迟初始化)
|
||||||
// 使用示例:client := ragflow.GetGlobalClient()
|
|
||||||
func GetGlobalClient() *Client {
|
func GetGlobalClient() *Client {
|
||||||
initClient()
|
initClient()
|
||||||
return globalClient
|
return globalClient
|
||||||
@@ -64,7 +57,6 @@ func GetGlobalClient() *Client {
|
|||||||
type Client struct {
|
type Client struct {
|
||||||
BaseURL string
|
BaseURL string
|
||||||
APIKey string
|
APIKey string
|
||||||
HTTPClient *gclient.Client // HTTP 客户端
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommonResponse 通用响应结构
|
// CommonResponse 通用响应结构
|
||||||
@@ -79,55 +71,30 @@ func (r *CommonResponse) IsSuccess() bool {
|
|||||||
return r.Code == 0
|
return r.Code == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// request 发送 HTTP 请求
|
// request 发送 HTTP 请求(使用统一的common/http包)
|
||||||
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) {
|
||||||
fullURL := c.BaseURL + path
|
fullURL := c.BaseURL + path
|
||||||
|
|
||||||
// 序列化请求体
|
headers := map[string]string{
|
||||||
var reqBody string
|
|
||||||
if body != nil {
|
|
||||||
jsonData, jsonErr := gjson.Encode(body)
|
|
||||||
if jsonErr != nil {
|
|
||||||
return gerror.Newf("marshal request body failed: %v", jsonErr)
|
|
||||||
}
|
|
||||||
reqBody = string(jsonData)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置请求头和超时
|
|
||||||
// 注意:使用 Chain 模式,避免修改全局 Httpclient
|
|
||||||
client := c.HTTPClient.Timeout(180 * time.Second).Header(map[string]string{
|
|
||||||
"Authorization": "Bearer " + c.APIKey,
|
"Authorization": "Bearer " + c.APIKey,
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
})
|
}
|
||||||
|
|
||||||
// 发送请求
|
|
||||||
var resp *gclient.Response
|
|
||||||
switch method {
|
switch method {
|
||||||
case "GET":
|
case "GET":
|
||||||
resp, err = client.Get(ctx, fullURL)
|
err = commonHttp.Get(ctx, fullURL, headers, result, body)
|
||||||
case "POST":
|
case "POST":
|
||||||
resp, err = client.Post(ctx, fullURL, reqBody)
|
err = commonHttp.Post(ctx, fullURL, headers, result, body)
|
||||||
case "PUT":
|
case "PUT":
|
||||||
resp, err = client.Put(ctx, fullURL, reqBody)
|
err = commonHttp.Put(ctx, fullURL, headers, result, body)
|
||||||
case "DELETE":
|
case "DELETE":
|
||||||
resp, err = client.Delete(ctx, fullURL, reqBody)
|
err = commonHttp.Delete(ctx, fullURL, headers, result, body)
|
||||||
default:
|
default:
|
||||||
return gerror.Newf("unsupported method: %s", method)
|
return gerror.Newf("unsupported method: %s", method)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return gerror.Newf("request failed: %v", err)
|
return gerror.Newf("RAGFlow API request failed: %v", err)
|
||||||
}
|
|
||||||
defer resp.Close()
|
|
||||||
|
|
||||||
respBody := resp.ReadAll()
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
return gerror.Newf("http status %d: %s", resp.StatusCode, string(respBody))
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = gjson.DecodeTo(respBody, result); err != nil {
|
|
||||||
return gerror.Newf("unmarshal response failed: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
commonHttp "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"
|
||||||
)
|
)
|
||||||
@@ -185,7 +186,7 @@ func (c *Client) UploadDocumentFromText(ctx context.Context, datasetId, content,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 发送请求
|
// 发送请求
|
||||||
client := c.HTTPClient.Clone()
|
client := commonHttp.Httpclient.Clone()
|
||||||
client.SetHeader("Authorization", "Bearer "+c.APIKey)
|
client.SetHeader("Authorization", "Bearer "+c.APIKey)
|
||||||
client.SetHeader("Content-Type", writer.FormDataContentType())
|
client.SetHeader("Content-Type", writer.FormDataContentType())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user