code refactor
This commit is contained in:
@@ -55,11 +55,17 @@ func (a *ExchangeRatesApi) LatestExchangeRateHandler(c *core.WebContext) (any, *
|
||||
Timeout: time.Duration(a.CurrentConfig().ExchangeRatesRequestTimeout) * time.Millisecond,
|
||||
}
|
||||
|
||||
urls := dataSource.GetRequestUrls()
|
||||
exchangeRateResps := make([]*models.LatestExchangeRateResponse, 0, len(urls))
|
||||
requests, err := dataSource.BuildRequests()
|
||||
|
||||
for i := 0; i < len(urls); i++ {
|
||||
req, _ := http.NewRequest("GET", urls[i], nil)
|
||||
if err != nil {
|
||||
log.Errorf(c, "[exchange_rates.LatestExchangeRateHandler] failed to build requests for user \"uid:%d\", because %s", uid, err.Error())
|
||||
return nil, errs.ErrFailedToRequestRemoteApi
|
||||
}
|
||||
|
||||
exchangeRateResps := make([]*models.LatestExchangeRateResponse, 0, len(requests))
|
||||
|
||||
for i := 0; i < len(requests); i++ {
|
||||
req := requests[i]
|
||||
req.Header.Set("User-Agent", fmt.Sprintf("ezBookkeeping/%s ", settings.Version))
|
||||
|
||||
resp, err := client.Do(req)
|
||||
|
||||
@@ -3,6 +3,7 @@ package exchangerates
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -129,9 +130,15 @@ func (e *BankOfCanadaExchangeRateData) ToLatestExchangeRateResponse(c core.Conte
|
||||
return latestExchangeRateResp
|
||||
}
|
||||
|
||||
// GetRequestUrls returns the bank of Canada data source urls
|
||||
func (e *BankOfCanadaDataSource) GetRequestUrls() []string {
|
||||
return []string{bankOfCanadaExchangeRateUrl}
|
||||
// BuildRequests returns the bank of Canada exchange rates http requests
|
||||
func (e *BankOfCanadaDataSource) BuildRequests() ([]*http.Request, error) {
|
||||
req, err := http.NewRequest("GET", bankOfCanadaExchangeRateUrl, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []*http.Request{req}, nil
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the bank of Canada data source raw response
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"math"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/html/charset"
|
||||
@@ -123,9 +124,15 @@ func (e *BankOfIsraelExchangeRate) ToLatestExchangeRate(c core.Context) *models.
|
||||
}
|
||||
}
|
||||
|
||||
// GetRequestUrls returns the bank of Israel data source urls
|
||||
func (e *BankOfIsraelDataSource) GetRequestUrls() []string {
|
||||
return []string{bankOfIsraelExchangeRateUrl}
|
||||
// BuildRequests returns the bank of Israel exchange rates http requests
|
||||
func (e *BankOfIsraelDataSource) BuildRequests() ([]*http.Request, error) {
|
||||
req, err := http.NewRequest("GET", bankOfIsraelExchangeRateUrl, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []*http.Request{req}, nil
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the bank of Israel data source raw response
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"math"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -120,9 +121,15 @@ func (e *BankOfRussiaExchangeRate) ToLatestExchangeRate(c core.Context) *models.
|
||||
}
|
||||
}
|
||||
|
||||
// GetRequestUrls returns the bank of Russia data source urls
|
||||
func (e *BankOfRussiaDataSource) GetRequestUrls() []string {
|
||||
return []string{bankOfRussiaExchangeRateUrl}
|
||||
// BuildRequests returns the bank of Russia exchange rates http requests
|
||||
func (e *BankOfRussiaDataSource) BuildRequests() ([]*http.Request, error) {
|
||||
req, err := http.NewRequest("GET", bankOfRussiaExchangeRateUrl, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []*http.Request{req}, nil
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the bank of Russia data source raw response
|
||||
|
||||
@@ -2,6 +2,7 @@ package exchangerates
|
||||
|
||||
import (
|
||||
"math"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -27,9 +28,21 @@ type CzechNationalBankDataSource struct {
|
||||
ExchangeRatesDataSource
|
||||
}
|
||||
|
||||
// GetRequestUrls returns the czech nation bank data source urls
|
||||
func (e *CzechNationalBankDataSource) GetRequestUrls() []string {
|
||||
return []string{czechNationalBankMonthlyOtherExchangeRateUrl, czechNationalBankDailyExchangeRateUrl}
|
||||
// BuildRequests returns the Czech National Bank exchange rates http requests
|
||||
func (e *CzechNationalBankDataSource) BuildRequests() ([]*http.Request, error) {
|
||||
monthlyReq, err := http.NewRequest("GET", czechNationalBankMonthlyOtherExchangeRateUrl, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dailyReq, err := http.NewRequest("GET", czechNationalBankDailyExchangeRateUrl, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []*http.Request{monthlyReq, dailyReq}, nil
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the czech nation bank data source raw response
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"math"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/html/charset"
|
||||
@@ -131,9 +132,15 @@ func (e *DanmarksNationalbankExchangeRate) ToLatestExchangeRate(c core.Context)
|
||||
}
|
||||
}
|
||||
|
||||
// GetRequestUrls returns the Danmarks Nationalbank data source urls
|
||||
func (e *DanmarksNationalbankDataSource) GetRequestUrls() []string {
|
||||
return []string{danmarksNationalbankExchangeRateUrl}
|
||||
// BuildRequests returns the Danmarks Nationalbank exchange rates http requests
|
||||
func (e *DanmarksNationalbankDataSource) BuildRequests() ([]*http.Request, error) {
|
||||
req, err := http.NewRequest("GET", danmarksNationalbankExchangeRateUrl, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []*http.Request{req}, nil
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the Danmarks Nationalbank data source raw response
|
||||
|
||||
@@ -3,6 +3,7 @@ package exchangerates
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/html/charset"
|
||||
@@ -110,9 +111,15 @@ func (e *EuroCentralBankExchangeRate) ToLatestExchangeRate() *models.LatestExcha
|
||||
}
|
||||
}
|
||||
|
||||
// GetRequestUrls returns the euro central bank data source urls
|
||||
func (e *EuroCentralBankDataSource) GetRequestUrls() []string {
|
||||
return []string{euroCentralBankExchangeRateUrl}
|
||||
// BuildRequests returns the euro central bank exchange rates http requests
|
||||
func (e *EuroCentralBankDataSource) BuildRequests() ([]*http.Request, error) {
|
||||
req, err := http.NewRequest("GET", euroCentralBankExchangeRateUrl, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []*http.Request{req}, nil
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the euro central bank data source raw response
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
package exchangerates
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
"github.com/mayswind/ezbookkeeping/pkg/models"
|
||||
)
|
||||
|
||||
// ExchangeRatesDataSource defines the structure of exchange rates data source
|
||||
type ExchangeRatesDataSource interface {
|
||||
// GetRequestUrl returns the data source urls
|
||||
GetRequestUrls() []string
|
||||
// BuildRequests returns the http requests
|
||||
BuildRequests() ([]*http.Request, error)
|
||||
|
||||
// Parse returns the common response entity according to the data source raw response
|
||||
Parse(c core.Context, content []byte) (*models.LatestExchangeRateResponse, error)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package exchangerates
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -71,9 +72,15 @@ func init() {
|
||||
internationalMonetaryFundCurrencyNameCodeMap["Uruguayan peso"] = "UYU"
|
||||
}
|
||||
|
||||
// GetRequestUrls returns the international monetary fund data source urls
|
||||
func (e *InternationalMonetaryFundDataSource) GetRequestUrls() []string {
|
||||
return []string{internationalMonetaryFundExchangeRateUrl}
|
||||
// BuildRequests returns the international monetary fund exchange rates http requests
|
||||
func (e *InternationalMonetaryFundDataSource) BuildRequests() ([]*http.Request, error) {
|
||||
req, err := http.NewRequest("GET", internationalMonetaryFundExchangeRateUrl, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []*http.Request{req}, nil
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the international monetary fund data source raw response
|
||||
|
||||
@@ -3,6 +3,7 @@ package exchangerates
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/core"
|
||||
@@ -111,9 +112,15 @@ func (e *NationalBankOfGeorgiaExchangeRate) ToLatestExchangeRate(c core.Context)
|
||||
}
|
||||
}
|
||||
|
||||
// GetRequestUrls returns the national bank of Georgia data source urls
|
||||
func (e *NationalBankOfGeorgiaDataSource) GetRequestUrls() []string {
|
||||
return []string{nationalBankOfGeorgiaExchangeRateUrl}
|
||||
// BuildRequests returns the national bank of Georgia exchange rates http requests
|
||||
func (e *NationalBankOfGeorgiaDataSource) BuildRequests() ([]*http.Request, error) {
|
||||
req, err := http.NewRequest("GET", nationalBankOfGeorgiaExchangeRateUrl, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []*http.Request{req}, nil
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the national bank of Georgia data source raw response
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"math"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/html/charset"
|
||||
@@ -125,11 +126,28 @@ func (e *NationalBankOfPolandDataSource) GetRequestUrls() []string {
|
||||
return []string{nationalBankOfPolandInconvertibleCurrencyExchangeRateUrl, nationalBankOfPolandDailyExchangeRateUrl}
|
||||
}
|
||||
|
||||
// BuildRequests returns the national bank of Poland exchange rates http requests
|
||||
func (e *NationalBankOfPolandDataSource) BuildRequests() ([]*http.Request, error) {
|
||||
inconvertibleCurrencyReq, err := http.NewRequest("GET", nationalBankOfPolandInconvertibleCurrencyExchangeRateUrl, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dailyReq, err := http.NewRequest("GET", nationalBankOfPolandDailyExchangeRateUrl, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []*http.Request{inconvertibleCurrencyReq, dailyReq}, nil
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the National Bank of Poland data source raw response
|
||||
func (e *NationalBankOfPolandDataSource) Parse(c core.Context, content []byte) (*models.LatestExchangeRateResponse, error) {
|
||||
xmlDecoder := xml.NewDecoder(bytes.NewReader(content))
|
||||
xmlDecoder.CharsetReader = charset.NewReaderLabel
|
||||
|
||||
|
||||
nationalBankOfPolandData := &NationalBankOfPolandExchangeRateData{}
|
||||
err := xmlDecoder.Decode(nationalBankOfPolandData)
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"math"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/html/charset"
|
||||
@@ -159,9 +160,15 @@ func (e *NationalBankOfRomaniaExchangeRate) ToLatestExchangeRate(c core.Context)
|
||||
}
|
||||
}
|
||||
|
||||
// GetRequestUrls returns the national bank of Romania data source urls
|
||||
func (e *NationalBankOfRomaniaDataSource) GetRequestUrls() []string {
|
||||
return []string{nationalBankOfRomaniaExchangeRateUrl}
|
||||
// BuildRequests returns the national bank of Romania exchange rates http requests
|
||||
func (e *NationalBankOfRomaniaDataSource) BuildRequests() ([]*http.Request, error) {
|
||||
req, err := http.NewRequest("GET", nationalBankOfRomaniaExchangeRateUrl, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []*http.Request{req}, nil
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the national bank of Romania data source raw response
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"math"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/html/charset"
|
||||
@@ -155,9 +156,15 @@ func (e *NorgesBankExchangeRate) ToLatestExchangeRate(c core.Context, exchangeRa
|
||||
}
|
||||
}
|
||||
|
||||
// GetRequestUrls returns the Norges Bank data source urls
|
||||
func (e *NorgesBankDataSource) GetRequestUrls() []string {
|
||||
return []string{norgesBankExchangeRateUrl}
|
||||
// BuildRequests returns the Norges Bank exchange rates http requests
|
||||
func (e *NorgesBankDataSource) BuildRequests() ([]*http.Request, error) {
|
||||
req, err := http.NewRequest("GET", norgesBankExchangeRateUrl, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []*http.Request{req}, nil
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the Norges Bank data source raw response
|
||||
|
||||
@@ -3,6 +3,7 @@ package exchangerates
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/html/charset"
|
||||
@@ -125,9 +126,15 @@ func (e *ReserveBankOfAustraliaExchangeRate) ToLatestExchangeRate() *models.Late
|
||||
}
|
||||
}
|
||||
|
||||
// GetRequestUrls returns the the reserve bank of Australia data source urls
|
||||
func (e *ReserveBankOfAustraliaDataSource) GetRequestUrls() []string {
|
||||
return []string{reserveBankOfAustraliaExchangeRateUrl}
|
||||
// BuildRequests returns the reserve bank of Australia exchange rates http requests
|
||||
func (e *ReserveBankOfAustraliaDataSource) BuildRequests() ([]*http.Request, error) {
|
||||
req, err := http.NewRequest("GET", reserveBankOfAustraliaExchangeRateUrl, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []*http.Request{req}, nil
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the the reserve bank of Australia data source raw response
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"math"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/html/charset"
|
||||
@@ -185,9 +186,15 @@ func (e *SwissNationalBankExchangeRate) ToLatestExchangeRate(c core.Context) *mo
|
||||
}
|
||||
}
|
||||
|
||||
// GetRequestUrls returns the the reserve Swiss National Bank data source urls
|
||||
func (e *SwissNationalBankDataSource) GetRequestUrls() []string {
|
||||
return []string{swissNationalBankExchangeRateUrl}
|
||||
// BuildRequests returns the Swiss National Bank exchange rates http requests
|
||||
func (e *SwissNationalBankDataSource) BuildRequests() ([]*http.Request, error) {
|
||||
req, err := http.NewRequest("GET", swissNationalBankExchangeRateUrl, nil)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []*http.Request{req}, nil
|
||||
}
|
||||
|
||||
// Parse returns the common response entity according to the the reserve Swiss National Bank data source raw response
|
||||
|
||||
Reference in New Issue
Block a user