Skip to content

Commit

Permalink
fix: panic when there is no network connection
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed Feb 16, 2021
1 parent 8a8fd37 commit 7674c6e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 15 deletions.
4 changes: 2 additions & 2 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ func getReference(config Config, client resty.Client) (Reference, error) {
aggregatedLots := position.GetLots(config.Lots)
symbols := position.GetSymbols(config.Watchlist, aggregatedLots)

currencyRates := currency.GetCurrencyRates(client, symbols, config.Currency)
currencyRates, err := currency.GetCurrencyRates(client, symbols, config.Currency)

return Reference{
CurrencyRates: currencyRates,
}, nil
}, err

}

Expand Down
40 changes: 30 additions & 10 deletions internal/currency/currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,20 @@ func transformResponseCurrencies(responseQuotes []ResponseQuote) c.CurrencyRates

}

func getCurrencyRatesFromCurrencyPairSymbols(client resty.Client, currencyPairSymbols []string) c.CurrencyRates {
func getCurrencyRatesFromCurrencyPairSymbols(client resty.Client, currencyPairSymbols []string) (c.CurrencyRates, error) {

symbolsString := strings.Join(currencyPairSymbols, ",")
url := fmt.Sprintf("https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com&fields=regularMarketPrice,currency&symbols=%s", symbolsString)
res, _ := client.R().

res, err := client.R().
SetResult(Response{}).
Get(url)

return transformResponseCurrencies((res.Result().(*Response)).QuoteResponse.Quotes)
if err != nil {
return c.CurrencyRates{}, err
}

return transformResponseCurrencies((res.Result().(*Response)).QuoteResponse.Quotes), nil
}

func transformResponseCurrencyPairs(responseQuotes []ResponseQuote, targetCurrency string) []string {
Expand All @@ -79,29 +84,44 @@ func transformResponseCurrencyPairs(responseQuotes []ResponseQuote, targetCurren

}

func getCurrencyPairSymbols(client resty.Client, symbols []string, targetCurrency string) []string {
func getCurrencyPairSymbols(client resty.Client, symbols []string, targetCurrency string) ([]string, error) {

symbolsString := strings.Join(symbols, ",")
url := fmt.Sprintf("https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com&fields=regularMarketPrice,currency&symbols=%s", symbolsString)
res, _ := client.R().
res, err := client.R().
SetResult(Response{}).
Get(url)

return transformResponseCurrencyPairs((res.Result().(*Response)).QuoteResponse.Quotes, targetCurrency)
if err != nil {
return []string{}, err
}

return transformResponseCurrencyPairs((res.Result().(*Response)).QuoteResponse.Quotes, targetCurrency), nil
}

func GetCurrencyRates(client resty.Client, symbols []string, targetCurrency string) c.CurrencyRates {
func GetCurrencyRates(client resty.Client, symbols []string, targetCurrency string) (c.CurrencyRates, error) {

if targetCurrency == "" {
targetCurrency = "USD"
}

currencyPairSymbols := getCurrencyPairSymbols(client, symbols, targetCurrency)
currencyPairSymbols, err := getCurrencyPairSymbols(client, symbols, targetCurrency)

if err != nil {
return c.CurrencyRates{}, err
}

if len(currencyPairSymbols) <= 0 {
return c.CurrencyRates{}
return c.CurrencyRates{}, nil
}

currencyRates, err := getCurrencyRatesFromCurrencyPairSymbols(client, currencyPairSymbols)

if err != nil {
return c.CurrencyRates{}, err
}

return getCurrencyRatesFromCurrencyPairSymbols(client, currencyPairSymbols)
return currencyRates, nil
}

func GetCurrencyRateFromContext(ctx c.Context, fromCurrency string) (float64, float64, string) {
Expand Down
68 changes: 65 additions & 3 deletions internal/currency/currency_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package currency_test

import (
"net/http"

"github.com/jarcoal/httpmock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

Expand All @@ -15,7 +18,8 @@ var _ = Describe("Currency", func() {

MockResponse(ResponseParameters{Symbol: "VOW3.DE", Currency: "EUR", Price: 0.0})
MockResponse(ResponseParameters{Symbol: "EURUSD=X", Currency: "USD", Price: 1.2})
output := GetCurrencyRates(*client, []string{"VOW3.DE"}, "USD")
output, err := GetCurrencyRates(*client, []string{"VOW3.DE"}, "USD")
Expect(err).ToNot(HaveOccurred())
Expect(output).To(Equal(c.CurrencyRates{
"EUR": c.CurrencyRate{
FromCurrency: "EUR",
Expand All @@ -31,7 +35,8 @@ var _ = Describe("Currency", func() {

MockResponse(ResponseParameters{Symbol: "VOW3.DE", Currency: "EUR", Price: 0.0})
MockResponse(ResponseParameters{Symbol: "EURUSD=X", Currency: "USD", Price: 1.2})
output := GetCurrencyRates(*client, []string{"VOW3.DE"}, "")
output, err := GetCurrencyRates(*client, []string{"VOW3.DE"}, "")
Expect(err).ToNot(HaveOccurred())
Expect(output).To(Equal(c.CurrencyRates{
"EUR": c.CurrencyRate{
FromCurrency: "EUR",
Expand All @@ -47,7 +52,64 @@ var _ = Describe("Currency", func() {
It("returns an empty currency exchange rate list", func() {

MockResponse(ResponseParameters{Symbol: "VOW3.DE", Currency: "EUR", Price: 0.0})
output := GetCurrencyRates(*client, []string{"VOW3.DE"}, "EUR")
output, err := GetCurrencyRates(*client, []string{"VOW3.DE"}, "EUR")
Expect(err).ToNot(HaveOccurred())
Expect(output).To(Equal(c.CurrencyRates{}))

})
})

When("the request to get the currencies of each symbol fails", func() {
It("returns error", func() {

responseText := `{
"quoteResponse": {
"result": [
{
"regularMarketPrice": 1.2,
"currency": "EUR",
"symbol": "EURUSD=X"
}
],
"error": null
}
}`
responseUrl := "https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com&fields=regularMarketPrice,currency&symbols=EURUSD=X"
httpmock.RegisterResponder("GET", responseUrl, func(req *http.Request) (*http.Response, error) {
resp := httpmock.NewStringResponse(200, responseText)
resp.Header.Set("Content-Type", "application/json")
return resp, nil
})
output, err := GetCurrencyRates(*client, []string{"VOW3.DE"}, "EUR")
Expect(err).To(HaveOccurred())
Expect(output).To(Equal(c.CurrencyRates{}))

})
})

When("the request to the exchange rate", func() {
It("returns error", func() {

responseText := `{
"quoteResponse": {
"result": [
{
"regularMarketPrice": 160.0,
"currency": "EUR",
"symbol": "VOW3.DE"
}
],
"error": null
}
}`
responseUrl := "https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US&region=US&corsDomain=finance.yahoo.com&fields=regularMarketPrice,currency&symbols=VOW3.DE"
httpmock.RegisterResponder("GET", responseUrl, func(req *http.Request) (*http.Response, error) {
resp := httpmock.NewStringResponse(200, responseText)
resp.Header.Set("Content-Type", "application/json")
return resp, nil
})
output, err := GetCurrencyRates(*client, []string{"VOW3.DE"}, "USD")
Expect(err).To(HaveOccurred())
Expect(output).To(Equal(c.CurrencyRates{}))

})
Expand Down

0 comments on commit 7674c6e

Please sign in to comment.