暂存
This commit is contained in:
109
ragflow/client.go
Normal file
109
ragflow/client.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package ragflow
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/net/gclient"
|
||||
)
|
||||
|
||||
// Client RAGFlow API 客户端
|
||||
type Client struct {
|
||||
BaseURL string
|
||||
APIKey string
|
||||
HTTPClient *gclient.Client
|
||||
}
|
||||
|
||||
// NewClient 创建新的 RAGFlow 客户端
|
||||
func NewClient(baseURL, apiKey string) *Client {
|
||||
client := gclient.New()
|
||||
client.SetHeader("Authorization", fmt.Sprintf("Bearer %s", apiKey))
|
||||
client.SetHeader("Content-Type", "application/json")
|
||||
|
||||
return &Client{
|
||||
BaseURL: strings.TrimSuffix(baseURL, "/"),
|
||||
APIKey: apiKey,
|
||||
HTTPClient: client,
|
||||
}
|
||||
}
|
||||
|
||||
// CommonResponse 通用响应结构
|
||||
type CommonResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// IsSuccess 检查响应是否成功
|
||||
func (r *CommonResponse) IsSuccess() bool {
|
||||
return r.Code == 0
|
||||
}
|
||||
|
||||
// request 发送 HTTP 请求
|
||||
func (c *Client) request(ctx context.Context, method, path string, body interface{}, result interface{}) error {
|
||||
fullURL := c.BaseURL + path
|
||||
|
||||
var reqBody io.Reader
|
||||
if body != nil {
|
||||
jsonData, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal request body failed: %w", err)
|
||||
}
|
||||
reqBody = strings.NewReader(string(jsonData))
|
||||
}
|
||||
|
||||
var resp *gclient.Response
|
||||
var err error
|
||||
|
||||
switch method {
|
||||
case "GET":
|
||||
resp, err = c.HTTPClient.Get(ctx, fullURL)
|
||||
case "POST":
|
||||
resp, err = c.HTTPClient.Post(ctx, fullURL, reqBody)
|
||||
case "PUT":
|
||||
resp, err = c.HTTPClient.Put(ctx, fullURL, reqBody)
|
||||
case "DELETE":
|
||||
resp, err = c.HTTPClient.Delete(ctx, fullURL, reqBody)
|
||||
default:
|
||||
return fmt.Errorf("unsupported method: %s", method)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("http request failed: %w", err)
|
||||
}
|
||||
defer resp.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("http request failed with status: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
respBody, err := resp.ReadAll()
|
||||
if err != nil {
|
||||
return fmt.Errorf("read response body failed: %w", err)
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(respBody, result); err != nil {
|
||||
return fmt.Errorf("unmarshal response failed: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildQueryString 构建查询字符串
|
||||
func buildQueryString(params map[string]interface{}) string {
|
||||
if len(params) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
var parts []string
|
||||
for k, v := range params {
|
||||
parts = append(parts, fmt.Sprintf("%s=%v", url.QueryEscape(k), url.QueryEscape(fmt.Sprintf("%v", v))))
|
||||
}
|
||||
return strings.Join(parts, "&")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user