mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 20:29:06 +00:00
Replace requests with httpx
This commit is contained in:
@@ -580,7 +580,7 @@ async def pipeline_post_to_zulip(transcript: Transcript, logger: Logger):
|
|||||||
message_updated = False
|
message_updated = False
|
||||||
if transcript.zulip_message_id:
|
if transcript.zulip_message_id:
|
||||||
try:
|
try:
|
||||||
update_zulip_message(
|
await update_zulip_message(
|
||||||
transcript.zulip_message_id,
|
transcript.zulip_message_id,
|
||||||
room.zulip_stream,
|
room.zulip_stream,
|
||||||
room.zulip_topic,
|
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}"
|
f"Failed to update zulip message with id {transcript.zulip_message_id}"
|
||||||
)
|
)
|
||||||
if not message_updated:
|
if not message_updated:
|
||||||
response = send_message_to_zulip(
|
response = await send_message_to_zulip(
|
||||||
room.zulip_stream, room.zulip_topic, message
|
room.zulip_stream, room.zulip_topic, message
|
||||||
)
|
)
|
||||||
await transcripts_controller.update(
|
await transcripts_controller.update(
|
||||||
|
|||||||
@@ -362,13 +362,15 @@ async def transcript_post_to_zulip(
|
|||||||
message_updated = False
|
message_updated = False
|
||||||
if transcript.zulip_message_id:
|
if transcript.zulip_message_id:
|
||||||
try:
|
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
|
message_updated = True
|
||||||
except InvalidMessageError:
|
except InvalidMessageError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if not message_updated:
|
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(
|
await transcripts_controller.update(
|
||||||
transcript, {"zulip_message_id": response["id"]}
|
transcript, {"zulip_message_id": response["id"]}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ async def zulip_get_streams(
|
|||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(status_code=403, detail="Authentication required")
|
raise HTTPException(status_code=403, detail="Authentication required")
|
||||||
|
|
||||||
streams = get_zulip_streams()
|
streams = await get_zulip_streams()
|
||||||
return streams
|
return streams
|
||||||
|
|
||||||
|
|
||||||
@@ -42,5 +42,5 @@ async def zulip_get_topics(
|
|||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(status_code=403, detail="Authentication required")
|
raise HTTPException(status_code=403, detail="Authentication required")
|
||||||
|
|
||||||
topics = get_zulip_topics(stream_id)
|
topics = await get_zulip_topics(stream_id)
|
||||||
return topics
|
return topics
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
import requests
|
import httpx
|
||||||
from reflector.db.transcripts import Transcript
|
from reflector.db.transcripts import Transcript
|
||||||
from reflector.settings import settings
|
from reflector.settings import settings
|
||||||
|
|
||||||
@@ -10,77 +10,81 @@ class InvalidMessageError(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def get_zulip_topics(stream_id: int) -> list[dict]:
|
async def get_zulip_topics(stream_id: int) -> list[dict]:
|
||||||
try:
|
try:
|
||||||
response = requests.get(
|
async with httpx.AsyncClient() as client:
|
||||||
f"https://{settings.ZULIP_REALM}/api/v1/users/me/{stream_id}/topics",
|
response = await client.get(
|
||||||
auth=(settings.ZULIP_BOT_EMAIL, settings.ZULIP_API_KEY),
|
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", [])
|
return response.json().get("topics", [])
|
||||||
except requests.RequestException as error:
|
except httpx.RequestError as error:
|
||||||
raise Exception(f"Failed to get topics: {error}")
|
raise Exception(f"Failed to get topics: {error}")
|
||||||
|
|
||||||
|
|
||||||
def get_zulip_streams() -> list[dict]:
|
async def get_zulip_streams() -> list[dict]:
|
||||||
try:
|
try:
|
||||||
response = requests.get(
|
async with httpx.AsyncClient() as client:
|
||||||
f"https://{settings.ZULIP_REALM}/api/v1/streams",
|
response = await client.get(
|
||||||
auth=(settings.ZULIP_BOT_EMAIL, settings.ZULIP_API_KEY),
|
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", [])
|
return response.json().get("streams", [])
|
||||||
except requests.RequestException as error:
|
except httpx.RequestError as error:
|
||||||
raise Exception(f"Failed to get streams: {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:
|
try:
|
||||||
response = requests.post(
|
async with httpx.AsyncClient() as client:
|
||||||
f"https://{settings.ZULIP_REALM}/api/v1/messages",
|
response = await client.post(
|
||||||
data={
|
f"https://{settings.ZULIP_REALM}/api/v1/messages",
|
||||||
"type": "stream",
|
data={
|
||||||
"to": stream,
|
"type": "stream",
|
||||||
"topic": topic,
|
"to": stream,
|
||||||
"content": content,
|
"topic": topic,
|
||||||
},
|
"content": content,
|
||||||
auth=(settings.ZULIP_BOT_EMAIL, settings.ZULIP_API_KEY),
|
},
|
||||||
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
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()
|
return response.json()
|
||||||
except requests.RequestException as error:
|
except httpx.RequestError as error:
|
||||||
raise Exception(f"Failed to send message to Zulip: {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:
|
try:
|
||||||
response = requests.patch(
|
async with httpx.AsyncClient() as client:
|
||||||
f"https://{settings.ZULIP_REALM}/api/v1/messages/{message_id}",
|
response = await client.patch(
|
||||||
data={
|
f"https://{settings.ZULIP_REALM}/api/v1/messages/{message_id}",
|
||||||
"topic": topic,
|
data={
|
||||||
"content": content,
|
"topic": topic,
|
||||||
},
|
"content": content,
|
||||||
auth=(settings.ZULIP_BOT_EMAIL, settings.ZULIP_API_KEY),
|
},
|
||||||
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
auth=(settings.ZULIP_BOT_EMAIL, settings.ZULIP_API_KEY),
|
||||||
)
|
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
||||||
|
)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
response.status_code == 400
|
response.status_code == 400
|
||||||
and response.json()["msg"] == "Invalid message(s)"
|
and response.json()["msg"] == "Invalid message(s)"
|
||||||
):
|
):
|
||||||
raise InvalidMessageError(f"There is no message with id: {message_id}")
|
raise InvalidMessageError(f"There is no message with id: {message_id}")
|
||||||
|
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
return response.json()
|
return response.json()
|
||||||
except requests.RequestException as error:
|
except httpx.RequestError as error:
|
||||||
raise Exception(f"Failed to update Zulip message: {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"
|
topic_text += "```\n\n"
|
||||||
|
|
||||||
summary = "```spoiler Summary\n"
|
summary = "```spoiler Summary\n"
|
||||||
summary += transcript.long_summary
|
summary += transcript.long_summary or "No summary available"
|
||||||
summary += "\n```\n\n"
|
summary += "\n```\n\n"
|
||||||
|
|
||||||
message = header_text + summary + topic_text + "-----\n"
|
message = header_text + summary + topic_text + "-----\n"
|
||||||
|
|||||||
Reference in New Issue
Block a user