Files
tradingagents/tradingagents/dataflows/net.py
T
Yijia-Xiao c42a2f2c61 refactor(dataflows): name the shared modules by what they hold
- interface -> router; symbol_utils -> symbols, which also takes safe_ticker_component
- utils is split: get_current_date to date_window, the HTTP helpers to net
- dataflows imports are absolute; the NoMarketDataError re-export from symbols is gone
2026-09-24 04:31:05 +00:00

38 lines
1.5 KiB
Python

"""HTTP helpers shared by the vendors."""
import requests
def get_scrubbed(url: str, *, params: dict, timeout: float, secret: str, passthrough=()):
"""``requests.get`` plus ``raise_for_status``, with ``secret`` kept out of errors.
Vendors that authenticate with a query parameter put the key in the URL, and
requests quotes the full URL in HTTP, connection and timeout errors, so any
log or traceback that records one would carry the key (#1324). A requests
error is re-raised as the same class with the key replaced and nothing
attached: no request or response (both hold the URL) and no exception chain,
which is why this raises after the ``except`` block rather than inside it.
Statuses in ``passthrough`` are returned for the caller to handle.
"""
try:
response = requests.get(url, params=params, timeout=timeout)
if response.status_code not in passthrough:
response.raise_for_status()
return response
except requests.RequestException as exc:
error = type(exc)(str(exc).replace(secret, "***")) if secret else exc
raise error
def vendor_reachable(url: str, timeout: float = 5.0) -> bool:
"""Whether the vendor answers at all, for telling silence from an outage.
A client that returns an empty result instead of raising leaves those two
cases indistinguishable. Called only when a result is empty.
"""
try:
requests.head(url, timeout=timeout, allow_redirects=True)
return True
except requests.RequestException:
return False