mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-19 17:24:26 +08:00
exchange rate data source supports requesting multi url
This commit is contained in:
+55
-14
@@ -3,12 +3,14 @@ package api
|
|||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mayswind/lab/pkg/core"
|
"github.com/mayswind/lab/pkg/core"
|
||||||
"github.com/mayswind/lab/pkg/errs"
|
"github.com/mayswind/lab/pkg/errs"
|
||||||
"github.com/mayswind/lab/pkg/exchangerates"
|
"github.com/mayswind/lab/pkg/exchangerates"
|
||||||
"github.com/mayswind/lab/pkg/log"
|
"github.com/mayswind/lab/pkg/log"
|
||||||
|
"github.com/mayswind/lab/pkg/models"
|
||||||
"github.com/mayswind/lab/pkg/settings"
|
"github.com/mayswind/lab/pkg/settings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,26 +36,65 @@ func (a *ExchangeRatesApi) LatestExchangeRateHandler(c *core.Context) (interface
|
|||||||
Timeout: time.Duration(settings.Container.Current.ExchangeRatesRequestTimeout) * time.Millisecond,
|
Timeout: time.Duration(settings.Container.Current.ExchangeRatesRequestTimeout) * time.Millisecond,
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := client.Get(dataSource.GetRequestUrl())
|
urls := dataSource.GetRequestUrls()
|
||||||
|
exchangeRateResps := make([]*models.LatestExchangeRateResponse, 0, len(urls))
|
||||||
|
|
||||||
if err != nil {
|
for i := 0; i < len(urls); i++ {
|
||||||
log.ErrorfWithRequestId(c, "[exchange_rates.LatestExchangeRateHandler] failed to request latest exchange rate data for user \"uid:%d\", because %s", uid, err.Error())
|
resp, err := client.Get(urls[i])
|
||||||
return nil, errs.ErrFailedToRequestRemoteApi
|
|
||||||
|
if err != nil {
|
||||||
|
log.ErrorfWithRequestId(c, "[exchange_rates.LatestExchangeRateHandler] failed to request latest exchange rate data for user \"uid:%d\", because %s", uid, err.Error())
|
||||||
|
return nil, errs.ErrFailedToRequestRemoteApi
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
log.ErrorfWithRequestId(c, "[exchange_rates.LatestExchangeRateHandler] failed to get latest exchange rate data response for user \"uid:%d\", because response code is not 200", uid)
|
||||||
|
return nil, errs.ErrFailedToRequestRemoteApi
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
exchangeRateResp, err := dataSource.Parse(c, body)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.ErrorfWithRequestId(c, "[exchange_rates.LatestExchangeRateHandler] failed to parse response for user \"uid:%d\", because %s", uid, err.Error())
|
||||||
|
return nil, errs.Or(err, errs.ErrFailedToRequestRemoteApi)
|
||||||
|
}
|
||||||
|
|
||||||
|
exchangeRateResps = append(exchangeRateResps, exchangeRateResp)
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != 200 {
|
lastExchangeRateResponse := exchangeRateResps[len(exchangeRateResps)-1]
|
||||||
log.ErrorfWithRequestId(c, "[exchange_rates.LatestExchangeRateHandler] failed to get latest exchange rate data response for user \"uid:%d\", because response code is not 200", uid)
|
allExchangeRatesMap := make(map[string]string)
|
||||||
return nil, errs.ErrFailedToRequestRemoteApi
|
|
||||||
|
for i := 0; i < len(exchangeRateResps); i++ {
|
||||||
|
exchangeRateResp := exchangeRateResps[i]
|
||||||
|
|
||||||
|
for j := 0; j < len(exchangeRateResp.ExchangeRates); j++ {
|
||||||
|
exchangeRate := exchangeRateResp.ExchangeRates[j]
|
||||||
|
allExchangeRatesMap[exchangeRate.Currency] = exchangeRate.Rate
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
allExchangeRatesMap[lastExchangeRateResponse.BaseCurrency] = "1"
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
allExchangeRates := make(models.LatestExchangeRateSlice, 0, len(allExchangeRatesMap))
|
||||||
latestExchangeRateResponse, err := dataSource.Parse(c, body)
|
|
||||||
|
|
||||||
if err != nil {
|
for currency, rate := range allExchangeRatesMap {
|
||||||
log.ErrorfWithRequestId(c, "[exchange_rates.LatestExchangeRateHandler] failed to parse response for user \"uid:%d\", because %s", uid, err.Error())
|
allExchangeRates = append(allExchangeRates, &models.LatestExchangeRate{
|
||||||
return nil, errs.Or(err, errs.ErrFailedToRequestRemoteApi)
|
Currency: currency,
|
||||||
|
Rate: rate,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return latestExchangeRateResponse, nil
|
sort.Sort(allExchangeRates)
|
||||||
|
|
||||||
|
finalExchangeRateResponse := &models.LatestExchangeRateResponse{
|
||||||
|
DataSource: lastExchangeRateResponse.DataSource,
|
||||||
|
ReferenceUrl: lastExchangeRateResponse.ReferenceUrl,
|
||||||
|
UpdateTime: lastExchangeRateResponse.UpdateTime,
|
||||||
|
BaseCurrency: lastExchangeRateResponse.BaseCurrency,
|
||||||
|
ExchangeRates: allExchangeRates,
|
||||||
|
}
|
||||||
|
|
||||||
|
return finalExchangeRateResponse, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,26 +42,30 @@ type EuroCentralBankExchangeRate struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ToLatestExchangeRateResponse returns a view-object according to original data from euro central bank
|
// ToLatestExchangeRateResponse returns a view-object according to original data from euro central bank
|
||||||
func (e *EuroCentralBankExchangeRateData) ToLatestExchangeRateResponse() *models.LatestExchangeRateResponse {
|
func (e *EuroCentralBankExchangeRateData) ToLatestExchangeRateResponse(c *core.Context) *models.LatestExchangeRateResponse {
|
||||||
if len(e.AllExchangeRates) < 1 {
|
if len(e.AllExchangeRates) < 1 {
|
||||||
|
log.ErrorfWithRequestId(c, "[euro_central_bank_datasource.ToLatestExchangeRateResponse] all exchange rates is empty")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
latestEuroCentralBankExchangeRate := e.AllExchangeRates[0]
|
latestEuroCentralBankExchangeRate := e.AllExchangeRates[0]
|
||||||
|
|
||||||
if len(latestEuroCentralBankExchangeRate.ExchangeRates) < 1 {
|
if len(latestEuroCentralBankExchangeRate.ExchangeRates) < 1 {
|
||||||
|
log.ErrorfWithRequestId(c, "[euro_central_bank_datasource.ToLatestExchangeRateResponse] exchange rates is empty")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
exchangeRates := make([]*models.LatestExchangeRate, len(latestEuroCentralBankExchangeRate.ExchangeRates))
|
exchangeRates := make(models.LatestExchangeRateSlice, 0, len(latestEuroCentralBankExchangeRate.ExchangeRates))
|
||||||
|
|
||||||
for i := 0; i < len(latestEuroCentralBankExchangeRate.ExchangeRates); i++ {
|
for i := 0; i < len(latestEuroCentralBankExchangeRate.ExchangeRates); i++ {
|
||||||
exchangeRates[i] = latestEuroCentralBankExchangeRate.ExchangeRates[i].ToLatestExchangeRate()
|
exchangeRate := latestEuroCentralBankExchangeRate.ExchangeRates[i]
|
||||||
|
exchangeRates = append(exchangeRates, exchangeRate.ToLatestExchangeRate())
|
||||||
}
|
}
|
||||||
|
|
||||||
timezone, err := time.LoadLocation(euroCentralBankDataUpdateDateTimezone)
|
timezone, err := time.LoadLocation(euroCentralBankDataUpdateDateTimezone)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.ErrorfWithRequestId(c, "[euro_central_bank_datasource.ToLatestExchangeRateResponse] failed to get timezone, timezone name is %s", euroCentralBankDataUpdateDateTimezone)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,6 +73,7 @@ func (e *EuroCentralBankExchangeRateData) ToLatestExchangeRateResponse() *models
|
|||||||
updateTime, err := time.ParseInLocation(euroCentralBankDataUpdateDateFormat, updateDateTime, timezone)
|
updateTime, err := time.ParseInLocation(euroCentralBankDataUpdateDateFormat, updateDateTime, timezone)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.ErrorfWithRequestId(c, "[euro_central_bank_datasource.ToLatestExchangeRateResponse] failed to parse update date, datetime is %s", updateDateTime)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,9 +96,9 @@ func (e *EuroCentralBankExchangeRate) ToLatestExchangeRate() *models.LatestExcha
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRequestUrl returns the euro central bank data source url
|
// GetRequestUrls returns the euro central bank data source urls
|
||||||
func (e *EuroCentralBankDataSource) GetRequestUrl() string {
|
func (e *EuroCentralBankDataSource) GetRequestUrls() []string {
|
||||||
return euroCentralBankExchangeRateUrl
|
return []string{euroCentralBankExchangeRateUrl}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse returns the common response entity according to the euro central bank data source raw response
|
// Parse returns the common response entity according to the euro central bank data source raw response
|
||||||
@@ -106,17 +111,12 @@ func (e *EuroCentralBankDataSource) Parse(c *core.Context, content []byte) (*mod
|
|||||||
return nil, errs.ErrFailedToRequestRemoteApi
|
return nil, errs.ErrFailedToRequestRemoteApi
|
||||||
}
|
}
|
||||||
|
|
||||||
latestExchangeRateResponse := euroCentralBankData.ToLatestExchangeRateResponse()
|
latestExchangeRateResponse := euroCentralBankData.ToLatestExchangeRateResponse(c)
|
||||||
|
|
||||||
if latestExchangeRateResponse == nil {
|
if latestExchangeRateResponse == nil {
|
||||||
log.ErrorfWithRequestId(c, "[euro_central_bank_datasource.Parse] failed to parse latest exchange rate data, content is %s", string(content))
|
log.ErrorfWithRequestId(c, "[euro_central_bank_datasource.Parse] failed to parse latest exchange rate data, content is %s", string(content))
|
||||||
return nil, errs.ErrFailedToRequestRemoteApi
|
return nil, errs.ErrFailedToRequestRemoteApi
|
||||||
}
|
}
|
||||||
|
|
||||||
latestExchangeRateResponse.ExchangeRates = append(latestExchangeRateResponse.ExchangeRates, &models.LatestExchangeRate{
|
|
||||||
Currency: euroCentralBankBaseCurrency,
|
|
||||||
Rate: "1",
|
|
||||||
})
|
|
||||||
|
|
||||||
return latestExchangeRateResponse, nil
|
return latestExchangeRateResponse, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import (
|
|||||||
|
|
||||||
// ExchangeRatesDataSource defines the structure of exchange rates data source
|
// ExchangeRatesDataSource defines the structure of exchange rates data source
|
||||||
type ExchangeRatesDataSource interface {
|
type ExchangeRatesDataSource interface {
|
||||||
// GetRequestUrl returns the data source url
|
// GetRequestUrl returns the data source urls
|
||||||
GetRequestUrl() string
|
GetRequestUrls() []string
|
||||||
|
|
||||||
// Parse returns the common response entity according to the data source raw response
|
// Parse returns the common response entity according to the data source raw response
|
||||||
Parse(c *core.Context, content []byte) (*models.LatestExchangeRateResponse, error)
|
Parse(c *core.Context, content []byte) (*models.LatestExchangeRateResponse, error)
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
// LatestExchangeRateResponse returns a view-object which contains latest exchange rate
|
// LatestExchangeRateResponse returns a view-object which contains latest exchange rate
|
||||||
type LatestExchangeRateResponse struct {
|
type LatestExchangeRateResponse struct {
|
||||||
DataSource string `json:"dataSource"`
|
DataSource string `json:"dataSource"`
|
||||||
ReferenceUrl string `json:"referenceUrl"`
|
ReferenceUrl string `json:"referenceUrl"`
|
||||||
UpdateTime int64 `json:"updateTime"`
|
UpdateTime int64 `json:"updateTime"`
|
||||||
BaseCurrency string `json:"baseCurrency"`
|
BaseCurrency string `json:"baseCurrency"`
|
||||||
ExchangeRates []*LatestExchangeRate `json:"exchangeRates"`
|
ExchangeRates LatestExchangeRateSlice `json:"exchangeRates"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// LatestExchangeRate represents a data pair of currency and exchange rate
|
// LatestExchangeRate represents a data pair of currency and exchange rate
|
||||||
@@ -14,3 +16,21 @@ type LatestExchangeRate struct {
|
|||||||
Currency string `json:"currency"`
|
Currency string `json:"currency"`
|
||||||
Rate string `json:"rate"`
|
Rate string `json:"rate"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LatestExchangeRateSlice represents the slice data structure of LatestExchangeRate
|
||||||
|
type LatestExchangeRateSlice []*LatestExchangeRate
|
||||||
|
|
||||||
|
// Len returns the count of items
|
||||||
|
func (c LatestExchangeRateSlice) Len() int {
|
||||||
|
return len(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Swap swaps two items
|
||||||
|
func (c LatestExchangeRateSlice) Swap(i, j int) {
|
||||||
|
c[i], c[j] = c[j], c[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Less reports whether the first item is less than the second one
|
||||||
|
func (c LatestExchangeRateSlice) Less(i, j int) bool {
|
||||||
|
return strings.Compare(c[i].Currency, c[j].Currency) < 0
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user