add National Bank of Romania exchange rates data source

This commit is contained in:
MaysWind
2024-11-16 15:08:34 +08:00
parent 3ee1683349
commit bdbd4d5302
5 changed files with 425 additions and 0 deletions
+1
View File
@@ -348,6 +348,7 @@ custom_map_tile_server_default_zoom_level = 14
# "bank_of_israel": https://www.boi.org.il/en/economic-roles/financial-markets/exchange-rates/ # "bank_of_israel": https://www.boi.org.il/en/economic-roles/financial-markets/exchange-rates/
# "swiss_national_bank": https://www.snb.ch/en/the-snb/mandates-goals/statistics/statistics-pub/current_interest_exchange_rates # "swiss_national_bank": https://www.snb.ch/en/the-snb/mandates-goals/statistics/statistics-pub/current_interest_exchange_rates
# "danmarks_national_bank": https://www.nationalbanken.dk/en/what-we-do/stable-prices-monetary-policy-and-the-danish-economy/exchange-rates # "danmarks_national_bank": https://www.nationalbanken.dk/en/what-we-do/stable-prices-monetary-policy-and-the-danish-economy/exchange-rates
# "national_bank_of_romania": https://www.bnr.ro/Exchange-rates-1224.aspx
# "international_monetary_fund": https://www.imf.org/external/np/fin/data/param_rms_mth.aspx # "international_monetary_fund": https://www.imf.org/external/np/fin/data/param_rms_mth.aspx
data_source = euro_central_bank data_source = euro_central_bank
@@ -41,6 +41,9 @@ func InitializeExchangeRatesDataSource(config *settings.Config) error {
} else if config.ExchangeRatesDataSource == settings.DanmarksNationalbankDataSource { } else if config.ExchangeRatesDataSource == settings.DanmarksNationalbankDataSource {
Container.Current = &DanmarksNationalbankDataSource{} Container.Current = &DanmarksNationalbankDataSource{}
return nil return nil
} else if config.ExchangeRatesDataSource == settings.NationalBankOfRomaniaDataSource {
Container.Current = &NationalBankOfRomaniaDataSource{}
return nil
} else if config.ExchangeRatesDataSource == settings.InternationalMonetaryFundDataSource { } else if config.ExchangeRatesDataSource == settings.InternationalMonetaryFundDataSource {
Container.Current = &InternationalMonetaryFundDataSource{} Container.Current = &InternationalMonetaryFundDataSource{}
return nil return nil
@@ -0,0 +1,182 @@
package exchangerates
import (
"encoding/xml"
"math"
"time"
"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/errs"
"github.com/mayswind/ezbookkeeping/pkg/log"
"github.com/mayswind/ezbookkeeping/pkg/models"
"github.com/mayswind/ezbookkeeping/pkg/utils"
"github.com/mayswind/ezbookkeeping/pkg/validators"
)
const nationalBankOfRomaniaExchangeRateUrl = "https://www.bnr.ro/nbrfxrates.xml"
const nationalBankOfRomaniaExchangeRateReferenceUrl = "https://www.bnr.ro/Exchange-rates-1224.aspx"
const nationalBankOfRomaniaDataSource = "National Bank of Romania"
const nationalBankOfRomaniaUpdateDateFormat = "2006-01-02 15"
const nationalBankOfRomaniaUpdateDateTimezone = "Europe/Bucharest"
// NationalBankOfRomaniaDataSource defines the structure of exchange rates data source of national bank of Romania
type NationalBankOfRomaniaDataSource struct {
ExchangeRatesDataSource
}
// NationalBankOfRomaniaExchangeRateData represents the whole data from national bank of Romania
type NationalBankOfRomaniaExchangeRateData struct {
XMLName xml.Name `xml:"DataSet"`
Header *NationalBankOfRomaniaExchangeRateDataHeader `xml:"Header"`
Body *NationalBankOfRomaniaExchangeRateDataBody `xml:"Body"`
}
// NationalBankOfRomaniaExchangeRateDataHeader represents the header for exchange rates data of national bank of Romania
type NationalBankOfRomaniaExchangeRateDataHeader struct {
PublishingDate string `xml:"PublishingDate"`
}
// NationalBankOfRomaniaExchangeRateDataBody represents the body for exchange rates data of national bank of Romania
type NationalBankOfRomaniaExchangeRateDataBody struct {
OrigCurrency string `xml:"OrigCurrency"`
AllExchangeRates []*NationalBankOfRomaniaExchangeRates `xml:"Cube"`
}
// NationalBankOfRomaniaExchangeRates represents the exchange rates data from national bank of Romania
type NationalBankOfRomaniaExchangeRates struct {
Date string `xml:"date,attr"`
ExchangeRates []*NationalBankOfRomaniaExchangeRate `xml:"Rate"`
}
// NationalBankOfRomaniaExchangeRate represents the exchange rate data from national bank of Romania
type NationalBankOfRomaniaExchangeRate struct {
Currency string `xml:"currency,attr"`
Multiplier string `xml:"multiplier,attr"`
Rate string `xml:",chardata"`
}
// ToLatestExchangeRateResponse returns a view-object according to original data from national bank of Romania
func (e *NationalBankOfRomaniaExchangeRateData) ToLatestExchangeRateResponse(c core.Context) *models.LatestExchangeRateResponse {
if e.Header == nil || e.Body == nil {
log.Errorf(c, "[national_bank_of_romania_datasource.ToLatestExchangeRateResponse] header or body is empty")
return nil
}
if len(e.Body.AllExchangeRates) < 1 {
log.Errorf(c, "[national_bank_of_romania_datasource.ToLatestExchangeRateResponse] all exchange rates is empty")
return nil
}
latestNationalBankOfRomaniaExchangeRate := e.Body.AllExchangeRates[0]
if len(latestNationalBankOfRomaniaExchangeRate.ExchangeRates) < 1 {
log.Errorf(c, "[national_bank_of_romania_datasource.ToLatestExchangeRateResponse] exchange rates is empty")
return nil
}
exchangeRates := make(models.LatestExchangeRateSlice, 0, len(latestNationalBankOfRomaniaExchangeRate.ExchangeRates))
for i := 0; i < len(latestNationalBankOfRomaniaExchangeRate.ExchangeRates); i++ {
exchangeRate := latestNationalBankOfRomaniaExchangeRate.ExchangeRates[i]
if _, exists := validators.AllCurrencyNames[exchangeRate.Currency]; !exists {
continue
}
finalExchangeRate := exchangeRate.ToLatestExchangeRate(c)
if finalExchangeRate == nil {
continue
}
exchangeRates = append(exchangeRates, finalExchangeRate)
}
timezone, err := time.LoadLocation(nationalBankOfRomaniaUpdateDateTimezone)
if err != nil {
log.Errorf(c, "[national_bank_of_romania_datasource.ToLatestExchangeRateResponse] failed to get timezone, timezone name is %s", nationalBankOfRomaniaUpdateDateTimezone)
return nil
}
updateDateTime := e.Header.PublishingDate + " 13" // The data are updated in real time, shortly after 13:00, every banking day.
updateTime, err := time.ParseInLocation(nationalBankOfRomaniaUpdateDateFormat, updateDateTime, timezone)
if err != nil {
log.Errorf(c, "[national_bank_of_romania_datasource.ToLatestExchangeRateResponse] failed to parse update date, datetime is %s", updateDateTime)
return nil
}
latestExchangeRateResp := &models.LatestExchangeRateResponse{
DataSource: nationalBankOfRomaniaDataSource,
ReferenceUrl: nationalBankOfRomaniaExchangeRateReferenceUrl,
UpdateTime: updateTime.Unix(),
BaseCurrency: e.Body.OrigCurrency,
ExchangeRates: exchangeRates,
}
return latestExchangeRateResp
}
// ToLatestExchangeRate returns a data pair according to original data from national bank of Romania
func (e *NationalBankOfRomaniaExchangeRate) ToLatestExchangeRate(c core.Context) *models.LatestExchangeRate {
rate, err := utils.StringToFloat64(e.Rate)
if err != nil {
log.Warnf(c, "[national_bank_of_romania_datasource.ToLatestExchangeRate] failed to parse rate, currency is %s, rate is %s", e.Currency, e.Rate)
return nil
}
if rate <= 0 {
log.Warnf(c, "[national_bank_of_romania_datasource.ToLatestExchangeRate] rate is invalid, currency is %s, rate is %s", e.Currency, e.Rate)
return nil
}
unit := float64(1)
if e.Multiplier != "" {
unit, err = utils.StringToFloat64(e.Multiplier)
if err != nil || unit <= 0 {
log.Warnf(c, "[national_bank_of_romania_datasource.ToLatestExchangeRate] failed to parse unit, currency is %s, unit is %s", e.Currency, e.Multiplier)
return nil
}
}
finalRate := unit / rate
if math.IsInf(finalRate, 0) {
return nil
}
return &models.LatestExchangeRate{
Currency: e.Currency,
Rate: utils.Float64ToString(finalRate),
}
}
// GetRequestUrls returns the national bank of Romania data source urls
func (e *NationalBankOfRomaniaDataSource) GetRequestUrls() []string {
return []string{nationalBankOfRomaniaExchangeRateUrl}
}
// Parse returns the common response entity according to the national bank of Romania data source raw response
func (e *NationalBankOfRomaniaDataSource) Parse(c core.Context, content []byte) (*models.LatestExchangeRateResponse, error) {
nationalBankOfRomaniaData := &NationalBankOfRomaniaExchangeRateData{}
err := xml.Unmarshal(content, nationalBankOfRomaniaData)
if err != nil {
log.Errorf(c, "[national_bank_of_romania_datasource.Parse] failed to parse xml data, content is %s, because %s", string(content), err.Error())
return nil, errs.ErrFailedToRequestRemoteApi
}
latestExchangeRateResponse := nationalBankOfRomaniaData.ToLatestExchangeRateResponse(c)
if latestExchangeRateResponse == nil {
log.Errorf(c, "[national_bank_of_romania_datasource.Parse] failed to parse latest exchange rate data, content is %s", string(content))
return nil, errs.ErrFailedToRequestRemoteApi
}
return latestExchangeRateResponse, nil
}
@@ -0,0 +1,236 @@
package exchangerates
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/models"
)
const nationalBankOfRomaniaMinimumRequiredContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<DataSet xmlns=\"http://www.bnr.ro/xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.bnr.ro/xsd nbrfxrates.xsd\">\n" +
" <Header>\n" +
" <PublishingDate>2024-11-15</PublishingDate>\n" +
" </Header>\n" +
" <Body>\n" +
" <OrigCurrency>RON</OrigCurrency>\n" +
" <Cube date=\"2024-11-15\">\n" +
" <Rate currency=\"JPY\" multiplier=\"100\">3.0303</Rate>\n" +
" <Rate currency=\"USD\">4.7057</Rate>\n" +
" </Cube>\n" +
" </Body>\n" +
"</DataSet>"
func TestNationalBankOfRomaniaDataSource_StandardDataExtractBaseCurrency(t *testing.T) {
dataSource := &NationalBankOfRomaniaDataSource{}
context := core.NewNullContext()
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(nationalBankOfRomaniaMinimumRequiredContent))
assert.Equal(t, nil, err)
assert.Equal(t, "RON", actualLatestExchangeRateResponse.BaseCurrency)
}
func TestNationalBankOfRomaniaDataSource_StandardDataExtractUpdateTime(t *testing.T) {
dataSource := &NationalBankOfRomaniaDataSource{}
context := core.NewNullContext()
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(nationalBankOfRomaniaMinimumRequiredContent))
assert.Equal(t, nil, err)
assert.Equal(t, int64(1731668400), actualLatestExchangeRateResponse.UpdateTime)
}
func TestNationalBankOfRomaniaDataSource_StandardDataExtractExchangeRates(t *testing.T) {
dataSource := &NationalBankOfRomaniaDataSource{}
context := core.NewNullContext()
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(nationalBankOfRomaniaMinimumRequiredContent))
assert.Equal(t, nil, err)
assert.Contains(t, actualLatestExchangeRateResponse.ExchangeRates, &models.LatestExchangeRate{
Currency: "JPY",
Rate: "33.000033000033",
})
assert.Contains(t, actualLatestExchangeRateResponse.ExchangeRates, &models.LatestExchangeRate{
Currency: "USD",
Rate: "0.21250823469409438",
})
}
func TestNationalBankOfRomaniaDataSource_BlankContent(t *testing.T) {
dataSource := &NationalBankOfRomaniaDataSource{}
context := core.NewNullContext()
_, err := dataSource.Parse(context, []byte(""))
assert.NotEqual(t, nil, err)
}
func TestNationalBankOfRomaniaDataSource_OnlyXMLHeader(t *testing.T) {
dataSource := &NationalBankOfRomaniaDataSource{}
context := core.NewNullContext()
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>"))
assert.NotEqual(t, nil, err)
}
func TestNationalBankOfRomaniaDataSource_EmptyExchangeRatesDataset(t *testing.T) {
dataSource := &NationalBankOfRomaniaDataSource{}
context := core.NewNullContext()
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<DataSet xmlns=\"http://www.bnr.ro/xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.bnr.ro/xsd nbrfxrates.xsd\">"+
"</DataSet>"))
assert.NotEqual(t, nil, err)
}
func TestNationalBankOfRomaniaDataSource_NoDailyRatesHeader(t *testing.T) {
dataSource := &NationalBankOfRomaniaDataSource{}
context := core.NewNullContext()
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<DataSet xmlns=\"http://www.bnr.ro/xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.bnr.ro/xsd nbrfxrates.xsd\">"+
" <Header>\n"+
" <PublishingDate>2024-11-15</PublishingDate>\n"+
" </Header>\n"+
"</DataSet>"))
assert.NotEqual(t, nil, err)
}
func TestNationalBankOfRomaniaDataSource_NoDailyRatesBody(t *testing.T) {
dataSource := &NationalBankOfRomaniaDataSource{}
context := core.NewNullContext()
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<DataSet xmlns=\"http://www.bnr.ro/xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.bnr.ro/xsd nbrfxrates.xsd\">"+
" <Header>\n"+
" <PublishingDate>2024-11-15</PublishingDate>\n"+
" </Header>\n"+
"</DataSet>"))
assert.NotEqual(t, nil, err)
}
func TestNationalBankOfRomaniaDataSource_NoDailyRatesCube(t *testing.T) {
dataSource := &NationalBankOfRomaniaDataSource{}
context := core.NewNullContext()
_, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<DataSet xmlns=\"http://www.bnr.ro/xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.bnr.ro/xsd nbrfxrates.xsd\">"+
" <Header>\n"+
" <PublishingDate>2024-11-15</PublishingDate>\n"+
" </Header>\n"+
" <Body>\n"+
" <OrigCurrency>RON</OrigCurrency>\n"+
" </Body>\n"+
"</DataSet>"))
assert.NotEqual(t, nil, err)
}
func TestNationalBankOfRomaniaDataSource_InvalidCurrency(t *testing.T) {
dataSource := &NationalBankOfRomaniaDataSource{}
context := core.NewNullContext()
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<DataSet xmlns=\"http://www.bnr.ro/xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.bnr.ro/xsd nbrfxrates.xsd\">"+
" <Header>\n"+
" <PublishingDate>2024-11-15</PublishingDate>\n"+
" </Header>\n"+
" <Body>\n"+
" <OrigCurrency>RON</OrigCurrency>\n"+
" <Cube date=\"2024-11-15\">\n"+
" <Rate currency=\"XXX\">1</Rate>\n"+
" </Cube>\n"+
" </Body>\n"+
"</DataSet>"))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
}
func TestNationalBankOfRomaniaDataSource_EmptyRate(t *testing.T) {
dataSource := &NationalBankOfRomaniaDataSource{}
context := core.NewNullContext()
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<DataSet xmlns=\"http://www.bnr.ro/xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.bnr.ro/xsd nbrfxrates.xsd\">"+
" <Header>\n"+
" <PublishingDate>2024-11-15</PublishingDate>\n"+
" </Header>\n"+
" <Body>\n"+
" <OrigCurrency>RON</OrigCurrency>\n"+
" <Cube date=\"2024-11-15\">\n"+
" <Rate currency=\"USD\"></Rate>\n"+
" </Cube>\n"+
" </Body>\n"+
"</DataSet>"))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
}
func TestNationalBankOfRomaniaDataSource_InvalidRate(t *testing.T) {
dataSource := &NationalBankOfRomaniaDataSource{}
context := core.NewNullContext()
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<DataSet xmlns=\"http://www.bnr.ro/xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.bnr.ro/xsd nbrfxrates.xsd\">"+
" <Header>\n"+
" <PublishingDate>2024-11-15</PublishingDate>\n"+
" </Header>\n"+
" <Body>\n"+
" <OrigCurrency>RON</OrigCurrency>\n"+
" <Cube date=\"2024-11-15\">\n"+
" <Rate currency=\"USD\">null</Rate>\n"+
" </Cube>\n"+
" </Body>\n"+
"</DataSet>"))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
actualLatestExchangeRateResponse, err = dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<DataSet xmlns=\"http://www.bnr.ro/xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.bnr.ro/xsd nbrfxrates.xsd\">"+
" <Header>\n"+
" <PublishingDate>2024-11-15</PublishingDate>\n"+
" </Header>\n"+
" <Body>\n"+
" <OrigCurrency>RON</OrigCurrency>\n"+
" <Cube date=\"2024-11-15\">\n"+
" <Rate currency=\"USD\">0</Rate>\n"+
" </Cube>\n"+
" </Body>\n"+
"</DataSet>"))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
}
func TestNationalBankOfRomaniaDataSource_InvalidMultiplier(t *testing.T) {
dataSource := &NationalBankOfRomaniaDataSource{}
context := core.NewNullContext()
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<DataSet xmlns=\"http://www.bnr.ro/xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.bnr.ro/xsd nbrfxrates.xsd\">"+
" <Header>\n"+
" <PublishingDate>2024-11-15</PublishingDate>\n"+
" </Header>\n"+
" <Body>\n"+
" <OrigCurrency>RON</OrigCurrency>\n"+
" <Cube date=\"2024-11-15\">\n"+
" <Rate currency=\"JPY\" multiplier=\"null\">3.0303</Rate>\n"+
" </Cube>\n"+
" </Body>\n"+
"</DataSet>"))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
actualLatestExchangeRateResponse, err = dataSource.Parse(context, []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<DataSet xmlns=\"http://www.bnr.ro/xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.bnr.ro/xsd nbrfxrates.xsd\">"+
" <Header>\n"+
" <PublishingDate>2024-11-15</PublishingDate>\n"+
" </Header>\n"+
" <Body>\n"+
" <OrigCurrency>RON</OrigCurrency>\n"+
" <Cube date=\"2024-11-15\">\n"+
" <Rate currency=\"JPY\" multiplier=\"0\">3.0303</Rate>\n"+
" </Cube>\n"+
" </Body>\n"+
"</DataSet>"))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
}
+3
View File
@@ -108,6 +108,7 @@ const (
BankOfIsraelDataSource string = "bank_of_israel" BankOfIsraelDataSource string = "bank_of_israel"
SwissNationalBankDataSource string = "swiss_national_bank" SwissNationalBankDataSource string = "swiss_national_bank"
DanmarksNationalbankDataSource string = "danmarks_national_bank" DanmarksNationalbankDataSource string = "danmarks_national_bank"
NationalBankOfRomaniaDataSource string = "national_bank_of_romania"
InternationalMonetaryFundDataSource string = "international_monetary_fund" InternationalMonetaryFundDataSource string = "international_monetary_fund"
) )
@@ -896,6 +897,8 @@ func loadExchangeRatesConfiguration(config *Config, configFile *ini.File, sectio
config.ExchangeRatesDataSource = SwissNationalBankDataSource config.ExchangeRatesDataSource = SwissNationalBankDataSource
} else if dataSource == DanmarksNationalbankDataSource { } else if dataSource == DanmarksNationalbankDataSource {
config.ExchangeRatesDataSource = DanmarksNationalbankDataSource config.ExchangeRatesDataSource = DanmarksNationalbankDataSource
} else if dataSource == NationalBankOfRomaniaDataSource {
config.ExchangeRatesDataSource = NationalBankOfRomaniaDataSource
} else if dataSource == InternationalMonetaryFundDataSource { } else if dataSource == InternationalMonetaryFundDataSource {
config.ExchangeRatesDataSource = InternationalMonetaryFundDataSource config.ExchangeRatesDataSource = InternationalMonetaryFundDataSource
} else { } else {