From fd8d4cc65ecdf959005d82286c1df27e0e008416 Mon Sep 17 00:00:00 2001 From: iamprecieee Date: Sat, 22 Feb 2025 20:53:53 +0100 Subject: [PATCH] refactor(routers): update send_payload function - updated send_payload function to handle curl posts to both telex and slack - httpx was not working as intended --- src/routers/github.py | 4 ++-- src/routers/telex.py | 14 ++++++++++---- src/utils/telex_utils.py | 6 +++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/routers/github.py b/src/routers/github.py index a47436f..2922f6e 100644 --- a/src/routers/github.py +++ b/src/routers/github.py @@ -1,7 +1,7 @@ from fastapi.routing import APIRouter from ..core.models import GitHubPayload, TelexWebhookPayload from ..config.config import settings -from ..utils.telex_utils import send_payload_to_telex +from ..utils.telex_utils import send_payload from fastapi.responses import JSONResponse from fastapi import status, HTTPException import json @@ -23,7 +23,7 @@ async def github_webhook(telex_channel_id: str, payload: GitHubPayload): telex_url = f"{settings.telex_webhook_url}/{telex_channel_id}" try: - response = await send_payload_to_telex(telex_payload, telex_url) + response = await send_payload(telex_payload, telex_url) response_data = json.loads(response.decode().strip()) except Exception as e: raise HTTPException( diff --git a/src/routers/telex.py b/src/routers/telex.py index fcf6d57..90ecaae 100644 --- a/src/routers/telex.py +++ b/src/routers/telex.py @@ -6,7 +6,8 @@ from fastapi import status, HTTPException, Query from typing import Annotated import ast -import httpx +from ..utils.telex_utils import send_payload +import json router = APIRouter(prefix="/telex") @@ -41,9 +42,14 @@ async def telex_webhook( if is_test == "true": all_messages.append(output_message["text"]) else: - async with httpx.AsyncClient() as client: - await client.post(slack_url, json={"text": output_message}) - + try: + await send_payload(json.dumps(output_message), slack_url) + except Exception as e: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail=f"Telex payload sending failed: {str(e)}", + ) + if is_test == "true": return JSONResponse( content=all_messages, diff --git a/src/utils/telex_utils.py b/src/utils/telex_utils.py index 6a6196c..26460f1 100644 --- a/src/utils/telex_utils.py +++ b/src/utils/telex_utils.py @@ -3,19 +3,19 @@ from fastapi import HTTPException, status -async def send_payload_to_telex(telex_payload: str, telex_url: str): +async def send_payload(payload: str, url: str): """Sends payload through an asynchronous curl subprocess.""" curl_command = [ settings.curl_command, "-X", "POST", - telex_url, + url, "-H", "Accept: application/json", "-H", "Content-Type: application/json", "-d", - telex_payload, + payload, ] process = await asyncio.create_subprocess_exec( *curl_command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE