cbrates

package module
v0.0.7 Latest Latest
Warning

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

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

README

About

A go module and a binary to get Russian Central Bank currency rates.

Run

go run github.com/itroot/cbrates/cmd/cbrates@latest

+----------------------------------------------------------+
| Курсы валют ЦБ РФ на 27.04.2023                          |
+------+-----+--------+----------------+---------+---------+
| CODE | NUM | AMOUNT | NAME           | VALUE   | FLOAT64 |
+------+-----+--------+----------------+---------+---------+
| USD  | 840 |      1 | Доллар США     | 81,6274 | 81.6274 |
| EUR  | 978 |      1 | Евро           | 90,1436 | 90.1436 |
| CNY  | 156 |      1 | Китайский юань | 11,7626 | 11.7626 |
+------+-----+--------+----------------+---------+---------+

go run github.com/itroot/cbrates/cmd/cbrates@latest -h

Usage of /tmp/go-build911405355/b001/exe/cbrates:
  -date string
    	Date to get rate values (default "27.04.2023")
  -filter string
    	Currencies codes to filter, empty string for all currencies (default "CNY,USD,EUR")
  -no-cache
    	Whether or not to cache requests
ivan@gbox:~/Desktop/itroot/projects/subm

Use

go get github.com/itroot/cbrates

An example could be found here.

Documentation

Overview

Package cbrates implements client for the API of Central Bank of Russia, described here: http://www.cbr.ru/development/sxml/ .

Example
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/itroot/cbrates"
)

func main() {
	client, err := cbrates.NewCachedClient(log.Default())
	if err != nil {
		log.Fatal(err)
	}

	rates, err := client.GetDailyRates(context.Background(), 2022, 5, 20)
	if err != nil {
		log.Fatal(err)
	}

	for _, valute := range rates.ValuteSeq {
		if valute.CharCode == "USD" {
			fmt.Println(valute.MustParseFloat64())
			break
		}
	}
}
Output:

62.4031

Index

Examples

Constants

View Source
const (
	DateFormat = "02.01.2006"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	// GetDailyRates implements a client to the http://www.cbr.ru/development/sxml/ for a single day request
	GetDailyRates(ctx context.Context, year int, month time.Month, day int) (*ValCurs, error)
}

Client is a interface for accessing the CBR API.

func NewCachedClient

func NewCachedClient(logger *log.Logger) (Client, error)

NewClient creates cache wrapper and a new client for the CBR API.

func NewClient

func NewClient(logger *log.Logger) (Client, error)

NewClient creates a new client for the CBR API.

type ValCurs

type ValCurs struct {
	Date      string    `xml:"Date,attr"`
	ValuteSeq []*Valute `xml:"Valute"`
}

type Valute

type Valute struct {
	ID       string  `xml:"ID,attr"`
	NumCode  int     `xml:"NumCode"`
	CharCode string  `xml:"CharCode"`
	Nominal  float64 `xml:"Nominal"`
	Name     string  `xml:"Name"`
	Value    string  `xml:"Value"`
}

func (*Valute) MustParseFloat64

func (v *Valute) MustParseFloat64() float64

MustParseFloat64 returns currency value in RUB in float64.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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