11from typing import Any , Dict
2- import asyncio
32
43from fastapi import APIRouter , Body , Depends , HTTPException , Response
54
1615router = APIRouter (prefix = "/webhooks" )
1716
1817
19- async def process_chainhook_async (data : Dict [str , Any ]) -> None :
20- """Process chainhook webhook asynchronously with logging.
21-
22- Args:
23- data: The webhook payload as JSON
24- """
25- service = ChainhookService ()
26-
27- try :
28- logger .info ("Starting async chainhook processing" )
29- result = await service .process (data )
30- logger .info (f"Chainhook processing completed successfully: { result } " )
31- except Exception as e :
32- logger .error (f"Chainhook processing failed: { str (e )} " , exc_info = True )
33-
34-
3518@router .post ("/chainhook" )
3619async def chainhook (
3720 data : Dict [str , Any ] = Body (...),
@@ -42,22 +25,29 @@ async def chainhook(
4225 This endpoint requires Bearer token authentication via the Authorization header.
4326 The token must match the one configured in AIBTC_WEBHOOK_AUTH_TOKEN.
4427
45- Returns 204 immediately and processes the webhook asynchronously .
28+ Returns 204 only if processing is successful, otherwise raises HTTPException .
4629
4730 Args:
4831 data: The webhook payload as JSON
4932
5033 Returns:
51- Response: 204 No Content status
34+ Response: 204 No Content status on success
5235
5336 Raises:
54- HTTPException: If authentication fails
37+ HTTPException: If authentication fails or processing fails
5538 """
56- # Start async processing without awaiting
57- asyncio .create_task (process_chainhook_async (data ))
39+ service = ChainhookService ()
5840
59- # Return 204 immediately
60- return Response (status_code = 204 )
41+ try :
42+ logger .info ("Processing chainhook webhook" )
43+ await service .process (data )
44+ logger .info ("Chainhook processing completed successfully" )
45+ return Response (status_code = 204 )
46+ except Exception as e :
47+ logger .error (f"Chainhook processing failed: { str (e )} " , exc_info = True )
48+ raise HTTPException (
49+ status_code = 500 , detail = f"Error processing chainhook webhook: { str (e )} "
50+ )
6151
6252
6353@router .post ("/dao" )
0 commit comments