change to httpx

This commit is contained in:
Thies Mueller
2026-06-19 20:01:15 +02:00
parent 8513224a04
commit 67e1c057be
2 changed files with 39 additions and 27 deletions
+37 -26
View File
@@ -1,6 +1,6 @@
import time import time
import jwt import jwt
import aiohttp import httpx
import asyncio import asyncio
import configparser import configparser
@@ -82,7 +82,6 @@ def generate_apns_token():
async def send_notification( async def send_notification(
session,
device_token, device_token,
title, title,
body body
@@ -105,29 +104,44 @@ async def send_notification(
headers = { headers = {
"authorization": f"bearer {jwt_token}", "authorization": f"bearer {jwt_token}",
"apns-topic": topic, "apns-topic": topic,
"apns-push-type": "alert", "apns-push-type": "alert"
"content-type": "application/json"
} }
url = f"{APNS_URL}/3/device/{device_token}" url = f"{APNS_URL}/3/device/{device_token}"
async with session.post( async with httpx.AsyncClient(
url, http2=True,
json=payload, timeout=30.0
headers=headers ) as client:
) as response:
if response.status != 200: response = await client.post(
url,
json=payload,
headers=headers
)
error_text = await response.text() if response.status_code != 200:
print( print(
f"APNS Fehler {response.status} " f"APNS Fehler "
f"für {device_token}: {error_text}" f"{response.status_code} "
f"für {device_token}: "
f"{response.text}"
)
else:
print(
f"Push erfolgreich "
f"an {device_token}"
) )
except Exception as e: except Exception as e:
print(f"Fehler beim Senden: {e}")
print(
f"Fehler beim Senden "
f"an {device_token}: {e}"
)
@app.route("/api/registerDeviceToken", methods=["POST"]) @app.route("/api/registerDeviceToken", methods=["POST"])
def register_device_token(): def register_device_token():
@@ -219,22 +233,19 @@ def notify():
async def send_all(): async def send_all():
async with aiohttp.ClientSession() as session: tasks = []
tasks = [] for token in DeviceToken.query.all():
for token in DeviceToken.query.all(): tasks.append(
send_notification(
tasks.append( device_token=token.device_token,
send_notification( title=title,
session=session, body=notification_text
device_token=token.device_token,
title=title,
body=notification_text
)
) )
)
await asyncio.gather(*tasks) await asyncio.gather(*tasks)
asyncio.run(send_all()) asyncio.run(send_all())
+2 -1
View File
@@ -5,4 +5,5 @@ flask
flask_sqlalchemy flask_sqlalchemy
aiohttp aiohttp
pyjwt pyjwt
cryptography cryptography
httpx[http2]