Код потоковой передачи
@app.get("/streaming/{symbol}")async def getStreamingQuoteBySymbol(symbol):
try:
client = schwab.auth.client_from_token_file(token_path=token_path, api_key=API_KEY, app_secret=API_SECRET)
streamClient = schwab.streaming.StreamClient(client=client, account_id=ACCOUNT_ID)
async def read_stream():
try:
await streamClient.login()
print("Login successfully")
except Exception as e:
print("Failed to login streaming api.")
def print_message(message):
data = json.dump(message, index=4)
print(data)
print()
streamClient.add_level_one_equity_handler(print_message)
await streamClient.level_one_equity_subs([symbol])
while True:
await streamClient.handle_message()
return StreamingResponse(await read_stream())
except Exception as e:
print("Error message: ", e)
return { "status": "failed to stream" }
Подробнее здесь: https://stackoverflow.com/questions/791 ... on-fastapi
Мобильная версия