重构了一下 rag的方法, 使用 goframe的框架, 还有redis连接部分
This commit is contained in:
@@ -2,13 +2,12 @@ package ragflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
)
|
||||
@@ -33,7 +32,7 @@ func init() {
|
||||
|
||||
// 初始化全局客户端
|
||||
httpClient := gclient.New()
|
||||
httpClient.SetHeader("Authorization", fmt.Sprintf("Bearer %s", apiKey))
|
||||
httpClient.SetHeader("Authorization", "Bearer "+apiKey)
|
||||
httpClient.SetHeader("Content-Type", "application/json")
|
||||
|
||||
globalClient = &Client{
|
||||
@@ -79,20 +78,19 @@ func (r *CommonResponse) IsSuccess() bool {
|
||||
}
|
||||
|
||||
// request 发送 HTTP 请求
|
||||
func (c *Client) request(ctx context.Context, method, path string, body interface{}, result interface{}) error {
|
||||
func (c *Client) request(ctx context.Context, method, path string, body interface{}, result interface{}) (err error) {
|
||||
fullURL := c.BaseURL + path
|
||||
|
||||
var reqBody io.Reader
|
||||
var reqBody string
|
||||
if body != nil {
|
||||
jsonData, err := json.Marshal(body)
|
||||
jsonData, err := gjson.Encode(body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal request body failed: %w", err)
|
||||
return gerror.Newf("marshal request body failed: %v", err)
|
||||
}
|
||||
reqBody = strings.NewReader(string(jsonData))
|
||||
reqBody = string(jsonData)
|
||||
}
|
||||
|
||||
var resp *gclient.Response
|
||||
var err error
|
||||
|
||||
switch method {
|
||||
case "GET":
|
||||
@@ -104,28 +102,24 @@ func (c *Client) request(ctx context.Context, method, path string, body interfac
|
||||
case "DELETE":
|
||||
resp, err = c.HTTPClient.Delete(ctx, fullURL, reqBody)
|
||||
default:
|
||||
return fmt.Errorf("unsupported method: %s", method)
|
||||
return gerror.Newf("unsupported method: %s", method)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("http request failed: %w", err)
|
||||
return gerror.Newf("http request failed: %v", err)
|
||||
}
|
||||
defer resp.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("http request failed with status: %d", resp.StatusCode)
|
||||
return gerror.Newf("http request failed with status: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
respBody := resp.ReadAll()
|
||||
if err != nil {
|
||||
return fmt.Errorf("read response body failed: %w", err)
|
||||
if err = gjson.DecodeTo(respBody, result); err != nil {
|
||||
return gerror.Newf("unmarshal response failed: %v", err)
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(respBody, result); err != nil {
|
||||
return fmt.Errorf("unmarshal response failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
// buildQueryString 构建查询字符串
|
||||
@@ -134,9 +128,9 @@ func buildQueryString(params map[string]interface{}) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
var parts []string
|
||||
parts := make([]string, 0, len(params))
|
||||
for k, v := range params {
|
||||
parts = append(parts, fmt.Sprintf("%s=%v", url.QueryEscape(k), url.QueryEscape(fmt.Sprintf("%v", v))))
|
||||
parts = append(parts, url.QueryEscape(k)+"="+url.QueryEscape(g.NewVar(v).String()))
|
||||
}
|
||||
return strings.Join(parts, "&")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user