code refactor

This commit is contained in:
MaysWind
2024-11-17 13:04:07 +08:00
parent 69498003d8
commit 8f944b1b46
15 changed files with 159 additions and 43 deletions
+10 -4
View File
@@ -55,11 +55,17 @@ func (a *ExchangeRatesApi) LatestExchangeRateHandler(c *core.WebContext) (any, *
Timeout: time.Duration(a.CurrentConfig().ExchangeRatesRequestTimeout) * time.Millisecond, Timeout: time.Duration(a.CurrentConfig().ExchangeRatesRequestTimeout) * time.Millisecond,
} }
urls := dataSource.GetRequestUrls() requests, err := dataSource.BuildRequests()
exchangeRateResps := make([]*models.LatestExchangeRateResponse, 0, len(urls))
for i := 0; i < len(urls); i++ { if err != nil {
req, _ := http.NewRequest("GET", urls[i], 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)) req.Header.Set("User-Agent", fmt.Sprintf("ezBookkeeping/%s ", settings.Version))
resp, err := client.Do(req) resp, err := client.Do(req)
+10 -3
View File
@@ -3,6 +3,7 @@ package exchangerates
import ( import (
"encoding/json" "encoding/json"
"math" "math"
"net/http"
"strings" "strings"
"time" "time"
@@ -129,9 +130,15 @@ func (e *BankOfCanadaExchangeRateData) ToLatestExchangeRateResponse(c core.Conte
return latestExchangeRateResp return latestExchangeRateResp
} }
// GetRequestUrls returns the bank of Canada data source urls // BuildRequests returns the bank of Canada exchange rates http requests
func (e *BankOfCanadaDataSource) GetRequestUrls() []string { func (e *BankOfCanadaDataSource) BuildRequests() ([]*http.Request, error) {
return []string{bankOfCanadaExchangeRateUrl} 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 // Parse returns the common response entity according to the bank of Canada data source raw response
+10 -3
View File
@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/xml" "encoding/xml"
"math" "math"
"net/http"
"time" "time"
"golang.org/x/net/html/charset" "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 // BuildRequests returns the bank of Israel exchange rates http requests
func (e *BankOfIsraelDataSource) GetRequestUrls() []string { func (e *BankOfIsraelDataSource) BuildRequests() ([]*http.Request, error) {
return []string{bankOfIsraelExchangeRateUrl} 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 // Parse returns the common response entity according to the bank of Israel data source raw response
+10 -3
View File
@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/xml" "encoding/xml"
"math" "math"
"net/http"
"strings" "strings"
"time" "time"
@@ -120,9 +121,15 @@ func (e *BankOfRussiaExchangeRate) ToLatestExchangeRate(c core.Context) *models.
} }
} }
// GetRequestUrls returns the bank of Russia data source urls // BuildRequests returns the bank of Russia exchange rates http requests
func (e *BankOfRussiaDataSource) GetRequestUrls() []string { func (e *BankOfRussiaDataSource) BuildRequests() ([]*http.Request, error) {
return []string{bankOfRussiaExchangeRateUrl} 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 // Parse returns the common response entity according to the bank of Russia data source raw response
@@ -2,6 +2,7 @@ package exchangerates
import ( import (
"math" "math"
"net/http"
"strings" "strings"
"time" "time"
@@ -27,9 +28,21 @@ type CzechNationalBankDataSource struct {
ExchangeRatesDataSource ExchangeRatesDataSource
} }
// GetRequestUrls returns the czech nation bank data source urls // BuildRequests returns the Czech National Bank exchange rates http requests
func (e *CzechNationalBankDataSource) GetRequestUrls() []string { func (e *CzechNationalBankDataSource) BuildRequests() ([]*http.Request, error) {
return []string{czechNationalBankMonthlyOtherExchangeRateUrl, czechNationalBankDailyExchangeRateUrl} 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 // Parse returns the common response entity according to the czech nation bank data source raw response
@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/xml" "encoding/xml"
"math" "math"
"net/http"
"time" "time"
"golang.org/x/net/html/charset" "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 // BuildRequests returns the Danmarks Nationalbank exchange rates http requests
func (e *DanmarksNationalbankDataSource) GetRequestUrls() []string { func (e *DanmarksNationalbankDataSource) BuildRequests() ([]*http.Request, error) {
return []string{danmarksNationalbankExchangeRateUrl} 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 // Parse returns the common response entity according to the Danmarks Nationalbank data source raw response
@@ -3,6 +3,7 @@ package exchangerates
import ( import (
"bytes" "bytes"
"encoding/xml" "encoding/xml"
"net/http"
"time" "time"
"golang.org/x/net/html/charset" "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 // BuildRequests returns the euro central bank exchange rates http requests
func (e *EuroCentralBankDataSource) GetRequestUrls() []string { func (e *EuroCentralBankDataSource) BuildRequests() ([]*http.Request, error) {
return []string{euroCentralBankExchangeRateUrl} 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 // Parse returns the common response entity according to the euro central bank data source raw response
@@ -1,14 +1,16 @@
package exchangerates package exchangerates
import ( import (
"net/http"
"github.com/mayswind/ezbookkeeping/pkg/core" "github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/models" "github.com/mayswind/ezbookkeeping/pkg/models"
) )
// 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 urls // BuildRequests returns the http requests
GetRequestUrls() []string BuildRequests() ([]*http.Request, error)
// 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,6 +1,7 @@
package exchangerates package exchangerates
import ( import (
"net/http"
"strings" "strings"
"time" "time"
@@ -71,9 +72,15 @@ func init() {
internationalMonetaryFundCurrencyNameCodeMap["Uruguayan peso"] = "UYU" internationalMonetaryFundCurrencyNameCodeMap["Uruguayan peso"] = "UYU"
} }
// GetRequestUrls returns the international monetary fund data source urls // BuildRequests returns the international monetary fund exchange rates http requests
func (e *InternationalMonetaryFundDataSource) GetRequestUrls() []string { func (e *InternationalMonetaryFundDataSource) BuildRequests() ([]*http.Request, error) {
return []string{internationalMonetaryFundExchangeRateUrl} 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 // Parse returns the common response entity according to the international monetary fund data source raw response
@@ -3,6 +3,7 @@ package exchangerates
import ( import (
"encoding/json" "encoding/json"
"math" "math"
"net/http"
"time" "time"
"github.com/mayswind/ezbookkeeping/pkg/core" "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 // BuildRequests returns the national bank of Georgia exchange rates http requests
func (e *NationalBankOfGeorgiaDataSource) GetRequestUrls() []string { func (e *NationalBankOfGeorgiaDataSource) BuildRequests() ([]*http.Request, error) {
return []string{nationalBankOfGeorgiaExchangeRateUrl} 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 // Parse returns the common response entity according to the national bank of Georgia data source raw response
@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/xml" "encoding/xml"
"math" "math"
"net/http"
"time" "time"
"golang.org/x/net/html/charset" "golang.org/x/net/html/charset"
@@ -125,6 +126,23 @@ func (e *NationalBankOfPolandDataSource) GetRequestUrls() []string {
return []string{nationalBankOfPolandInconvertibleCurrencyExchangeRateUrl, nationalBankOfPolandDailyExchangeRateUrl} 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 // 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) { func (e *NationalBankOfPolandDataSource) Parse(c core.Context, content []byte) (*models.LatestExchangeRateResponse, error) {
xmlDecoder := xml.NewDecoder(bytes.NewReader(content)) xmlDecoder := xml.NewDecoder(bytes.NewReader(content))
@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/xml" "encoding/xml"
"math" "math"
"net/http"
"time" "time"
"golang.org/x/net/html/charset" "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 // BuildRequests returns the national bank of Romania exchange rates http requests
func (e *NationalBankOfRomaniaDataSource) GetRequestUrls() []string { func (e *NationalBankOfRomaniaDataSource) BuildRequests() ([]*http.Request, error) {
return []string{nationalBankOfRomaniaExchangeRateUrl} 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 // Parse returns the common response entity according to the national bank of Romania data source raw response
+10 -3
View File
@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/xml" "encoding/xml"
"math" "math"
"net/http"
"time" "time"
"golang.org/x/net/html/charset" "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 // BuildRequests returns the Norges Bank exchange rates http requests
func (e *NorgesBankDataSource) GetRequestUrls() []string { func (e *NorgesBankDataSource) BuildRequests() ([]*http.Request, error) {
return []string{norgesBankExchangeRateUrl} 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 // Parse returns the common response entity according to the Norges Bank data source raw response
@@ -3,6 +3,7 @@ package exchangerates
import ( import (
"bytes" "bytes"
"encoding/xml" "encoding/xml"
"net/http"
"time" "time"
"golang.org/x/net/html/charset" "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 // BuildRequests returns the reserve bank of Australia exchange rates http requests
func (e *ReserveBankOfAustraliaDataSource) GetRequestUrls() []string { func (e *ReserveBankOfAustraliaDataSource) BuildRequests() ([]*http.Request, error) {
return []string{reserveBankOfAustraliaExchangeRateUrl} 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 // Parse returns the common response entity according to the the reserve bank of Australia data source raw response
@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/xml" "encoding/xml"
"math" "math"
"net/http"
"time" "time"
"golang.org/x/net/html/charset" "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 // BuildRequests returns the Swiss National Bank exchange rates http requests
func (e *SwissNationalBankDataSource) GetRequestUrls() []string { func (e *SwissNationalBankDataSource) BuildRequests() ([]*http.Request, error) {
return []string{swissNationalBankExchangeRateUrl} 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 // Parse returns the common response entity according to the the reserve Swiss National Bank data source raw response