ragflow的dto
This commit is contained in:
77
ragflow/service/client.go
Normal file
77
ragflow/service/client.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/ragflow/dto"
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/gclient"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
BaseURL string
|
||||
ApiKey string
|
||||
Client *gclient.Client
|
||||
}
|
||||
|
||||
// NewClient 创建一个新的 RAGFlow 客户端
|
||||
func NewClient(baseUrl, apiKey string) *Client {
|
||||
return &Client{
|
||||
BaseURL: baseUrl,
|
||||
ApiKey: apiKey,
|
||||
Client: g.Client().SetTimeout(30 * time.Second),
|
||||
}
|
||||
}
|
||||
|
||||
// request 发送 HTTP 请求
|
||||
func (c *Client) request(ctx context.Context, method, path string, data interface{}, result interface{}) error {
|
||||
url := fmt.Sprintf("%s%s", c.BaseURL, path)
|
||||
|
||||
req := c.Client.Header(map[string]string{
|
||||
"Authorization": fmt.Sprintf("Bearer %s", c.ApiKey),
|
||||
"Content-Type": "application/json",
|
||||
})
|
||||
|
||||
var res *gclient.Response
|
||||
var err error
|
||||
|
||||
switch method {
|
||||
case "GET":
|
||||
res, err = req.Get(ctx, url, data)
|
||||
case "POST":
|
||||
res, err = req.Post(ctx, url, data)
|
||||
case "PUT":
|
||||
res, err = req.Put(ctx, url, data)
|
||||
case "DELETE":
|
||||
res, err = req.Delete(ctx, url, data)
|
||||
default:
|
||||
return fmt.Errorf("unsupported method: %s", method)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Close()
|
||||
|
||||
// 读取响应体
|
||||
body := res.ReadAllString()
|
||||
|
||||
// 解析响应
|
||||
if result != nil {
|
||||
if err := gjson.DecodeTo(body, result); err != nil {
|
||||
return fmt.Errorf("failed to decode response: %v, body: %s", err, body)
|
||||
}
|
||||
|
||||
// 检查业务错误码
|
||||
if commonRes, ok := result.(*dto.CommonResponse); ok {
|
||||
if !commonRes.IsSuccess() {
|
||||
return fmt.Errorf("api error: code=%d, message=%s", commonRes.Code, commonRes.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user