diff --git a/pkg/api/exchange_rates.go b/pkg/api/exchange_rates.go index 3ff135f5..744db6e9 100644 --- a/pkg/api/exchange_rates.go +++ b/pkg/api/exchange_rates.go @@ -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) diff --git a/pkg/exchangerates/bank_of_canada_datasource.go b/pkg/exchangerates/bank_of_canada_datasource.go index 92c338e5..e05a9b6a 100644 --- a/pkg/exchangerates/bank_of_canada_datasource.go +++ b/pkg/exchangerates/bank_of_canada_datasource.go @@ -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 diff --git a/pkg/exchangerates/bank_of_israel_datasource.go b/pkg/exchangerates/bank_of_israel_datasource.go index d3e6e122..ac0c300b 100644 --- a/pkg/exchangerates/bank_of_israel_datasource.go +++ b/pkg/exchangerates/bank_of_israel_datasource.go @@ -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 diff --git a/pkg/exchangerates/bank_of_russia_datasource.go b/pkg/exchangerates/bank_of_russia_datasource.go index c072c47b..9e79f8e9 100644 --- a/pkg/exchangerates/bank_of_russia_datasource.go +++ b/pkg/exchangerates/bank_of_russia_datasource.go @@ -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 diff --git a/pkg/exchangerates/czech_national_bank_datasource.go b/pkg/exchangerates/czech_national_bank_datasource.go index 02ed4a55..8972a482 100644 --- a/pkg/exchangerates/czech_national_bank_datasource.go +++ b/pkg/exchangerates/czech_national_bank_datasource.go @@ -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 diff --git a/pkg/exchangerates/danmarks_national_bank_datasource.go b/pkg/exchangerates/danmarks_national_bank_datasource.go index 1abfee8e..ba9dbf6a 100644 --- a/pkg/exchangerates/danmarks_national_bank_datasource.go +++ b/pkg/exchangerates/danmarks_national_bank_datasource.go @@ -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 diff --git a/pkg/exchangerates/euro_central_bank_datasource.go b/pkg/exchangerates/euro_central_bank_datasource.go index a55ead0a..092bc75e 100644 --- a/pkg/exchangerates/euro_central_bank_datasource.go +++ b/pkg/exchangerates/euro_central_bank_datasource.go @@ -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 diff --git a/pkg/exchangerates/exchange_rates_datasource.go b/pkg/exchangerates/exchange_rates_datasource.go index eef78e67..b16caec3 100644 --- a/pkg/exchangerates/exchange_rates_datasource.go +++ b/pkg/exchangerates/exchange_rates_datasource.go @@ -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) diff --git a/pkg/exchangerates/international_monetary_fund_datasource.go b/pkg/exchangerates/international_monetary_fund_datasource.go index b74e6441..6fb13d58 100644 --- a/pkg/exchangerates/international_monetary_fund_datasource.go +++ b/pkg/exchangerates/international_monetary_fund_datasource.go @@ -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 diff --git a/pkg/exchangerates/national_bank_of_georgia_datasource.go b/pkg/exchangerates/national_bank_of_georgia_datasource.go index f8eeb005..febf7624 100644 --- a/pkg/exchangerates/national_bank_of_georgia_datasource.go +++ b/pkg/exchangerates/national_bank_of_georgia_datasource.go @@ -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 diff --git a/pkg/exchangerates/national_bank_of_poland_datasource.go b/pkg/exchangerates/national_bank_of_poland_datasource.go index ea7e60b9..43d2c1d3 100644 --- a/pkg/exchangerates/national_bank_of_poland_datasource.go +++ b/pkg/exchangerates/national_bank_of_poland_datasource.go @@ -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) diff --git a/pkg/exchangerates/national_bank_of_romania_datasource.go b/pkg/exchangerates/national_bank_of_romania_datasource.go index 483b6800..26a6cfdb 100644 --- a/pkg/exchangerates/national_bank_of_romania_datasource.go +++ b/pkg/exchangerates/national_bank_of_romania_datasource.go @@ -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 diff --git a/pkg/exchangerates/norges_bank_datasource.go b/pkg/exchangerates/norges_bank_datasource.go index 43be8a27..22985955 100644 --- a/pkg/exchangerates/norges_bank_datasource.go +++ b/pkg/exchangerates/norges_bank_datasource.go @@ -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 diff --git a/pkg/exchangerates/reserve_bank_of_australia_datasource.go b/pkg/exchangerates/reserve_bank_of_australia_datasource.go index ff384415..0eca300a 100644 --- a/pkg/exchangerates/reserve_bank_of_australia_datasource.go +++ b/pkg/exchangerates/reserve_bank_of_australia_datasource.go @@ -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 diff --git a/pkg/exchangerates/swiss_national_bank_datasource.go b/pkg/exchangerates/swiss_national_bank_datasource.go index 69da8c1a..669aa593 100644 --- a/pkg/exchangerates/swiss_national_bank_datasource.go +++ b/pkg/exchangerates/swiss_national_bank_datasource.go @@ -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