Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gnvk committed Nov 14, 2024
1 parent d3444c9 commit 6047845
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 57 deletions.
13 changes: 8 additions & 5 deletions alpaca/common/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,10 @@ def _validate_credentials(

return api_key, secret_key, oauth_token

def get_marketdata(
def _get_marketdata(
self,
path: str,
params: Dict[str, Any] = None,
params: Dict[str, Any],
page_limit: int = 10_000,
no_sub_key: bool = False,
) -> Dict[str, List[Any]]:
Expand Down Expand Up @@ -426,10 +426,13 @@ def _get_marketdata_entries(response: HTTPResult, no_sub_key: bool) -> RawData:
"trade",
"trades",
}
selected_key = data_keys.intersection(response)
if selected_key is None or len(selected_key) < 1:
selected_keys = data_keys.intersection(response)
# Neither of these should ever happen!
if selected_keys is None or len(selected_keys) < 1:
raise ValueError("The data in response does not match any known keys.")
selected_key = selected_key.pop()
if len(selected_keys) > 1:
raise ValueError("The data in response matches multiple known keys.")
selected_key = selected_keys.pop()
if selected_key == "news":
return {"news": response[selected_key]}
return response[selected_key]
2 changes: 1 addition & 1 deletion alpaca/data/historical/corporate_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def get_corporate_actions(
if request_params.types:
params["types"] = ",".join(request_params.types)

response = self.get_marketdata(
response = self._get_marketdata(
path="/corporate-actions",
params=params,
page_limit=1000,
Expand Down
16 changes: 8 additions & 8 deletions alpaca/data/historical/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def get_crypto_bars(
"""

# paginated get request for crypto market data api
raw_bars = self.get_marketdata(
raw_bars = self._get_marketdata(
path=f"/crypto/{feed.value}/bars",
params=request_params.to_request_fields(),
)
Expand All @@ -115,7 +115,7 @@ def get_crypto_quotes(
"""

# paginated get request for market data api
raw_quotes = self.get_marketdata(
raw_quotes = self._get_marketdata(
path=f"/crypto/{feed.value}/quotes",
params=request_params.to_request_fields(),
)
Expand All @@ -140,7 +140,7 @@ def get_crypto_trades(
"""

# paginated get request for market data api
raw_trades = self.get_marketdata(
raw_trades = self._get_marketdata(
path=f"/crypto/{feed.value}/trades",
params=request_params.to_request_fields(),
)
Expand All @@ -163,7 +163,7 @@ def get_crypto_latest_trade(
Union[Dict[str, Trade], RawData]: The latest trade in raw or wrapped format
"""

raw_trades = self.get_marketdata(
raw_trades = self._get_marketdata(
path=f"/crypto/{feed.value}/latest/trades",
params=request_params.to_request_fields(),
)
Expand All @@ -186,7 +186,7 @@ def get_crypto_latest_quote(
Union[Dict[str, Quote], RawData]: The latest quote in raw or wrapped format
"""

raw_quotes = self.get_marketdata(
raw_quotes = self._get_marketdata(
path=f"/crypto/{feed.value}/latest/quotes",
params=request_params.to_request_fields(),
)
Expand All @@ -209,7 +209,7 @@ def get_crypto_latest_bar(
Union[Dict[str, Bar], RawData]: The latest bar in raw or wrapped format
"""

raw_bars = self.get_marketdata(
raw_bars = self._get_marketdata(
path=f"/crypto/{feed.value}/latest/bars",
params=request_params.to_request_fields(),
)
Expand All @@ -235,7 +235,7 @@ def get_crypto_latest_orderbook(
Union[Dict[str, Orderbook], RawData]: The orderbook data either in raw or wrapped form.
"""

raw_orderbooks = self.get_marketdata(
raw_orderbooks = self._get_marketdata(
path=f"/crypto/{feed.value}/latest/orderbooks",
params=request_params.to_request_fields(),
)
Expand All @@ -259,7 +259,7 @@ def get_crypto_snapshot(
Union[SnapshotSet, RawData]: The snapshot data either in raw or wrapped form
"""

raw_snapshots = self.get_marketdata(
raw_snapshots = self._get_marketdata(
path=f"/crypto/{feed.value}/snapshots",
params=request_params.to_request_fields(),
)
Expand Down
2 changes: 1 addition & 1 deletion alpaca/data/historical/news.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_news(self, request_params: NewsRequest) -> Union[RawData, NewsSet]:
Args:
request_params (NewsRequest): The request params to filter the news data"""
raw_news = self.get_marketdata(
raw_news = self._get_marketdata(
path=f"/news",
params=request_params.to_request_fields(),
)
Expand Down
12 changes: 6 additions & 6 deletions alpaca/data/historical/option.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def get_option_bars(
"""

# paginated get request for market data api
raw_bars = self.get_marketdata(
raw_bars = self._get_marketdata(
path=f"/options/bars",
params=request_params.to_request_fields(),
)
Expand Down Expand Up @@ -123,7 +123,7 @@ def get_option_latest_quote(
Returns:
Union[Dict[str, Quote], RawData]: The latest quote in raw or wrapped format
"""
raw_latest_quotes = self.get_marketdata(
raw_latest_quotes = self._get_marketdata(
path=f"/options/quotes/latest",
params=request_params.to_request_fields(),
)
Expand All @@ -144,7 +144,7 @@ def get_option_latest_trade(
Returns:
Union[Dict[str, Quote], RawData]: The latest quote in raw or wrapped format
"""
raw_latest_trades = self.get_marketdata(
raw_latest_trades = self._get_marketdata(
path=f"/options/trades/latest",
params=request_params.to_request_fields(),
)
Expand All @@ -165,7 +165,7 @@ def get_option_trades(
Returns:
Union[TradeSet, RawData]: The trade data either in raw or wrapped form
"""
raw_trades = self.get_marketdata(
raw_trades = self._get_marketdata(
path=f"/options/trades",
params=request_params.to_request_fields(),
)
Expand All @@ -187,7 +187,7 @@ def get_option_snapshot(
Returns:
Union[Dict[str, OptionsSnapshot], RawData]: The snapshot data either in raw or wrapped form
"""
raw_snapshots = self.get_marketdata(
raw_snapshots = self._get_marketdata(
path=f"/options/snapshots",
params=request_params.to_request_fields(),
)
Expand All @@ -213,7 +213,7 @@ def get_option_chain(
params = request_params.to_request_fields()
del params["underlying_symbol"]

raw_snapshots = self.get_marketdata(
raw_snapshots = self._get_marketdata(
path=f"/options/snapshots/{request_params.underlying_symbol}",
params=params,
)
Expand Down
14 changes: 7 additions & 7 deletions alpaca/data/historical/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def get_stock_bars(
Returns:
Union[BarSet, RawData]: The bar data either in raw or wrapped form
"""
raw_bars = self.get_marketdata(
raw_bars = self._get_marketdata(
path="/stocks/bars",
params=request_params.to_request_fields(),
)
Expand All @@ -105,7 +105,7 @@ def get_stock_quotes(
Returns:
Union[QuoteSet, RawData]: The quote data either in raw or wrapped form
"""
raw_quotes = self.get_marketdata(
raw_quotes = self._get_marketdata(
path="/stocks/quotes",
params=request_params.to_request_fields(),
)
Expand All @@ -126,7 +126,7 @@ def get_stock_trades(
Returns:
Union[TradeSet, RawData]: The trade data either in raw or wrapped form
"""
raw_trades = self.get_marketdata(
raw_trades = self._get_marketdata(
path="/stocks/trades",
params=request_params.to_request_fields(),
)
Expand All @@ -147,7 +147,7 @@ def get_stock_latest_trade(
Returns:
Union[Dict[str, Trade], RawData]: The latest trade in raw or wrapped format
"""
raw_latest_trades = self.get_marketdata(
raw_latest_trades = self._get_marketdata(
path="/stocks/trades/latest",
params=request_params.to_request_fields(),
)
Expand All @@ -168,7 +168,7 @@ def get_stock_latest_quote(
Returns:
Union[Dict[str, Quote], RawData]: The latest quote in raw or wrapped format
"""
raw_latest_quotes = self.get_marketdata(
raw_latest_quotes = self._get_marketdata(
path="/stocks/quotes/latest",
params=request_params.to_request_fields(),
)
Expand All @@ -189,7 +189,7 @@ def get_stock_latest_bar(
Returns:
Union[Dict[str, Bar], RawData]: The latest minute bar in raw or wrapped format
"""
raw_latest_bars = self.get_marketdata(
raw_latest_bars = self._get_marketdata(
path="/stocks/bars/latest",
params=request_params.to_request_fields(),
)
Expand All @@ -211,7 +211,7 @@ def get_stock_snapshot(
Returns:
Union[SnapshotSet, RawData]: The snapshot data either in raw or wrapped form
"""
raw_snapshots = self.get_marketdata(
raw_snapshots = self._get_marketdata(
path="/stocks/snapshots",
params=request_params.to_request_fields(),
no_sub_key=True,
Expand Down
29 changes: 0 additions & 29 deletions tests/data/test_historical_stock_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
from datetime import datetime, timezone
from typing import Dict

import pytest

from alpaca.common.enums import Sort
from alpaca.common.exceptions import APIError
from alpaca.data import Bar, Quote, Snapshot, Trade
from alpaca.data.enums import DataFeed, Exchange
from alpaca.data.historical import StockHistoricalDataClient
Expand Down Expand Up @@ -707,32 +704,6 @@ def test_get_multisymbol_latest_trade(reqmock, stock_client: StockHistoricalData
assert reqmock.called_once


def test_get_latest_trade_multi_not_found(
reqmock, stock_client: StockHistoricalDataClient
):
symbol = "AAPL"

reqmock.get(
f"https://data.alpaca.markets/v2/stocks/{symbol}/trades/latest?&feed=IEX",
text="""
{
"next_page_token": null
"trade": null,
"symbol": "AAPL"
}
""",
)
request = StockLatestTradeRequest(symbol_or_symbols=symbol, feed=DataFeed.IEX)

trade = stock_client.get_stock_latest_trade(request_params=request)

assert isinstance(trade, Dict)

assert trade == {}

assert reqmock.called_once


def test_get_latest_trade_multi_not_found(
reqmock, stock_client: StockHistoricalDataClient
):
Expand Down

0 comments on commit 6047845

Please sign in to comment.