package rainyun import ( "bytes" "context" "encoding/json" "fmt" "io" "net/http" "net/url" "time" ) const defaultEndpoint = "https://api.v2.rainyun.com" type client struct { httpClient *http.Client baseURL string apiKey string } func newClient(endpoint, apiKey string) *client { if endpoint == "" { endpoint = defaultEndpoint } return &client{ httpClient: &http.Client{Timeout: 30 * time.Second}, baseURL: endpoint, apiKey: apiKey, } } type apiResponse struct { Code int `json:"code"` Msg string `json:"msg"` Data json.RawMessage `json:"data"` } func (c *client) get(ctx context.Context, path string, result any) error { return c.do(ctx, "GET", path, nil, result, nil) } func (c *client) getWithQuery(ctx context.Context, path string, query map[string]string, result any) error { return c.do(ctx, "GET", path, nil, result, query) } func (c *client) post(ctx context.Context, path string, body, result any) error { return c.do(ctx, "POST", path, body, result, nil) } func (c *client) patch(ctx context.Context, path string, body, result any) error { return c.do(ctx, "PATCH", path, body, result, nil) } func (c *client) delete(ctx context.Context, path string, result any) error { return c.do(ctx, "DELETE", path, nil, result, nil) } func (c *client) do(ctx context.Context, method, path string, body, result any, query map[string]string) error { u := c.baseURL + path if len(query) > 0 { params := url.Values{} for k, v := range query { params.Set(k, v) } u += "?" + params.Encode() } var bodyReader io.Reader if body != nil { b, err := json.Marshal(body) if err != nil { return fmt.Errorf("marshal request: %w", err) } bodyReader = bytes.NewReader(b) } req, err := http.NewRequestWithContext(ctx, method, u, bodyReader) if err != nil { return fmt.Errorf("create request: %w", err) } req.Header.Set("X-Api-Key", c.apiKey) req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json") resp, err := c.httpClient.Do(req) if err != nil { return fmt.Errorf("request failed: %w", err) } defer resp.Body.Close() respBody, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("read response: %w", err) } var apiResp apiResponse if err := json.Unmarshal(respBody, &apiResp); err == nil { if resp.StatusCode >= 400 || apiResp.Code != 0 { return &APIError{ StatusCode: resp.StatusCode, Method: method, URL: u, Body: respBody, APICode: apiResp.Code, APIMsg: apiResp.Msg, } } } else if resp.StatusCode >= 400 { return &APIError{ StatusCode: resp.StatusCode, Method: method, URL: u, Body: respBody, } } if result != nil && apiResp.Data != nil { if err := json.Unmarshal(apiResp.Data, result); err != nil { return fmt.Errorf("unmarshal data: %w", err) } } return nil } type APIError struct { StatusCode int Method string URL string Body []byte APICode int APIMsg string } func (e *APIError) Error() string { msg := e.APIMsg if msg == "" { // 非 JSON 响应,截取 body 前 200 字符 body := string(e.Body) if len(body) > 200 { body = body[:200] + "..." } if body != "" { msg = body } else { msg = http.StatusText(e.StatusCode) } } return msg } func (e *APIError) Detail() string { return fmt.Sprintf("%s %s [%d]", e.Method, e.URL, e.StatusCode) }