Skip to content

Commit

Permalink
fix: improve binance service
Browse files Browse the repository at this point in the history
  • Loading branch information
adagora committed Nov 28, 2023
1 parent 41ff59e commit ee9574c
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions solarathon/pages/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
# root.setLevel(logging.DEBUG)

# some app state that outlives a single page
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, ValidationError


init_app_state = solara.reactive(["ada", "btc","bnb", "eth","doge", "xrp"])
Expand Down Expand Up @@ -73,9 +73,22 @@ def format_price(price):

def get_binance_ticket(symbol: str) -> TickerData:
binance_url = f"https://api.binance.com/api/v3/ticker/24hr?symbol={symbol}"
return TickerData.model_validate_json(
json_data=requests.get(binance_url).content
)
response = requests.get(binance_url)

try:
response.raise_for_status()
data = response.json()

# Ensure that all required fields are present in the data
if all(key in data for key in ('lastPrice', 'priceChangePercent', 'highPrice', 'lowPrice')):
ticker_data = TickerData(**data)
return ticker_data
else:
return TickerData(symbol="No data", last_price=0.0, price_change_percent=0.0, high_price=0.0, low_price=0.0)
except requests.exceptions.RequestException as e:
print("Request Exception:", e)
except ValidationError as ve:
print("Validation Error:", ve)


@solara.component
Expand Down

0 comments on commit ee9574c

Please sign in to comment.