Documentation ¶
Overview ¶
Package cbrapi implements basic functionality of API of Central Bank of Russia (cbr.ru). It allows to quote exchange rates of currencies in very simple steps
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrIncorrectCode is returned if currency code is not supported by the API ErrIncorrectCode = errors.New("Provided currency code is not known to the API") // ErrNoData is returned if rate for requested date(s) was not found ErrNoData = errors.New("API did not provide requested data") )
var ( BaseURL string = "https://www.cbr.ru/scripts/" EndpointSingleDate = BaseURL + "XML_daily.asp?date_req=%v" EndpointDateRange = BaseURL + "XML_dynamic.asp?date_req1=%v&date_req2=%v&VAL_NM_RQ=%v" EndpointCurrencyCodes = BaseURL + "XML_valFull.asp" )
Functions ¶
This section is empty.
Types ¶
type Currency ¶
type Currency struct { NameRUS string NameENG string APIID string Nominal int ISONumCode int ISOCharCode string }
Currency represents a currency known to Central Bank of Russia API Fields of this struct are almost identical to what API returns
func New ¶
New returns instance of Currency object which can be used to request exchange rate of currency from the Central Bank API with RateAtDate() and RateAtRangeDates() methods or QuoteAtDate() and QuoteAtRangeDates() In case of error New returns nil. Make sure to always check error.
func (*Currency) RateAtDate ¶
func (c *Currency) RateAtDate(date interface{}) (ExchangeRate, error)
RateAtDate accept either "DD/MM/YYYY" formatted date or time.Time object. It sends request to the API and returns ExchangeRate object
func (*Currency) RateAtRangeDates ¶
func (c *Currency) RateAtRangeDates(startdate, enddate interface{}) ([]ExchangeRate, error)
RateAtRangeDates accept either "DD/MM/YYYY" formatted date or time.Time object. It sends request to the API and returns slice of ExchangeRate objects
type ExchangeRate ¶
ExchangeRate represents exchange rate of a currency at specified date.
func QuoteAtDate ¶
func QuoteAtDate(ISOCode string, date interface{}) (ExchangeRate, error)
QuoteAtDate accepts ISO code of currency and either "DD/MM/YYYY" formatted date or time.Time object and returns ExchangeRate object
func QuoteAtRangeDates ¶
func QuoteAtRangeDates(ISOCode string, startdate, enddate interface{}) ([]ExchangeRate, error)
QuoteAtRangeDates accepts ISO code of currency and either "DD/MM/YYYY" formatted dates or time.Time objects where startdate and enddate are both inclusive and returns slice of ExchangeRate objects for corresponding dates
func (ExchangeRate) String ¶
func (r ExchangeRate) String() string
type ForeignCurrencyAPICodes ¶
type ForeignCurrencyAPICodes struct { XMLName xml.Name `xml:"Valuta"` Elements []ForeignCurrencyAPICodesItem `xml:"Item"` }
ForeignCurrencyAPICodes is a list of elements representing currencies known to the API of Central Bank.
type ForeignCurrencyAPICodesItem ¶
type ForeignCurrencyAPICodesItem struct { XMLName xml.Name `xml:"Item"` APIID string `xml:"ID,attr"` Name string `xml:"Name"` EngName string `xml:"EngName"` Nominal int `xml:"Nominal"` ISONumCode int `xml:"ISO_Num_Code"` ISOCharCode string `xml:"ISO_Char_Code"` }
ForeignCurrencyAPICodesItem represents XML structure of currency item returned by the API
type ResponseDaily ¶
type ResponseDaily struct { XMLName xml.Name `xml:"ValCurs"` Date string `xml:"Date,attr"` Elements []ResponseDailyElement `xml:"Valute"` }
ResponseDaily represents XML structure of response to request of singe date exchange rates from the endpoint This response contains all currencies rates as of the requested date
type ResponseDailyElement ¶
type ResponseDailyElement struct { XMLName xml.Name `xml:"Valute"` APIID string `xml:"ID,attr"` NumericCode int `xml:"NumCode"` CharacterCode string `xml:"CharCode"` Nominal int `xml:"Nominal"` Name string `xml:"Name"` Value string `xml:"Value"` }
ResponseDailyElement represents XML element of ResponseDaily
type ResponseRange ¶
type ResponseRange struct { XMLName xml.Name `xml:"ValCurs"` APIID string `xml:"ID,attr"` DateStart string `xml:"DateRange1,attr"` DateEnd string `xml:"DateRange2,attr"` Elements []ResponseRangeElement `xml:"Record"` }
ResponseRange list of exchange rates for requested dates