diff --git a/brapi.go b/brapi.go index e75c95a..2e6870e 100644 --- a/brapi.go +++ b/brapi.go @@ -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"` } @@ -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) { @@ -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, "eData) + + if err != nil { + return nil, err + } + + return quoteData.Results, nil +} diff --git a/brapi_test.go b/brapi_test.go index b750bf0..4bc20e7 100644 --- a/brapi_test.go +++ b/brapi_test.go @@ -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 = "e } - if stock.Stock == "ITUB3" { - itub3Stock = &stock + if quote.Symbol == "ITUB3" { + itub3Quote = "e } } - 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") } } diff --git a/brapi_test_helper.go b/brapi_test_helper.go index 7115c4c..a1699e1 100644 --- a/brapi_test_helper.go +++ b/brapi_test_helper.go @@ -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") @@ -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, "eData) + + return "eData, err +} diff --git a/test-data/quote_data.json b/test-data/quote_data.json new file mode 100755 index 0000000..2aec00a --- /dev/null +++ b/test-data/quote_data.json @@ -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" +} \ No newline at end of file