Skip to content

Commit 70fe1e2

Browse files
committed
Status code when error was occured changed 418 -> 200 with error message.
Success status code changed `204` -> `200`. Minor changes to examples Version updated
1 parent a7a6189 commit 70fe1e2

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

examples/docker_example.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
from whatsapp_api_webhook_server_python_v2 import GreenAPIWebhookServer
44

5-
count = 0
6-
75
port = int(os.environ.get("PORT", 8080))
8-
auth_header = str(os.environ.get("AUTH_HEADER", None))
6+
auth_header = os.environ.get("AUTH_HEADER", None)
7+
8+
count = 0
99

1010

1111
def event_handler(webhook_type: str, webhook_data: dict):
@@ -23,6 +23,7 @@ def event_handler(webhook_type: str, webhook_data: dict):
2323
host="0.0.0.0",
2424
port=port,
2525
webhook_auth_header=auth_header, # Change it to actual webhook secret
26+
return_keys_by_alias=True,
2627
)
2728

2829

examples/receive_all_with_counter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def event_handler(webhook_type: str, webhook_data: dict):
1818
host="0.0.0.0",
1919
port=8080,
2020
webhook_auth_header=None, # Change it to actual webhook secret
21+
return_keys_by_alias=True,
2122
)
2223

2324

examples/simple_receive_message_status.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def event_handler(webhook_type: str, webhook_data: dict):
3131
host="0.0.0.0",
3232
port=8080,
3333
webhook_auth_header=None, # Change it to actual webhook secret
34+
return_keys_by_alias=True,
3435
)
3536

3637
if __name__ == "__main__":

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="whatsapp-api-webhook-server-python-v2",
5-
version="0.1.0",
5+
version="0.1.1",
66
description=(
77
"This library helps you easily create"
88
" a Python server endpoint to receive"

whatsapp_api_webhook_server_python_v2/main.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
import uvicorn
77
import 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

1012
from .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

Comments
 (0)