[+] implment assets_index

This commit is contained in:
Siarhei Siniak 2025-07-16 12:21:18 +03:00
parent e73f57670a
commit f7abca1e1b
3 changed files with 37 additions and 3 deletions

@ -56,7 +56,7 @@ class AssetTickerResponse(pydantic.BaseModel):
message: 'AssetHistoryResponse.message_t.point_t'
class AssetsResponse(pydantic.BaseModel):
action: Literal['asset_history'] = 'asset_history'
action: Literal['assets'] = 'assets'
class message_t(pydantic.BaseModel):
class asset_t(pydantic.BaseModel):

@ -3,7 +3,7 @@ import datetime
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.asyncio import async_sessionmaker
from . import schema
from ..tickers.logic import tickers_get_by_period
from ..tickers.logic import tickers_get_by_period, markets_all
from typing import (Optional, Literal)
@ -81,7 +81,23 @@ class WebsocketAPI:
self,
client: fastapi.WebSocket,
) -> None:
raise NotImplementedError
markets = await markets_all(
self.session,
)
await client.send_text(
schema.AssetsResponse(
message=schema.AssetsResponse.message_t(
assets=[
schema.AssetsResponse.message_t.asset_t.model_construct(
name=o.name,
id=o.id,
)
for o in markets
]
)
).json(by_alias=True,),
)
async def on_message(
self,

@ -63,3 +63,21 @@ async def tickers_get_by_period(
make_transient(o.market)
return rows
async def markets_all(
session: 'async_sessionmaker[AsyncSession]',
) -> list[Market]:
async with session() as active_session:
async with active_session.begin() as transaction:
q = select(
Market
)
res = await active_session.execute(q)
rows = [o[0] for o in res]
for o in rows:
active_session.expunge(o)
return rows