fix(exchange): update exchange rate API endpoint and adjust request parameters
This commit is contained in:
parent
90e2f24d46
commit
58caa497c3
@ -6,10 +6,11 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
"github.com/perfect-panel/server/pkg/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
Url = "https://api.exchangerate.host"
|
||||
Url = "https://api.apilayer.com"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
@ -37,18 +38,20 @@ func GetExchangeRete(form, to, access string, amount float64) (float64, error) {
|
||||
amountStr := strconv.FormatFloat(amount, 'f', -1, 64)
|
||||
|
||||
client.SetQueryParams(map[string]string{
|
||||
"from": form,
|
||||
"to": to,
|
||||
"amount": amountStr,
|
||||
"access_key": access,
|
||||
"from": form,
|
||||
"to": to,
|
||||
"amount": amountStr,
|
||||
})
|
||||
resp := new(Response)
|
||||
_, err := client.R().SetResult(resp).Get("/convert")
|
||||
result := new(Response)
|
||||
resp, err := client.R().SetHeader("apikey", access).SetResult(result).Get("/currency_data/convert")
|
||||
|
||||
if err != nil {
|
||||
|
||||
return 0, err
|
||||
}
|
||||
if !resp.Success {
|
||||
if !result.Success {
|
||||
logger.Info("Exchange Rate Response: ", resp.String())
|
||||
return 0, errors.New("exchange rate failed")
|
||||
}
|
||||
return resp.Result, nil
|
||||
return result.Result, nil
|
||||
}
|
||||
|
||||
@ -4,7 +4,7 @@ import "testing"
|
||||
|
||||
func TestGetExchangeRete(t *testing.T) {
|
||||
t.Skip("skip TestGetExchangeRete")
|
||||
result, err := GetExchangeRete("USD", "CNY", "90734e5af4f5353114cdaf3bb9c3f2e3", 1)
|
||||
result, err := GetExchangeRete("USD", "CNY", "", 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user