diff --git a/kubernetes-manifests/frontend.yaml b/kubernetes-manifests/frontend.yaml index 5cad3ed..8454bb4 100644 --- a/kubernetes-manifests/frontend.yaml +++ b/kubernetes-manifests/frontend.yaml @@ -42,8 +42,8 @@ spec: env: - name: ADDRESS value: ":8080" - - name: FREECURRENCYAPIKEY - value: "fca_live_VKZlykCWEiFcpBHnw74pzd4vLi04q1h9JySbVHDF" + - name: JSDELIVRAPIKEY + value: "prod" - name: CARTSERVICEHOST value: cartservice - name: PRODUCTCATALOGSERVICEHOST diff --git a/release/obd-kardinal.yaml b/release/obd-kardinal.yaml index 88ea69d..30fc303 100644 --- a/release/obd-kardinal.yaml +++ b/release/obd-kardinal.yaml @@ -127,8 +127,8 @@ spec: env: - name: ADDRESS value: ":8080" - - name: FREECURRENCYAPIKEY - value: "fca_live_nFVVF8CvfxqJhzMHB4N2x1NH7ffVVPwZr9hg3iNl" + - name: JSDELIVRAPIKEY + value: "prod" - name: CARTSERVICEHOST value: cartservice - name: PRODUCTCATALOGSERVICEHOST @@ -144,9 +144,9 @@ metadata: annotations: kardinal.dev.service/dependencies: "productcatalogservice:http,cartservice:http" kardinal.dev.service/plugins: | - - name: github.com/kurtosis-tech/free-currency-api-plugin + - name: github.com/kurtosis-tech/jsdelivr-api-plugin type: external - servicename: free-currency-api + servicename: jsdelivr-api args: api_key: "" spec: diff --git a/src/currencyexternalapi/config/jsdelivr/jsdelivr.go b/src/currencyexternalapi/config/jsdelivr/jsdelivr.go index f601ab1..49db42f 100644 --- a/src/currencyexternalapi/config/jsdelivr/jsdelivr.go +++ b/src/currencyexternalapi/config/jsdelivr/jsdelivr.go @@ -11,6 +11,7 @@ import ( const ( apiBaseURL = "https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/" + apiKeyQueryParamKey = "apikey" currenciesEndpointPath = "currencies.json" latestRatesEndpointPath = "currencies/usd.json" ) @@ -20,38 +21,59 @@ type LatestRatesResponse struct { Usd map[string]float64 `json:"usd"` } -var JsdelivrAPIConfig = config.NewCurrencyAPIConfig( - // saving the response for a week because app.freecurrencyapi.com has a low limit - // and this is a demo project, it's not important to have the latest data - 5*time.Second, - getGetCurrenciesURLFunc, - getGetLatestRatesURLFunc, - getCurrencyListFromResponseFunc, - getLatestRatesFromResponse, -) +func GetJsdelivrAPIConfig(apiKey string) *config.CurrencyAPIConfig { + var JsdelivrAPIConfig = config.NewCurrencyAPIConfig( + 5*time.Second, + getGetCurrenciesURLFunc(apiKey), + getGetLatestRatesURLFunc(apiKey), + getCurrencyListFromResponseFunc, + getLatestRatesFromResponse, + ) + return JsdelivrAPIConfig +} + +func getGetCurrenciesURLFunc(apiKey string) func() (*url.URL, error) { + + getCurrenciesURLFunc := func() (*url.URL, error) { + currenciesEndpointUrlStr := fmt.Sprintf("%s%s", apiBaseURL, currenciesEndpointPath) + + currenciesEndpointUrl, err := url.Parse(currenciesEndpointUrlStr) + if err != nil { + return nil, err + } -func getGetCurrenciesURLFunc() (*url.URL, error) { + currenciesEndpointQuery := currenciesEndpointUrl.Query() - currenciesEndpointUrlStr := fmt.Sprintf("%s%s", apiBaseURL, currenciesEndpointPath) + currenciesEndpointQuery.Set(apiKeyQueryParamKey, apiKey) - currenciesEndpointUrl, err := url.Parse(currenciesEndpointUrlStr) - if err != nil { - return nil, err + currenciesEndpointUrl.RawQuery = currenciesEndpointQuery.Encode() + + return currenciesEndpointUrl, nil } - return currenciesEndpointUrl, nil + return getCurrenciesURLFunc } -func getGetLatestRatesURLFunc(from string, to string) (*url.URL, error) { +func getGetLatestRatesURLFunc(apiKey string) func(string, string) (*url.URL, error) { + + getLatestRatesURLFunc := func(from string, to string) (*url.URL, error) { + latestRatesEndpointUrlStr := fmt.Sprintf("%s%s", apiBaseURL, latestRatesEndpointPath) + + latestRatesEndpointUrl, err := url.Parse(latestRatesEndpointUrlStr) + if err != nil { + return nil, err + } + + latestRatesEndpointQuery := latestRatesEndpointUrl.Query() + + latestRatesEndpointQuery.Set(apiKeyQueryParamKey, apiKey) - latestRatesEndpointUrlStr := fmt.Sprintf("%s%s", apiBaseURL, latestRatesEndpointPath) + latestRatesEndpointUrl.RawQuery = latestRatesEndpointQuery.Encode() - latestRatesEndpointUrl, err := url.Parse(latestRatesEndpointUrlStr) - if err != nil { - return nil, err + return latestRatesEndpointUrl, nil } - return latestRatesEndpointUrl, nil + return getLatestRatesURLFunc } func getCurrencyListFromResponseFunc(httpResponseBodyBytes []byte) ([]string, error) { diff --git a/src/frontend/currencyexternalservice/service.go b/src/frontend/currencyexternalservice/service.go index 7f035c4..5b89a0c 100644 --- a/src/frontend/currencyexternalservice/service.go +++ b/src/frontend/currencyexternalservice/service.go @@ -7,12 +7,11 @@ import ( ) type CurrencyExternalService struct { - primaryApi *currencyexternalapi.CurrencyAPI - secondaryApi *currencyexternalapi.CurrencyAPI + primaryApi *currencyexternalapi.CurrencyAPI } -func NewService(primaryApi *currencyexternalapi.CurrencyAPI, secondaryApi *currencyexternalapi.CurrencyAPI) *CurrencyExternalService { - return &CurrencyExternalService{primaryApi: primaryApi, secondaryApi: secondaryApi} +func NewService(primaryApi *currencyexternalapi.CurrencyAPI) *CurrencyExternalService { + return &CurrencyExternalService{primaryApi: primaryApi} } func (s *CurrencyExternalService) GetSupportedCurrencies(ctx context.Context) ([]string, error) { @@ -24,10 +23,7 @@ func (s *CurrencyExternalService) GetSupportedCurrencies(ctx context.Context) ([ currencyCodes, err = s.primaryApi.GetSupportedCurrencies(ctx) if err != nil { - currencyCodes, err = s.secondaryApi.GetSupportedCurrencies(ctx) - if err != nil { - return nil, err - } + return nil, err } return currencyCodes, nil @@ -43,12 +39,9 @@ func (s *CurrencyExternalService) Convert(ctx context.Context, fromCode string, err error ) - code, units, nanos, err = s.secondaryApi.Convert(ctx, fromCode, fromUnits, fromNanos, to) + code, units, nanos, err = s.primaryApi.Convert(ctx, fromCode, fromUnits, fromNanos, to) if err != nil { - code, units, nanos, err = s.secondaryApi.Convert(ctx, fromCode, fromUnits, fromNanos, to) - if err != nil { - return nil, err - } + return nil, err } money.CurrencyCode = &code diff --git a/src/frontend/currencyexternalservice/servicefactory.go b/src/frontend/currencyexternalservice/servicefactory.go index b60faa3..940f0f9 100644 --- a/src/frontend/currencyexternalservice/servicefactory.go +++ b/src/frontend/currencyexternalservice/servicefactory.go @@ -2,15 +2,11 @@ package currencyexternalservice import ( "github.com/kurtosis-tech/new-obd/src/currencyexternalapi" - "github.com/kurtosis-tech/new-obd/src/currencyexternalapi/config/freecurrency" - "github.com/kurtosis-tech/new-obd/src/currencyexternalapi/config/ghgist" + "github.com/kurtosis-tech/new-obd/src/currencyexternalapi/config/jsdelivr" ) func CreateService(apiKey string) *CurrencyExternalService { - primaryApi := currencyexternalapi.NewCurrencyAPI(freecurrency.GetFreeCurrencyAPIConfig(apiKey)) - secondaryApi := currencyexternalapi.NewCurrencyAPI(ghgist.GHGistCurrencyAPIConfig) - - service := NewService(primaryApi, secondaryApi) - + primaryApi := currencyexternalapi.NewCurrencyAPI(jsdelivr.GetJsdelivrAPIConfig(apiKey)) + service := NewService(primaryApi) return service } diff --git a/src/frontend/main.go b/src/frontend/main.go index c407993..cb83253 100644 --- a/src/frontend/main.go +++ b/src/frontend/main.go @@ -71,7 +71,7 @@ func main() { logrus.Fatal("An error occurred creating cart service client!\nError was: %s", err) } - apiKey := os.Getenv("FREECURRENCYAPIKEY") + apiKey := os.Getenv("JSDELIVRAPIKEY") svc := &frontendServer{ cartService: cartServiceClient,