55
66import uvicorn
77import uvicorn .config
8- from fastapi import Depends , FastAPI , Header , HTTPException , status
8+ from fastapi import Depends , FastAPI , Header , HTTPException , Request , status
9+ from fastapi .exceptions import RequestValidationError
10+ from fastapi .responses import JSONResponse
911
1012from .webhook_dto import WebhookData
1113
@@ -65,7 +67,14 @@ def _init_server(self):
6567
6668 self ._server_app = FastAPI (docs_url = None )
6769
68- @self ._server_app .post ("/" , status_code = status .HTTP_204_NO_CONTENT )
70+ @self ._server_app .exception_handler (RequestValidationError )
71+ def validation_exception_handler (request : Request , exc : RequestValidationError ):
72+ return JSONResponse (
73+ status_code = 200 ,
74+ content = {"message" : "Incorrect data received" , "errors" : exc .errors ()},
75+ )
76+
77+ @self ._server_app .post ("/" , status_code = status .HTTP_200_OK )
6978 def webhook_endpoint (
7079 webhook_data : WebhookData ,
7180 authorization : Annotated [Union [str , None ], Header ()] = None ,
@@ -77,8 +86,8 @@ def webhook_endpoint(
7786 """
7887 Endpoint for receiving webhooks from GreenAPI
7988
80- If `WEBHOOK_AUTH_TOKEN` env provided, request's `authorization`
81- header must be in format `Bearer {WEBHOOK_AUTH_TOKEN }`
89+ If `webhook_auth_header` arg provided, request's `authorization`
90+ header must be in format `Bearer {webhook_auth_header }`
8291 """
8392 if webhook_auth_header and authorization != f"Bearer { webhook_auth_header } " :
8493 raise HTTPException (status_code = status .HTTP_401_UNAUTHORIZED )
0 commit comments