feat: add realtime rate command
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user