mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-15 15:37:33 +08:00
refactor exchange rates data backend, supports multi data source, supports timeout
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
package exchangerates
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
|
||||
"github.com/mayswind/lab/pkg/core"
|
||||
"github.com/mayswind/lab/pkg/errs"
|
||||
"github.com/mayswind/lab/pkg/log"
|
||||
"github.com/mayswind/lab/pkg/models"
|
||||
)
|
||||
|
||||
const euroCentralBankExchangeRateUrl = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
|
||||
const euroCentralBankDataSource = "European Central Bank"
|
||||
const euroCentralBankBaseCurrency = "EUR"
|
||||
|
||||
// EuroCentralBankDataSource defines the structure of exchange rates data source of euro central bank
|
||||
type EuroCentralBankDataSource struct {
|
||||
ExchangeRatesDataSource
|
||||
}
|
||||
|
||||
// EuroCentralBankExchangeRateData represents the whole data from euro central bank
|
||||
type EuroCentralBankExchangeRateData struct {
|
||||
XMLName xml.Name `xml:"Envelope"`
|
||||
AllExchangeRates []*EuroCentralBankExchangeRates `xml:"Cube>Cube"`
|
||||
}
|
||||
|
||||
// EuroCentralBankExchangeRates represents the exchange rates data from euro central bank
|
||||
type EuroCentralBankExchangeRates struct {
|
||||
Date string `xml:"time,attr"`
|
||||
ExchangeRates []*EuroCentralBankExchangeRate `xml:"Cube"`
|
||||
}
|
||||
|
||||
// EuroCentralBankExchangeRate represents the exchange rate data from euro central bank
|
||||
type EuroCentralBankExchangeRate struct {
|
||||
Currency string `xml:"currency,attr"`
|
||||
Rate string `xml:"rate,attr"`
|
||||
}
|
||||
|
||||
// ToLatestExchangeRateResponse returns a view-object according to original data from euro central bank
|
||||
func (e *EuroCentralBankExchangeRateData) ToLatestExchangeRateResponse() *models.LatestExchangeRateResponse {
|
||||
if len(e.AllExchangeRates) < 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
latestEuroCentralBankExchangeRate := e.AllExchangeRates[0]
|
||||
|
||||
if len(latestEuroCentralBankExchangeRate.ExchangeRates) < 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
exchangeRates := make([]*models.LatestExchangeRate, len(latestEuroCentralBankExchangeRate.ExchangeRates))
|
||||
|
||||
for i := 0; i < len(latestEuroCentralBankExchangeRate.ExchangeRates); i++ {
|
||||
exchangeRates[i] = latestEuroCentralBankExchangeRate.ExchangeRates[i].ToLatestExchangeRate()
|
||||
}
|
||||
|
||||
latestExchangeRateResp := &models.LatestExchangeRateResponse{
|
||||
DataSource: euroCentralBankDataSource,
|
||||
Date: latestEuroCentralBankExchangeRate.Date,
|
||||
BaseCurrency: euroCentralBankBaseCurrency,
|
||||
ExchangeRates: exchangeRates,
|
||||
}
|
||||
|
||||
return latestExchangeRateResp
|
||||
}
|
||||
|
||||
// ToLatestExchangeRate returns a data pair according to original data from euro central bank
|
||||
func (e *EuroCentralBankExchangeRate) ToLatestExchangeRate() *models.LatestExchangeRate {
|
||||
return &models.LatestExchangeRate{
|
||||
Currency: e.Currency,
|
||||
Rate: e.Rate,
|
||||
}
|
||||
}
|
||||
|
||||
// GetRequestUrl returns the euro central bank data source url
|
||||
func (e *EuroCentralBankDataSource) GetRequestUrl() string {
|
||||
return euroCentralBankExchangeRateUrl
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the euro central bank data source raw response
|
||||
func (e *EuroCentralBankDataSource) Parse(c *core.Context, content []byte) (*models.LatestExchangeRateResponse, error) {
|
||||
euroCentralBankData := &EuroCentralBankExchangeRateData{}
|
||||
err := xml.Unmarshal(content, euroCentralBankData)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[euro_central_bank_datasource.Parse] failed to parse xml data, content is %s, because %s", string(content), err.Error())
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
latestExchangeRateResponse := euroCentralBankData.ToLatestExchangeRateResponse()
|
||||
|
||||
if latestExchangeRateResponse == nil {
|
||||
log.ErrorfWithRequestId(c, "[euro_central_bank_datasource.Parse] failed to parse latest exchange rate data, content is %s", string(content))
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
latestExchangeRateResponse.ExchangeRates = append(latestExchangeRateResponse.ExchangeRates, &models.LatestExchangeRate{
|
||||
Currency: euroCentralBankBaseCurrency,
|
||||
Rate: "1",
|
||||
})
|
||||
|
||||
return latestExchangeRateResponse, nil
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package exchangerates
|
||||
|
||||
import (
|
||||
"github.com/mayswind/lab/pkg/core"
|
||||
"github.com/mayswind/lab/pkg/models"
|
||||
)
|
||||
|
||||
// ExchangeRatesDataSource defines the structure of exchange rates data source
|
||||
type ExchangeRatesDataSource interface {
|
||||
// GetRequestUrl returns the data source url
|
||||
GetRequestUrl() string
|
||||
|
||||
// Parse returns the common response entity according to the data source raw response
|
||||
Parse(c *core.Context, content []byte) (*models.LatestExchangeRateResponse, error)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package exchangerates
|
||||
|
||||
import (
|
||||
"github.com/mayswind/lab/pkg/errs"
|
||||
"github.com/mayswind/lab/pkg/settings"
|
||||
)
|
||||
|
||||
// ExchangeRatesDataSourceContainer contains the current exchange rates data source
|
||||
type ExchangeRatesDataSourceContainer struct {
|
||||
Current ExchangeRatesDataSource
|
||||
}
|
||||
|
||||
// Initialize a exchange rates data source container singleton instance
|
||||
var (
|
||||
Container = &ExchangeRatesDataSourceContainer{}
|
||||
)
|
||||
|
||||
// InitializeExchangeRatesDataSource initializes the current exchange rates data source according to the config
|
||||
func InitializeExchangeRatesDataSource(config *settings.Config) error {
|
||||
if config.ExchangeRatesDataSource == settings.EuroCentralBankDataSource {
|
||||
Container.Current = &EuroCentralBankDataSource{}
|
||||
return nil
|
||||
}
|
||||
|
||||
return errs.ErrInvalidExchangeRatesDataSource
|
||||
}
|
||||
Reference in New Issue
Block a user