This commit is contained in:
2025-10-10 07:13:36 -07:00
commit 95c66c0a8a
1023 changed files with 78353 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
package exchangeRate
import (
"errors"
"strconv"
"time"
"github.com/go-resty/resty/v2"
)
const (
Url = "https://api.exchangerate.host"
)
type Response struct {
Success bool `json:"success"`
Terms string `json:"terms"`
Privacy string `json:"privacy"`
Query struct {
From string `json:"from"`
To string `json:"to"`
Amount float64 `json:"amount"`
} `json:"query"`
Info struct {
Timestamp int64 `json:"timestamp"`
Quote float64 `json:"quote"`
} `json:"info"`
Result float64 `json:"result"`
}
func GetExchangeRete(form, to, access string, amount float64) (float64, error) {
client := resty.New()
client.SetRetryCount(3)
client.SetTimeout(5 * time.Second)
client.SetBaseURL(Url)
// amount to string
amountStr := strconv.FormatFloat(amount, 'f', -1, 64)
client.SetQueryParams(map[string]string{
"from": form,
"to": to,
"amount": amountStr,
"access_key": access,
})
resp := new(Response)
_, err := client.R().SetResult(resp).Get("/convert")
if err != nil {
return 0, err
}
if !resp.Success {
return 0, errors.New("exchange rate failed")
}
return resp.Result, nil
}
+12
View File
@@ -0,0 +1,12 @@
package exchangeRate
import "testing"
func TestGetExchangeRete(t *testing.T) {
t.Skip("skip TestGetExchangeRete")
result, err := GetExchangeRete("USD", "CNY", "90734e5af4f5353114cdaf3bb9c3f2e3", 1)
if err != nil {
t.Fatal(err)
}
t.Log(result)
}