cbr

package module
v0.0.0-...-344cf48 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2023 License: MIT Imports: 14 Imported by: 0

README

Golang client for the Central Bank of the Russian Federation currency rates API

Go Reference Test Codecov

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

func SetLogger

func SetLogger(logger Logger) error

SetLogger specifies the logger that the package should use.

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?

func NewClient

func NewClient() Client

NewClient creates a new rates service instance

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

func (cur Currency) ValueDecimal() (decimal.Decimal, error)

Returns currency Value in Decimal, corrected by nominal

Rationale: https://pkg.go.dev/github.com/shopspring/decimal - FAQ section

func (Currency) ValueDecimalRaw

func (cur Currency) ValueDecimalRaw() (decimal.Decimal, error)

Returns currency Value in Decimal, without nominal correction

Rationale: https://pkg.go.dev/github.com/shopspring/decimal - FAQ section

func (Currency) ValueFloat

func (cur Currency) ValueFloat() (float64, error)

Returns currency Value in float64, corrected by nominal

func (Currency) ValueFloatRaw

func (cur Currency) ValueFloatRaw() (float64, error)

Returns currency Value in float64, without nominal correction

func (Currency) ValueString

func (cur Currency) ValueString() string

Returns properly formatted currency Value string

type Logger

type Logger interface {
	Println(v ...interface{})
	Printf(format string, v ...interface{})
}

Logger is an interface that represents the required methods to log data.

type Result

type Result struct {
	XMLName    xml.Name   `xml:"ValCurs"`
	Date       string     `xml:"Date,attr"`
	Currencies []Currency `xml:"Valute"`
}

Result is a result representation

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL