Skip to content

Commit

Permalink
feat: reuse http client
Browse files Browse the repository at this point in the history
  • Loading branch information
jackstar12 committed Jan 24, 2025
1 parent 308d954 commit 6e61a0b
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,20 @@ async def notify_subscribers(
logging.error(f"Error notifying subscriber {chat_id}: {e}")


async def get_fees(api_url: str) -> Fees:
# TODO: reuse session
async with AsyncClient(base_url=api_url) as client:
response = await client.get("/v2/swap/submarine", headers={"Referral": "pro"})
response.raise_for_status()
data = response.json()
async def get_fees(client: AsyncClient) -> Fees:
response = await client.get("/v2/swap/submarine", headers={"Referral": "pro"})
response.raise_for_status()
data = response.json()

fees = {}
for quote_currency in data:
fees[quote_currency] = {}
for base_currency in data[quote_currency]:
fees[quote_currency][base_currency] = data[quote_currency][
base_currency
]["fees"]["percentage"]
fees = {}
for quote_currency in data:
fees[quote_currency] = {}
for base_currency in data[quote_currency]:
fees[quote_currency][base_currency] = data[quote_currency][
base_currency
]["fees"]["percentage"]

return fees
return fees


async def check_fees(
Expand Down Expand Up @@ -144,12 +142,17 @@ def main():
application.add_handler(CommandHandler("subscribe", subscribe))
application.add_handler(CommandHandler("unsubscribe", unsubscribe))

client = AsyncClient(base_url=settings.api_url)

async def post_init(app: Application):
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)

async def post_shutdown(app: Application):
await client.aclose()

async def monitor_fees(app: Application):
current = await get_fees(settings.api_url)
current = await get_fees(client)

async with async_session() as session:
previous = await get_previous(session, SUBMARINE_SWAP_TYPE)
Expand All @@ -167,6 +170,7 @@ async def monitor_fees(app: Application):
await upsert_previous(session, SUBMARINE_SWAP_TYPE, current)

application.post_init = post_init
application.post_shutdown = post_shutdown
application.job_queue.run_repeating(
monitor_fees, interval=settings.check_interval, first=0
)
Expand Down

0 comments on commit 6e61a0b

Please sign in to comment.