diff --git a/pkg/exchangerates/national_bank_of_poland_datasource.go b/pkg/exchangerates/national_bank_of_poland_datasource.go
index d3b7cb41..db2dd5e7 100644
--- a/pkg/exchangerates/national_bank_of_poland_datasource.go
+++ b/pkg/exchangerates/national_bank_of_poland_datasource.go
@@ -14,9 +14,9 @@ import (
"github.com/mayswind/ezbookkeeping/pkg/validators"
)
-const nationalBankOfPolandDailyExchangeRateUrl = "https://www.nbp.pl/kursy/xml/en/lastaen.xml"
-const nationalBankOfPolandInconvertibleCurrencyExchangeRateUrl = "https://www.nbp.pl/kursy/xml/en/lastben.xml"
-const nationalBankOfPolandExchangeRateReferenceUrl = "https://www.nbp.pl/homen.aspx?f=/kursy/kursyen.htm"
+const nationalBankOfPolandDailyExchangeRateUrl = "https://api.nbp.pl/api/exchangerates/tables/A?format=xml"
+const nationalBankOfPolandInconvertibleCurrencyExchangeRateUrl = "https://api.nbp.pl/api/exchangerates/tables/B?format=xml"
+const nationalBankOfPolandExchangeRateReferenceUrl = "https://nbp.pl/en/statistic-and-financial-reporting/rates/"
const nationalBankOfPolandDataSource = "Narodowy Bank Polski"
const nationalBankOfPolandBaseCurrency = "PLN"
@@ -30,16 +30,15 @@ type NationalBankOfPolandDataSource struct {
// NationalBankOfPolandExchangeRateData represents the whole data from National Bank of Poland
type NationalBankOfPolandExchangeRateData struct {
- XMLName xml.Name `xml:"exchange_rates"`
- Date string `xml:"date,attr"`
- AllExchangeRates []*NationalBankOfPolandExchangeRate `xml:"mid-rate"`
+ XMLName xml.Name `xml:"ArrayOfExchangeRatesTable"`
+ Date string `xml:"ExchangeRatesTable>EffectiveDate"`
+ AllExchangeRates []*NationalBankOfPolandExchangeRate `xml:"ExchangeRatesTable>Rates>Rate"`
}
// NationalBankOfPolandExchangeRate represents the exchange rate data from National Bank of Poland
type NationalBankOfPolandExchangeRate struct {
- Currency string `xml:"code,attr"`
- Units string `xml:"units,attr"`
- Rate string `xml:",chardata"`
+ Currency string `xml:"Code"`
+ Rate string `xml:"Mid"`
}
// ToLatestExchangeRateResponse returns a view-object according to original data from National Bank of Poland
@@ -95,13 +94,6 @@ func (e *NationalBankOfPolandExchangeRateData) ToLatestExchangeRateResponse(c *c
// ToLatestExchangeRate returns a data pair according to original data from National Bank of Poland
func (e *NationalBankOfPolandExchangeRate) ToLatestExchangeRate(c *core.Context) *models.LatestExchangeRate {
- amount, err := utils.StringToInt64(e.Units)
-
- if err != nil {
- log.WarnfWithRequestId(c, "[national_bank_of_poland_datasource.ToLatestExchangeRate] failed to parse amount, currency is %s, amount is %s", e.Currency, e.Units)
- return nil
- }
-
rate, err := utils.StringToFloat64(e.Rate)
if err != nil {
@@ -114,7 +106,7 @@ func (e *NationalBankOfPolandExchangeRate) ToLatestExchangeRate(c *core.Context)
return nil
}
- finalRate := float64(amount) / rate
+ finalRate := 1 / rate
if math.IsInf(finalRate, 0) {
return nil
diff --git a/pkg/exchangerates/national_bank_of_poland_datasource_test.go b/pkg/exchangerates/national_bank_of_poland_datasource_test.go
index 128a8117..5d2aba9d 100644
--- a/pkg/exchangerates/national_bank_of_poland_datasource_test.go
+++ b/pkg/exchangerates/national_bank_of_poland_datasource_test.go
@@ -10,11 +10,22 @@ import (
"github.com/mayswind/ezbookkeeping/pkg/models"
)
-const nationalBankOfPolandMinimumRequiredContent = "\n" +
- "\n" +
- " 3.8986\n" +
- " 0.5941\n" +
- ""
+const nationalBankOfPolandMinimumRequiredContent = "\n" +
+ "\n" +
+ " \n" +
+ " 2024-02-28\n" +
+ " \n" +
+ " \n" +
+ " USD\n" +
+ " 3.9922\n" +
+ " \n" +
+ " \n" +
+ " CNY\n" +
+ " 0.5545\n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ ""
func TestNationalBankOfPolandDataSource_StandardDataExtractBaseCurrency(t *testing.T) {
dataSource := &NationalBankOfPolandDataSource{}
@@ -37,11 +48,11 @@ func TestNationalBankOfPolandDataSource_StandardDataExtractExchangeRates(t *test
assert.Equal(t, nil, err)
assert.Contains(t, actualLatestExchangeRateResponse.ExchangeRates, &models.LatestExchangeRate{
Currency: "USD",
- Rate: "0.25650233417124096",
+ Rate: "0.2504884524823406",
})
assert.Contains(t, actualLatestExchangeRateResponse.ExchangeRates, &models.LatestExchangeRate{
Currency: "CNY",
- Rate: "1.68321831341525",
+ Rate: "1.8034265103697025",
})
}
@@ -61,7 +72,33 @@ func TestNationalBankOfPolandDataSource_OnlyXMLHeader(t *testing.T) {
Context: &gin.Context{},
}
- _, err := dataSource.Parse(context, []byte(""))
+ _, err := dataSource.Parse(context, []byte(""))
+ assert.NotEqual(t, nil, err)
+}
+
+func TestNationalBankOfPolandDataSource_EmptyArrayOfExchangeRatesTable(t *testing.T) {
+ dataSource := &NationalBankOfPolandDataSource{}
+ context := &core.Context{
+ Context: &gin.Context{},
+ }
+
+ _, err := dataSource.Parse(context, []byte("\n"+
+ "\n"+
+ ""))
+ assert.NotEqual(t, nil, err)
+}
+
+func TestNationalBankOfPolandDataSource_EmptyExchangeRatesTable(t *testing.T) {
+ dataSource := &NationalBankOfPolandDataSource{}
+ context := &core.Context{
+ Context: &gin.Context{},
+ }
+
+ _, err := dataSource.Parse(context, []byte("\n"+
+ "\n"+
+ " \n"+
+ " \n"+
+ ""))
assert.NotEqual(t, nil, err)
}
@@ -71,9 +108,14 @@ func TestNationalBankOfPolandDataSource_EmptyExchangeRatesContent(t *testing.T)
Context: &gin.Context{},
}
- _, err := dataSource.Parse(context, []byte("\n"+
- "\n"+
- ""))
+ _, err := dataSource.Parse(context, []byte("\n"+
+ "\n"+
+ " \n"+
+ " 2024-02-28\n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ ""))
assert.NotEqual(t, nil, err)
}
@@ -83,10 +125,18 @@ func TestNationalBankOfPolandDataSource_InvalidCurrency(t *testing.T) {
Context: &gin.Context{},
}
- actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("\n"+
- "\n"+
- " 1\n"+
- ""))
+ actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("\n"+
+ "\n"+
+ " \n"+
+ " 2024-02-28\n"+
+ " \n"+
+ " \n"+
+ " XXX\n"+
+ " 1\n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ ""))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
}
@@ -97,10 +147,18 @@ func TestNationalBankOfPolandDataSource_EmptyRate(t *testing.T) {
Context: &gin.Context{},
}
- actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("\n"+
- "\n"+
- " \n"+
- ""))
+ actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("\n"+
+ "\n"+
+ " \n"+
+ " 2024-02-28\n"+
+ " \n"+
+ " \n"+
+ " USD\n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ ""))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
}
@@ -111,10 +169,18 @@ func TestNationalBankOfPolandDataSource_InvalidRate(t *testing.T) {
Context: &gin.Context{},
}
- actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("\n"+
- "\n"+
- " null\n"+
- ""))
+ actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("\n"+
+ "\n"+
+ " \n"+
+ " 2024-02-28\n"+
+ " \n"+
+ " \n"+
+ " USD\n"+
+ " null\n"+
+ " \n"+
+ " \n"+
+ " \n"+
+ ""))
assert.Equal(t, nil, err)
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
}