This commit is contained in:
2026-08-26 02:10:05 +08:00
parent f14423418a
commit 550fdbf016
11 changed files with 140 additions and 177 deletions

View File

@@ -31,31 +31,33 @@ type Assets struct {
Available float64 `json:"available"`
}
func (c *Client) Positions(ctx context.Context) ([]Position, error) {
func (c *Client) Positions(ctx context.Context) ([]string, []Position, error) {
return c.decodePositions(ctx, "/api/v2/positions")
}
func (c *Client) Holding(ctx context.Context) ([]Position, error) {
func (c *Client) Holding(ctx context.Context) ([]string, []Position, error) {
return c.decodePositions(ctx, "/api/holding")
}
func (c *Client) decodePositions(ctx context.Context, path string) ([]Position, error) {
func (c *Client) decodePositions(ctx context.Context, path string) ([]string, []Position, error) {
raw := map[string]json.RawMessage{}
if err := c.post(ctx, path, map[string]any{"account": c.accountType}, &raw); err != nil {
return nil, err
return nil, nil, err
}
codes := make([]string, 0, len(raw))
out := make([]Position, 0, len(raw))
for code, blob := range raw {
var p Position
if err := json.Unmarshal(blob, &p); err != nil {
return nil, fmt.Errorf("position %s: %w", code, err)
return nil, nil, fmt.Errorf("position %s: %w", code, err)
}
if p.StockCode == "" {
p.StockCode = code
}
codes = append(codes, code)
out = append(out, p)
}
return out, nil
return codes, out, nil
}
func (c *Client) Assets(ctx context.Context) (*Assets, error) {