Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace free currency external api with jsdelivr currency api #16

Merged
merged 6 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions kubernetes-manifests/frontend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions release/obd-kardinal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
64 changes: 43 additions & 21 deletions src/currencyexternalapi/config/jsdelivr/jsdelivr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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) {
Expand Down
19 changes: 6 additions & 13 deletions src/frontend/currencyexternalservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 3 additions & 7 deletions src/frontend/currencyexternalservice/servicefactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion src/frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading