mirror-nourybot/pkg/commands/currency.go

33 lines
744 B
Go
Raw Normal View History

2023-09-07 21:09:33 +02:00
package commands
import (
"fmt"
"io"
"net/http"
)
2024-02-28 11:53:39 +01:00
// Currency queries the decapi.me api for the current exchange rate for
// two given currencies.
2023-09-07 21:09:33 +02:00
func Currency(currAmount, currFrom, currTo string) (string, error) {
basePath := "https://decapi.me/misc/currency/"
from := fmt.Sprintf("?from=%s", currFrom)
to := fmt.Sprintf("&to=%s", currTo)
value := fmt.Sprintf("&value=%s", currAmount)
// https://decapi.me/misc/currency/?from=usd&to=usd&value=10
resp, err := http.Get(fmt.Sprint(basePath + from + to + value))
if err != nil {
return "", ErrInternalServerError
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", ErrInternalServerError
}
reply := string(body)
return reply, nil
}