This commit is contained in:
2026-08-25 18:59:18 +08:00
parent fa62436a73
commit ec58641d09
23 changed files with 563 additions and 944 deletions

View File

@@ -11,25 +11,30 @@ import (
"time"
)
// Client 调用 QMT HTTP API。
type Client struct {
baseURL string
token string
baseURL string
token string
accountType string
http *http.Client
http *http.Client
}
func New(baseURL, token, accountType string, timeout time.Duration) *Client {
base := strings.TrimRight(baseURL, "/")
return &Client{baseURL: base, token: token, accountType: accountType, http: &http.Client{Timeout: timeout}}
}
func (c *Client) Account(override string) string {
if strings.TrimSpace(override) != "" {
return override
func New(baseURL, token string, timeout time.Duration) *Client {
if timeout <= 0 {
timeout = 15 * time.Second
}
return c.accountType
return &Client{
baseURL: strings.TrimRight(baseURL, "/"),
token: token,
accountType: "stock",
http: &http.Client{Timeout: timeout},
}
}
func (c *Client) SetAccountType(accountType string) *Client {
if strings.TrimSpace(accountType) != "" {
c.accountType = accountType
}
return c
}
func (c *Client) get(ctx context.Context, path string, dest any) error {
@@ -92,14 +97,32 @@ func (c *Client) do(ctx context.Context, method, path string, body any, dest any
return nil
}
func truncate(b []byte, n int) string {
if len(b) <= n {
return string(b)
func (c *Client) getField(ctx context.Context, path, key string) (any, error) {
var out map[string]any
if err := c.get(ctx, path, &out); err != nil {
return nil, err
}
return string(b[:n]) + "..."
return out[key], nil
}
func ArrayJoin(items []string) string {
func (c *Client) postField(ctx context.Context, path string, body any, key string) (any, error) {
var out map[string]any
if err := c.post(ctx, path, body, &out); err != nil {
return nil, err
}
if msg, ok := out["error"].(string); ok && msg != "" {
return nil, &BusinessError{Message: msg}
}
if key == "" {
return out, nil
}
if v, ok := out[key]; ok {
return v, nil
}
return out, nil
}
func csvJoin(items []string) string {
parts := make([]string, 0, len(items))
for _, s := range items {
s = strings.TrimSpace(s)
@@ -109,3 +132,10 @@ func ArrayJoin(items []string) string {
}
return strings.Join(parts, ",")
}
func truncate(b []byte, n int) string {
if len(b) <= n {
return string(b)
}
return string(b[:n]) + "..."
}