更新
This commit is contained in:
@@ -24,7 +24,7 @@ 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,
|
||||
@@ -34,8 +34,8 @@ func NewClient(baseURL, apiKey string) *Client {
|
||||
|
||||
// CommonResponse 通用响应结构
|
||||
type CommonResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ func (r *CommonResponse) IsSuccess() bool {
|
||||
// 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)
|
||||
@@ -56,10 +56,10 @@ func (c *Client) request(ctx context.Context, method, path string, body interfac
|
||||
}
|
||||
reqBody = strings.NewReader(string(jsonData))
|
||||
}
|
||||
|
||||
|
||||
var resp *gclient.Response
|
||||
var err error
|
||||
|
||||
|
||||
switch method {
|
||||
case "GET":
|
||||
resp, err = c.HTTPClient.Get(ctx, fullURL)
|
||||
@@ -72,25 +72,25 @@ func (c *Client) request(ctx context.Context, method, path string, body interfac
|
||||
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()
|
||||
|
||||
respBody := 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
|
||||
}
|
||||
|
||||
@@ -99,11 +99,10 @@ 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