Skip to content

Commit

Permalink
Wrong method implementation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
geovannyAvelar committed Nov 4, 2022
1 parent b9b3ccf commit ab01c02
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 28 deletions.
37 changes: 35 additions & 2 deletions brapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ type StockApiResponse struct {
Stocks []Stock `json:"stocks"`
}

type QuoteApiResponse struct {
Results []Quote `json:"results"`
}

type TickerApiResponse struct {
Stocks []string `json:"stocks"`
}
Expand All @@ -40,10 +44,18 @@ type Stock struct {
Sector string `json:"sector"`
}

func (a BrApi) FindAssetByTicker(tickers ...string) ([]Stock, error) {
type Quote struct {
Symbol string `json:"symbol"`
ShortName string `json:"shortName"`
LongName string `json:"LongName"`
Currency string `json:"Currency"`
RegularMarketPrice float64 `json:"RegularMarketPrice"`
}

func (a BrApi) FindAssetByTicker(tickers ...string) ([]Quote, error) {
tickersParam := strings.Join(tickers, ",")
resp, err := http.Get(a.baseUrl + "/api/quote/" + tickersParam)
return parseStockResponse(resp, err)
return parseQuoteResponse(resp, err)
}

func (a BrApi) SearchTickets(keyword string) ([]string, error) {
Expand Down Expand Up @@ -107,3 +119,24 @@ func parseStockResponse(resp *http.Response, err error) ([]Stock, error) {

return stocksData.Stocks, nil
}

func parseQuoteResponse(resp *http.Response, err error) ([]Quote, error) {
if err != nil {
return nil, err
}

responseData, err := ioutil.ReadAll(resp.Body)

if err != nil {
return nil, err
}

quoteData := QuoteApiResponse{}
err = json.Unmarshal(responseData, &quoteData)

if err != nil {
return nil, err
}

return quoteData.Results, nil
}
34 changes: 10 additions & 24 deletions brapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,26 @@ func TestFindAssetByTicker(t *testing.T) {

brApi := NewBrApiWithCustomBaseUrl(testApiServer.URL)

stocks, err := brApi.FindAssetByTicker("PETR3")
quotes, err := brApi.FindAssetByTicker("PETR4", "ITUB3")

if err != nil {
t.Errorf("Error in /api/quote/PETR3 request. Cause %s", err.Error())
t.Errorf("Error during /api/quote/PETR4,ITUB3. Cause: %s", err.Error())
}

var petr3Stock *Stock
var petr4Quote *Quote
var itub3Quote *Quote

for _, stock := range stocks {
if stock.Stock == "PETR3" {
petr3Stock = &stock
}
}

if petr3Stock == nil {
t.Errorf("Error in /api/quote/PETR3 request. Could not find PETR3")
}

stocks, err = brApi.FindAssetByTicker("PETR4,ITUB3")

var petr4Stock *Stock
var itub3Stock *Stock

for _, stock := range stocks {
if stock.Stock == "PETR4" {
petr4Stock = &stock
for _, quote := range quotes {
if quote.Symbol == "PETR4" {
petr4Quote = &quote
}

if stock.Stock == "ITUB3" {
itub3Stock = &stock
if quote.Symbol == "ITUB3" {
itub3Quote = &quote
}
}

if petr4Stock == nil || itub3Stock == nil {
if petr4Quote == nil || itub3Quote == nil {
t.Errorf("Error in /api/quote/PETR4,ITUB3 request. Could not find PETR4 or ITUB3")
}
}
Expand Down
17 changes: 15 additions & 2 deletions brapi_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ var endpoints map[string]handler = map[string]handler{
return json.Marshal(StockApiResponse{Stocks: stocksFound})
},
"/api/quote/PETR4,ITUB3": func(r *http.Request) ([]byte, error) {
stocksFound, err := searchAssetsByTicker("PETR4,ITUB3")
quotes, err := loadQuoteData()

if err != nil {
return nil, err
}

return json.Marshal(StockApiResponse{Stocks: stocksFound})
return json.Marshal(quotes)
},
"/api/available": func(r *http.Request) ([]byte, error) {
keyword := r.URL.Query().Get("search")
Expand Down Expand Up @@ -126,3 +126,16 @@ func loadAllAssets() (*StockApiResponse, error) {

return &stocksData, err
}

func loadQuoteData() (*QuoteApiResponse, error) {
data, err := os.ReadFile("test-data/quote_data.json")

if err != nil {
return nil, err
}

quoteData := QuoteApiResponse{}
err = json.Unmarshal(data, &quoteData)

return &quoteData, err
}
69 changes: 69 additions & 0 deletions test-data/quote_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"results": [
{
"symbol": "PETR4",
"shortName": "PETROBRAS PN N2",
"longName": "Petróleo Brasileiro S.A. - Petrobras",
"currency": "BRL",
"regularMarketPrice": 28.3,
"regularMarketDayHigh": 30.9,
"regularMarketDayLow": 28.18,
"regularMarketDayRange": "28.18 - 30.9",
"regularMarketChange": -1.6500015,
"regularMarketChangePercent": -5.5091867,
"regularMarketTime": "2022-11-04T20:07:52.000Z",
"marketCap": 393907666944,
"regularMarketVolume": 193762100,
"regularMarketPreviousClose": 29.95,
"regularMarketOpen": 30.8,
"averageDailyVolume10Day": 141597577,
"averageDailyVolume3Month": 95189864,
"fiftyTwoWeekLowChange": 2.58,
"fiftyTwoWeekLowChangePercent": 0.10031104,
"fiftyTwoWeekRange": "25.72 - 38.39",
"fiftyTwoWeekHighChange": -10.09,
"fiftyTwoWeekHighChangePercent": -0.26282886,
"fiftyTwoWeekLow": 25.72,
"fiftyTwoWeekHigh": 38.39,
"twoHundredDayAverage": 31.81595,
"twoHundredDayAverageChange": -3.5159512,
"twoHundredDayAverageChangePercent": -0.110509075,
"priceEarnings": 2.41848229,
"earningsPerShare": 12.3838169,
"logourl": "https://s3-symbol-logo.tradingview.com/brasileiro-petrobras--big.svg"
},
{
"symbol": "ITUB3",
"shortName": "ITAUUNIBANCOON N1",
"longName": "Itaú Unibanco Holding S.A.",
"currency": "BRL",
"regularMarketPrice": 25.43,
"regularMarketDayHigh": 25.87,
"regularMarketDayLow": 25.36,
"regularMarketDayRange": "25.36 - 25.87",
"regularMarketChange": 0.069999695,
"regularMarketChangePercent": 0.27602404,
"regularMarketTime": "2022-11-04T20:07:00.000Z",
"marketCap": 272932552704,
"regularMarketVolume": 604500,
"regularMarketPreviousClose": 25.36,
"regularMarketOpen": 25.52,
"averageDailyVolume10Day": 960970,
"averageDailyVolume3Month": 700041,
"fiftyTwoWeekLowChange": 6.5699997,
"fiftyTwoWeekLowChangePercent": 0.34835628,
"fiftyTwoWeekRange": "18.86 - 26.04",
"fiftyTwoWeekHighChange": -0.6100006,
"fiftyTwoWeekHighChangePercent": -0.023425521,
"fiftyTwoWeekLow": 18.86,
"fiftyTwoWeekHigh": 26.04,
"twoHundredDayAverage": 22.09025,
"twoHundredDayAverageChange": 3.3397503,
"twoHundredDayAverageChangePercent": 0.15118662,
"priceEarnings": 9.32901744,
"earningsPerShare": 2.7355789,
"logourl": "https://s3-symbol-logo.tradingview.com/itau-unibanco--big.svg"
}
],
"requestedAt": "2022-11-04T23:16:47.476Z"
}

0 comments on commit ab01c02

Please sign in to comment.