Skip to content

Commit 59f9cd7

Browse files
committed
Added more examples
1 parent 1160b96 commit 59f9cd7

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ HTTP_TIMEOUT="30"
77
REDIS_URL="redis://redis:6380/1"
88

99
# API token, SHA-256 key.
10-
API_TOKEN="$5$1O/inyTZhNvFt.GW$Zfckz9OL.lm2wh3IewTm8YJ914wjz5txFnXG5XW.wb4"
10+
API_TOKEN='$5$1O/inyTZhNvFt.GW$Zfckz9OL.lm2wh3IewTm8YJ914wjz5txFnXG5XW.wb4'
1111

1212
# Retry parameters.
1313
MAX_RETRIES="3"

app/services.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def send_post_request(webhook_payload: SlingerRequestPayload) -> NoReturn:
7373
raise WebhookPostFailedError(
7474
f"Sending webhook failed.\n"
7575
f"to_url: {to_url}\n"
76-
f"payload: {payload}\n"
76+
f"payload: {payload}\n, code: {response.status_code}"
7777
)
7878

7979

config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
"API_TOKEN", "$5$1O/inyTZhNvFt.GW$Zfckz9OL.lm2wh3IewTm8YJ914wjz5txFnXG5XW.wb4"
1818
)
1919

20+
print("=" * 40)
21+
print(f"{API_TOKEN=}")
22+
print("=" * 40)
23+
2024
# Retry parameters.
2125
MAX_RETRIES: int = int(os.environ.get("MAX_RETRIES", 3))
2226
INTERVAL: int = int(os.environ.get("INTERVAL", 5))

examples/send_webhook.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Requires python3.8 and up!
2+
3+
import asyncio
4+
from http import HTTPStatus
5+
from pprint import pprint
6+
7+
import httpx
8+
9+
10+
async def send_webhook() -> None:
11+
12+
wh_payload = {
13+
"to_url": "https://webhook.site/f864d28d-9162-4ad5-9205-458e2b561c07",
14+
"to_auth": "",
15+
"tag": "Dhaka",
16+
"group": "Bangladesh",
17+
"payload": {"greetings": "Hello, world!"},
18+
}
19+
20+
async with httpx.AsyncClient(http2=True) as session:
21+
headers = {
22+
"Content-Type": "application/json",
23+
"Authorization": (
24+
"Token $5$1O/inyTZhNvFt.GW$Zfckz9OL.lm2wh3IewTm8YJ914wjz5txFnXG5XW.wb4"
25+
),
26+
}
27+
28+
response = await session.post(
29+
"http://localhost:5000/hook_slinger",
30+
headers=headers,
31+
json=wh_payload,
32+
follow_redirects=True,
33+
)
34+
35+
# Hook Slinger returns http code 202, accepted, for a successful request.
36+
assert response.status_code == HTTPStatus.ACCEPTED
37+
result = response.json()
38+
pprint(result)
39+
40+
41+
if __name__ == "__main__":
42+
asyncio.run(send_webhook())

examples/send_webhook.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
curl -X 'POST' \
6+
'http://localhost:5000/hook_slinger/' \
7+
-H 'accept: application/json' \
8+
-H 'Authorization: Token $5$1O/inyTZhNvFt.GW$Zfckz9OL.lm2wh3IewTm8YJ914wjz5txFnXG5XW.wb4' \
9+
-H 'Content-Type: application/json' \
10+
-d '{
11+
"to_url": "https://webhook.site/37ad9530-59c3-430d-9db6-e68317321a9f",
12+
"to_auth": "",
13+
"tag": "Dhaka",
14+
"group": "Bangladesh",
15+
"payload": {
16+
"greetings": "Hello, world!"
17+
}
18+
}' | python -m json.tool

0 commit comments

Comments
 (0)