Files
ops-assistant/internal/module/rate/query.go
T

29 lines
694 B
Go

package rate
import (
"context"
"fmt"
"os/exec"
"strconv"
"strings"
"time"
)
func query(amount float64, base, quote string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 190*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "python3", "scripts/rate_query.py", strconv.FormatFloat(amount, 'f', -1, 64), base, quote)
out, err := cmd.Output()
if ctx.Err() == context.DeadlineExceeded {
return "", fmt.Errorf("汇率服务超时,请稍后重试")
}
if err != nil {
return "", fmt.Errorf("汇率查询失败")
}
text := strings.TrimSpace(string(out))
if text == "" {
return "", fmt.Errorf("未获取到汇率")
}
return text, nil
}