add National Bank of Georgia exchange rates data source
This commit is contained in:
@@ -44,6 +44,9 @@ func InitializeExchangeRatesDataSource(config *settings.Config) error {
|
||||
} else if config.ExchangeRatesDataSource == settings.NationalBankOfRomaniaDataSource {
|
||||
Container.Current = &NationalBankOfRomaniaDataSource{}
|
||||
return nil
|
||||
} else if config.ExchangeRatesDataSource == settings.NationalBankOfGeorgiaDataSource {
|
||||
Container.Current = &NationalBankOfGeorgiaDataSource{}
|
||||
return nil
|
||||
} else if config.ExchangeRatesDataSource == settings.InternationalMonetaryFundDataSource {
|
||||
Container.Current = &InternationalMonetaryFundDataSource{}
|
||||
return nil
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
package exchangerates
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"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 nationalBankOfGeorgiaExchangeRateUrl = "https://nbg.gov.ge/gw/api/ct/monetarypolicy/currencies/en/json"
|
||||
const nationalBankOfGeorgiaExchangeRateReferenceUrl = "https://nbg.gov.ge/en/monetary-policy/currency"
|
||||
const nationalBankOfGeorgiaDataSource = "National Bank of Georgia"
|
||||
const nationalBankOfGeorgiaBaseCurrency = "GEL"
|
||||
|
||||
const nationalBankOfGeorgiaUpdateDateFormat = "2006-01-02T15:04:05.999Z"
|
||||
|
||||
// NationalBankOfGeorgiaDataSource defines the structure of exchange rates data source of national bank of Georgia
|
||||
type NationalBankOfGeorgiaDataSource struct {
|
||||
ExchangeRatesDataSource
|
||||
}
|
||||
|
||||
// NationalBankOfGeorgiaExchangeRates represents the exchange rates data from national bank of Georgia
|
||||
type NationalBankOfGeorgiaExchangeRates struct {
|
||||
Date string `json:"date"`
|
||||
ExchangeRates []*NationalBankOfGeorgiaExchangeRate `json:"currencies"`
|
||||
}
|
||||
|
||||
// NationalBankOfGeorgiaExchangeRate represents the exchange rate data from national bank of Georgia
|
||||
type NationalBankOfGeorgiaExchangeRate struct {
|
||||
Currency string `json:"code"`
|
||||
Quantity float64 `json:"quantity"`
|
||||
Rate float64 `json:"rate"`
|
||||
Date string `json:"date"`
|
||||
}
|
||||
|
||||
// ToLatestExchangeRateResponse returns a view-object according to original data from national bank of Georgia
|
||||
func (e *NationalBankOfGeorgiaExchangeRates) ToLatestExchangeRateResponse(c core.Context) *models.LatestExchangeRateResponse {
|
||||
if len(e.ExchangeRates) < 1 {
|
||||
log.Errorf(c, "[national_bank_of_georgia_datasource.ToLatestExchangeRateResponse] exchange rates is empty")
|
||||
return nil
|
||||
}
|
||||
|
||||
exchangeRates := make(models.LatestExchangeRateSlice, 0, len(e.ExchangeRates))
|
||||
latestUpdateTime := int64(0)
|
||||
|
||||
for i := 0; i < len(e.ExchangeRates); i++ {
|
||||
exchangeRate := e.ExchangeRates[i]
|
||||
|
||||
if _, exists := validators.AllCurrencyNames[exchangeRate.Currency]; !exists {
|
||||
continue
|
||||
}
|
||||
|
||||
updateTime, err := time.Parse(nationalBankOfGeorgiaUpdateDateFormat, exchangeRate.Date)
|
||||
|
||||
if err != nil {
|
||||
log.Errorf(c, "[national_bank_of_georgia_datasource.ToLatestExchangeRateResponse] failed to parse update date, datetime is %s", exchangeRate.Date)
|
||||
return nil
|
||||
}
|
||||
|
||||
if updateTime.Unix() > latestUpdateTime {
|
||||
latestUpdateTime = updateTime.Unix()
|
||||
}
|
||||
|
||||
finalExchangeRate := exchangeRate.ToLatestExchangeRate(c)
|
||||
|
||||
if finalExchangeRate == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
exchangeRates = append(exchangeRates, finalExchangeRate)
|
||||
}
|
||||
|
||||
latestExchangeRateResp := &models.LatestExchangeRateResponse{
|
||||
DataSource: nationalBankOfGeorgiaDataSource,
|
||||
ReferenceUrl: nationalBankOfGeorgiaExchangeRateReferenceUrl,
|
||||
UpdateTime: latestUpdateTime,
|
||||
BaseCurrency: nationalBankOfGeorgiaBaseCurrency,
|
||||
ExchangeRates: exchangeRates,
|
||||
}
|
||||
|
||||
return latestExchangeRateResp
|
||||
}
|
||||
|
||||
// ToLatestExchangeRate returns a data pair according to original data from national bank of Georgia
|
||||
func (e *NationalBankOfGeorgiaExchangeRate) ToLatestExchangeRate(c core.Context) *models.LatestExchangeRate {
|
||||
if e.Rate <= 0 {
|
||||
log.Warnf(c, "[national_bank_of_georgia_datasource.ToLatestExchangeRate] rate is invalid, currency is %s, rate is %f", e.Currency, e.Rate)
|
||||
return nil
|
||||
}
|
||||
|
||||
if e.Quantity <= 0 {
|
||||
log.Warnf(c, "[national_bank_of_georgia_datasource.ToLatestExchangeRate] quantity is invalid, currency is %s, quantity is %f", e.Currency, e.Quantity)
|
||||
return nil
|
||||
}
|
||||
|
||||
finalRate := e.Quantity / e.Rate
|
||||
|
||||
if math.IsInf(finalRate, 0) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &models.LatestExchangeRate{
|
||||
Currency: e.Currency,
|
||||
Rate: utils.Float64ToString(finalRate),
|
||||
}
|
||||
}
|
||||
|
||||
// GetRequestUrls returns the national bank of Georgia data source urls
|
||||
func (e *NationalBankOfGeorgiaDataSource) GetRequestUrls() []string {
|
||||
return []string{nationalBankOfGeorgiaExchangeRateUrl}
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the national bank of Georgia data source raw response
|
||||
func (e *NationalBankOfGeorgiaDataSource) Parse(c core.Context, content []byte) (*models.LatestExchangeRateResponse, error) {
|
||||
nationalBankOfGeorgiaData := &[]*NationalBankOfGeorgiaExchangeRates{}
|
||||
err := json.Unmarshal(content, nationalBankOfGeorgiaData)
|
||||
|
||||
if err != nil {
|
||||
log.Errorf(c, "[national_bank_of_georgia_datasource.Parse] failed to parse xml data, content is %s, because %s", string(content), err.Error())
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
if nationalBankOfGeorgiaData == nil || len(*nationalBankOfGeorgiaData) < 1 {
|
||||
log.Errorf(c, "[national_bank_of_georgia_datasource.ToLatestExchangeRateResponse] all exchange rates is empty")
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
latestExchangeRateResponse := (*nationalBankOfGeorgiaData)[0].ToLatestExchangeRateResponse(c)
|
||||
|
||||
if latestExchangeRateResponse == nil {
|
||||
log.Errorf(c, "[national_bank_of_georgia_datasource.Parse] failed to parse latest exchange rate data, content is %s", string(content))
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
return latestExchangeRateResponse, nil
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
package exchangerates
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/models"
|
||||
)
|
||||
|
||||
const nationalBankOfGeorgiaMinimumRequiredContent = "[\n" +
|
||||
" {\n" +
|
||||
" \"date\": \"2024-11-16T00:00:00.000Z\",\n" +
|
||||
" \"currencies\": [\n" +
|
||||
" {\n" +
|
||||
" \"code\": \"JPY\",\n" +
|
||||
" \"quantity\": 100,\n" +
|
||||
" \"rate\": 1.7589,\n" +
|
||||
" \"date\": \"2024-11-15T17:01:11.702Z\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"code\": \"USD\",\n" +
|
||||
" \"quantity\": 1,\n" +
|
||||
" \"rate\": 2.7311,\n" +
|
||||
" \"date\": \"2024-11-15T17:01:11.702Z\"\n" +
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
" }\n" +
|
||||
"]"
|
||||
|
||||
func TestNationalBankOfGeorgiaDataSource_StandardDataExtractBaseCurrency(t *testing.T) {
|
||||
dataSource := &NationalBankOfGeorgiaDataSource{}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(nationalBankOfGeorgiaMinimumRequiredContent))
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, "GEL", actualLatestExchangeRateResponse.BaseCurrency)
|
||||
}
|
||||
|
||||
func TestNationalBankOfGeorgiaDataSource_StandardDataExtractUpdateTime(t *testing.T) {
|
||||
dataSource := &NationalBankOfGeorgiaDataSource{}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(nationalBankOfGeorgiaMinimumRequiredContent))
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, int64(1731690071), actualLatestExchangeRateResponse.UpdateTime)
|
||||
}
|
||||
|
||||
func TestNationalBankOfGeorgiaDataSource_StandardDataExtractExchangeRates(t *testing.T) {
|
||||
dataSource := &NationalBankOfGeorgiaDataSource{}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte(nationalBankOfGeorgiaMinimumRequiredContent))
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Contains(t, actualLatestExchangeRateResponse.ExchangeRates, &models.LatestExchangeRate{
|
||||
Currency: "JPY",
|
||||
Rate: "56.853715390300756",
|
||||
})
|
||||
assert.Contains(t, actualLatestExchangeRateResponse.ExchangeRates, &models.LatestExchangeRate{
|
||||
Currency: "USD",
|
||||
Rate: "0.366152832192157",
|
||||
})
|
||||
}
|
||||
|
||||
func TestNationalBankOfGeorgiaDataSource_BlankContent(t *testing.T) {
|
||||
dataSource := &NationalBankOfGeorgiaDataSource{}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte(""))
|
||||
assert.NotEqual(t, nil, err)
|
||||
}
|
||||
|
||||
func TestNationalBankOfGeorgiaDataSource_EmptyData(t *testing.T) {
|
||||
dataSource := &NationalBankOfGeorgiaDataSource{}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte("[]"))
|
||||
assert.NotEqual(t, nil, err)
|
||||
}
|
||||
|
||||
func TestNationalBankOfGeorgiaDataSource_EmptyExchangeRatesData(t *testing.T) {
|
||||
dataSource := &NationalBankOfGeorgiaDataSource{}
|
||||
context := core.NewNullContext()
|
||||
|
||||
_, err := dataSource.Parse(context, []byte("[{}]"))
|
||||
assert.NotEqual(t, nil, err)
|
||||
|
||||
_, err = dataSource.Parse(context, []byte("[\n"+
|
||||
" {\n"+
|
||||
" \"date\": \"2024-11-16T00:00:00.000Z\",\n"+
|
||||
" \"currencies\": [\n"+
|
||||
" ]\n"+
|
||||
" }\n"+
|
||||
"]"))
|
||||
assert.NotEqual(t, nil, err)
|
||||
}
|
||||
|
||||
func TestNationalBankOfGeorgiaDataSource_InvalidCurrency(t *testing.T) {
|
||||
dataSource := &NationalBankOfGeorgiaDataSource{}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("[\n"+
|
||||
" {\n"+
|
||||
" \"date\": \"2024-11-16T00:00:00.000Z\",\n"+
|
||||
" \"currencies\": [\n"+
|
||||
" {\n"+
|
||||
" \"code\": \"XXX\",\n"+
|
||||
" \"quantity\": 1,\n"+
|
||||
" \"rate\": 1,\n"+
|
||||
" \"date\": \"2024-11-15T17:01:11.702Z\"\n"+
|
||||
" }\n"+
|
||||
" ]\n"+
|
||||
" }\n"+
|
||||
"]"))
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
|
||||
}
|
||||
|
||||
func TestNationalBankOfGeorgiaDataSource_InvalidQuantity(t *testing.T) {
|
||||
dataSource := &NationalBankOfGeorgiaDataSource{}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("[\n"+
|
||||
" {\n"+
|
||||
" \"date\": \"2024-11-16T00:00:00.000Z\",\n"+
|
||||
" \"currencies\": [\n"+
|
||||
" {\n"+
|
||||
" \"code\": \"USD\",\n"+
|
||||
" \"quantity\": null,\n"+
|
||||
" \"rate\": 2.7311,\n"+
|
||||
" \"date\": \"2024-11-15T17:01:11.702Z\"\n"+
|
||||
" }\n"+
|
||||
" ]\n"+
|
||||
" }\n"+
|
||||
"]"))
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
|
||||
|
||||
actualLatestExchangeRateResponse, err = dataSource.Parse(context, []byte("[\n"+
|
||||
" {\n"+
|
||||
" \"date\": \"2024-11-16T00:00:00.000Z\",\n"+
|
||||
" \"currencies\": [\n"+
|
||||
" {\n"+
|
||||
" \"code\": \"USD\",\n"+
|
||||
" \"quantity\": 0,\n"+
|
||||
" \"rate\": 2.7311,\n"+
|
||||
" \"date\": \"2024-11-15T17:01:11.702Z\"\n"+
|
||||
" }\n"+
|
||||
" ]\n"+
|
||||
" }\n"+
|
||||
"]"))
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
|
||||
}
|
||||
|
||||
func TestNationalBankOfGeorgiaDataSource_InvalidRate(t *testing.T) {
|
||||
dataSource := &NationalBankOfGeorgiaDataSource{}
|
||||
context := core.NewNullContext()
|
||||
|
||||
actualLatestExchangeRateResponse, err := dataSource.Parse(context, []byte("[\n"+
|
||||
" {\n"+
|
||||
" \"date\": \"2024-11-16T00:00:00.000Z\",\n"+
|
||||
" \"currencies\": [\n"+
|
||||
" {\n"+
|
||||
" \"code\": \"USD\",\n"+
|
||||
" \"quantity\": 1,\n"+
|
||||
" \"rate\": null,\n"+
|
||||
" \"date\": \"2024-11-15T17:01:11.702Z\"\n"+
|
||||
" }\n"+
|
||||
" ]\n"+
|
||||
" }\n"+
|
||||
"]"))
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
|
||||
|
||||
actualLatestExchangeRateResponse, err = dataSource.Parse(context, []byte("[\n"+
|
||||
" {\n"+
|
||||
" \"date\": \"2024-11-16T00:00:00.000Z\",\n"+
|
||||
" \"currencies\": [\n"+
|
||||
" {\n"+
|
||||
" \"code\": \"USD\",\n"+
|
||||
" \"quantity\": 1,\n"+
|
||||
" \"rate\": 0,\n"+
|
||||
" \"date\": \"2024-11-15T17:01:11.702Z\"\n"+
|
||||
" }\n"+
|
||||
" ]\n"+
|
||||
" }\n"+
|
||||
"]"))
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Len(t, actualLatestExchangeRateResponse.ExchangeRates, 0)
|
||||
}
|
||||
Reference in New Issue
Block a user