From 670852a8b287a910f7fcae1622997370d68b0f72 Mon Sep 17 00:00:00 2001 From: Sergey Mankovsky Date: Mon, 10 Feb 2025 17:29:53 +0100 Subject: [PATCH] Replace requests with httpx --- .../reflector/pipelines/main_live_pipeline.py | 4 +- server/reflector/views/transcripts.py | 6 +- server/reflector/views/zulip.py | 4 +- server/reflector/zulip.py | 106 +++++++++--------- 4 files changed, 63 insertions(+), 57 deletions(-) diff --git a/server/reflector/pipelines/main_live_pipeline.py b/server/reflector/pipelines/main_live_pipeline.py index d54ebac0..8ce8d6bd 100644 --- a/server/reflector/pipelines/main_live_pipeline.py +++ b/server/reflector/pipelines/main_live_pipeline.py @@ -580,7 +580,7 @@ async def pipeline_post_to_zulip(transcript: Transcript, logger: Logger): message_updated = False if transcript.zulip_message_id: try: - update_zulip_message( + await update_zulip_message( transcript.zulip_message_id, room.zulip_stream, room.zulip_topic, @@ -592,7 +592,7 @@ async def pipeline_post_to_zulip(transcript: Transcript, logger: Logger): f"Failed to update zulip message with id {transcript.zulip_message_id}" ) if not message_updated: - response = send_message_to_zulip( + response = await send_message_to_zulip( room.zulip_stream, room.zulip_topic, message ) await transcripts_controller.update( diff --git a/server/reflector/views/transcripts.py b/server/reflector/views/transcripts.py index f51f5aca..c4e2913c 100644 --- a/server/reflector/views/transcripts.py +++ b/server/reflector/views/transcripts.py @@ -362,13 +362,15 @@ async def transcript_post_to_zulip( message_updated = False if transcript.zulip_message_id: try: - update_zulip_message(transcript.zulip_message_id, stream, topic, content) + await update_zulip_message( + transcript.zulip_message_id, stream, topic, content + ) message_updated = True except InvalidMessageError: pass if not message_updated: - response = send_message_to_zulip(stream, topic, content) + response = await send_message_to_zulip(stream, topic, content) await transcripts_controller.update( transcript, {"zulip_message_id": response["id"]} ) diff --git a/server/reflector/views/zulip.py b/server/reflector/views/zulip.py index 451e94e0..62d77f94 100644 --- a/server/reflector/views/zulip.py +++ b/server/reflector/views/zulip.py @@ -27,7 +27,7 @@ async def zulip_get_streams( if not user: raise HTTPException(status_code=403, detail="Authentication required") - streams = get_zulip_streams() + streams = await get_zulip_streams() return streams @@ -42,5 +42,5 @@ async def zulip_get_topics( if not user: raise HTTPException(status_code=403, detail="Authentication required") - topics = get_zulip_topics(stream_id) + topics = await get_zulip_topics(stream_id) return topics diff --git a/server/reflector/zulip.py b/server/reflector/zulip.py index 7f6b85ba..ca44a3d2 100644 --- a/server/reflector/zulip.py +++ b/server/reflector/zulip.py @@ -1,7 +1,7 @@ from datetime import timedelta from urllib.parse import urlparse -import requests +import httpx from reflector.db.transcripts import Transcript from reflector.settings import settings @@ -10,77 +10,81 @@ class InvalidMessageError(Exception): pass -def get_zulip_topics(stream_id: int) -> list[dict]: +async def get_zulip_topics(stream_id: int) -> list[dict]: try: - response = requests.get( - f"https://{settings.ZULIP_REALM}/api/v1/users/me/{stream_id}/topics", - auth=(settings.ZULIP_BOT_EMAIL, settings.ZULIP_API_KEY), - ) + async with httpx.AsyncClient() as client: + response = await client.get( + f"https://{settings.ZULIP_REALM}/api/v1/users/me/{stream_id}/topics", + auth=(settings.ZULIP_BOT_EMAIL, settings.ZULIP_API_KEY), + ) - response.raise_for_status() + response.raise_for_status() - return response.json().get("topics", []) - except requests.RequestException as error: + return response.json().get("topics", []) + except httpx.RequestError as error: raise Exception(f"Failed to get topics: {error}") -def get_zulip_streams() -> list[dict]: +async def get_zulip_streams() -> list[dict]: try: - response = requests.get( - f"https://{settings.ZULIP_REALM}/api/v1/streams", - auth=(settings.ZULIP_BOT_EMAIL, settings.ZULIP_API_KEY), - ) + async with httpx.AsyncClient() as client: + response = await client.get( + f"https://{settings.ZULIP_REALM}/api/v1/streams", + auth=(settings.ZULIP_BOT_EMAIL, settings.ZULIP_API_KEY), + ) - response.raise_for_status() + response.raise_for_status() - return response.json().get("streams", []) - except requests.RequestException as error: + return response.json().get("streams", []) + except httpx.RequestError as error: raise Exception(f"Failed to get streams: {error}") -def send_message_to_zulip(stream: str, topic: str, content: str): +async def send_message_to_zulip(stream: str, topic: str, content: str): try: - response = requests.post( - f"https://{settings.ZULIP_REALM}/api/v1/messages", - data={ - "type": "stream", - "to": stream, - "topic": topic, - "content": content, - }, - auth=(settings.ZULIP_BOT_EMAIL, settings.ZULIP_API_KEY), - headers={"Content-Type": "application/x-www-form-urlencoded"}, - ) + async with httpx.AsyncClient() as client: + response = await client.post( + f"https://{settings.ZULIP_REALM}/api/v1/messages", + data={ + "type": "stream", + "to": stream, + "topic": topic, + "content": content, + }, + auth=(settings.ZULIP_BOT_EMAIL, settings.ZULIP_API_KEY), + headers={"Content-Type": "application/x-www-form-urlencoded"}, + ) - response.raise_for_status() + response.raise_for_status() - return response.json() - except requests.RequestException as error: + return response.json() + except httpx.RequestError as error: raise Exception(f"Failed to send message to Zulip: {error}") -def update_zulip_message(message_id: int, stream: str, topic: str, content: str): +async def update_zulip_message(message_id: int, stream: str, topic: str, content: str): try: - response = requests.patch( - f"https://{settings.ZULIP_REALM}/api/v1/messages/{message_id}", - data={ - "topic": topic, - "content": content, - }, - auth=(settings.ZULIP_BOT_EMAIL, settings.ZULIP_API_KEY), - headers={"Content-Type": "application/x-www-form-urlencoded"}, - ) + async with httpx.AsyncClient() as client: + response = await client.patch( + f"https://{settings.ZULIP_REALM}/api/v1/messages/{message_id}", + data={ + "topic": topic, + "content": content, + }, + auth=(settings.ZULIP_BOT_EMAIL, settings.ZULIP_API_KEY), + headers={"Content-Type": "application/x-www-form-urlencoded"}, + ) - if ( - response.status_code == 400 - and response.json()["msg"] == "Invalid message(s)" - ): - raise InvalidMessageError(f"There is no message with id: {message_id}") + if ( + response.status_code == 400 + and response.json()["msg"] == "Invalid message(s)" + ): + raise InvalidMessageError(f"There is no message with id: {message_id}") - response.raise_for_status() + response.raise_for_status() - return response.json() - except requests.RequestException as error: + return response.json() + except httpx.RequestError as error: raise Exception(f"Failed to update Zulip message: {error}") @@ -101,7 +105,7 @@ def get_zulip_message(transcript: Transcript, include_topics: bool): topic_text += "```\n\n" summary = "```spoiler Summary\n" - summary += transcript.long_summary + summary += transcript.long_summary or "No summary available" summary += "\n```\n\n" message = header_text + summary + topic_text + "-----\n"