Categorygithub.com/alexanderkmd/go-cbr-client
modulepackage
0.0.0-20230429051223-344cf4893444
Repository: https://github.com/alexanderkmd/go-cbr-client.git
Documentation: pkg.go.dev

# 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:

# Functions

NewClient creates a new rates service instance.
SetLogger specifies the logger that the package should use.

# Structs

Currency is a currency item.
Result is a result representation.

# Interfaces

Client is a currency rates service client..
Logger is an interface that represents the required methods to log data.