Skip to content

Commit 326c3c9

Browse files
authored
Merge pull request #34 from telexintegrations/refactor/routers
refactor(routers): update send_payload function
2 parents 3abe8b5 + fd8d4cc commit 326c3c9

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

src/routers/github.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from fastapi.routing import APIRouter
22
from ..core.models import GitHubPayload, TelexWebhookPayload
33
from ..config.config import settings
4-
from ..utils.telex_utils import send_payload_to_telex
4+
from ..utils.telex_utils import send_payload
55
from fastapi.responses import JSONResponse
66
from fastapi import status, HTTPException
77
import json
@@ -23,7 +23,7 @@ async def github_webhook(telex_channel_id: str, payload: GitHubPayload):
2323
telex_url = f"{settings.telex_webhook_url}/{telex_channel_id}"
2424

2525
try:
26-
response = await send_payload_to_telex(telex_payload, telex_url)
26+
response = await send_payload(telex_payload, telex_url)
2727
response_data = json.loads(response.decode().strip())
2828
except Exception as e:
2929
raise HTTPException(

src/routers/telex.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from fastapi import status, HTTPException, Query
77
from typing import Annotated
88
import ast
9-
import httpx
9+
from ..utils.telex_utils import send_payload
10+
import json
1011

1112

1213
router = APIRouter(prefix="/telex")
@@ -41,9 +42,14 @@ async def telex_webhook(
4142
if is_test == "true":
4243
all_messages.append(output_message["text"])
4344
else:
44-
async with httpx.AsyncClient() as client:
45-
await client.post(slack_url, json={"text": output_message})
46-
45+
try:
46+
await send_payload(json.dumps(output_message), slack_url)
47+
except Exception as e:
48+
raise HTTPException(
49+
status_code=status.HTTP_400_BAD_REQUEST,
50+
detail=f"Telex payload sending failed: {str(e)}",
51+
)
52+
4753
if is_test == "true":
4854
return JSONResponse(
4955
content=all_messages,

src/utils/telex_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
from fastapi import HTTPException, status
44

55

6-
async def send_payload_to_telex(telex_payload: str, telex_url: str):
6+
async def send_payload(payload: str, url: str):
77
"""Sends payload through an asynchronous curl subprocess."""
88
curl_command = [
99
settings.curl_command,
1010
"-X",
1111
"POST",
12-
telex_url,
12+
url,
1313
"-H",
1414
"Accept: application/json",
1515
"-H",
1616
"Content-Type: application/json",
1717
"-d",
18-
telex_payload,
18+
payload,
1919
]
2020
process = await asyncio.create_subprocess_exec(
2121
*curl_command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE

0 commit comments

Comments
 (0)