README
¶
Golang client for the Central Bank of the Russian Federation currency rates API
go-cbr-client is a fork of matperez's client and ivangile's client for CBRF API.
Request cache and different output types added.
Example
First, ensure the library is installed and up to date by running go get -u github.com/alexanderkmd/go-cbr-client
.
This is a very simple app that just displays exchange rate of US dollar.
package main
import (
"fmt"
"time"
cbr "github.com/alexanderkmd/go-cbr-client"
)
func main() {
client := cbr.NewClient()
// For float64 value:
rateFloat64, err := client.GetRate("USD", time.Now())
// For Decimal value:
rateDecimal, err := client.GetRateDecimal("USD", time.Now())
// For String value with dot as decimal separator:
rateString, err := client.GetRateString("USD", time.Now())
if err != nil {
panic(err)
}
fmt.Println(rateFloat64)
}
See main.go.
Set Alternative API point
Due to lots of IP bans from CBR (HTTP error 403) - you can change the different/alternative compatible API URL.
For example:: www.cbr-xml-daily.ru provide one.
package main
import (
"fmt"
"time"
cbr "github.com/alexanderkmd/go-cbr-client"
)
func main() {
client := cbr.NewClient()
client.SetBaseUrl("http://new-base-url.com")
// For float64 value:
rateFloat64, err := client.GetRate("USD", time.Now())
if err != nil {
panic(err)
}
fmt.Println(rateFloat64)
}
References
For more information check out the following links:
Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client interface { // Returns currency rate in float64 GetRate(string, time.Time) (float64, error) // Returns currency rate in Decimal // // Rationale: https://pkg.go.dev/github.com/shopspring/decimal - FAQ section GetRateDecimal(string, time.Time) (decimal.Decimal, error) // Returns currency rate string with dot as decimal separator GetRateString(string, time.Time) (string, error) // Returns currency struct GetCurrencyInfo(string, time.Time) (Currency, error) SetFetchFunction(fetchFunction) SetBaseUrl(string) error SetLogLevel(logrus.Level) }
Client is a currency rates service client... what else?
type Currency ¶
type Currency struct { ID string `xml:"ID,attr"` NumCode uint `xml:"NumCode"` CharCode string `xml:"CharCode"` Nom uint `xml:"Nominal"` Name string `xml:"Name"` Value string `xml:"Value"` }
Currency is a currency item
func (Currency) ValueDecimal ¶
Returns currency Value in Decimal, corrected by nominal
Rationale: https://pkg.go.dev/github.com/shopspring/decimal - FAQ section
func (Currency) ValueDecimalRaw ¶
Returns currency Value in Decimal, without nominal correction
Rationale: https://pkg.go.dev/github.com/shopspring/decimal - FAQ section
func (Currency) ValueFloat ¶
Returns currency Value in float64, corrected by nominal
func (Currency) ValueFloatRaw ¶
Returns currency Value in float64, without nominal correction
func (Currency) ValueString ¶
Returns properly formatted currency Value string