From 67e1c057bef9c9049558d8d1136d7d39d71b69c7 Mon Sep 17 00:00:00 2001 From: Thies Mueller Date: Fri, 19 Jun 2026 20:01:15 +0200 Subject: [PATCH] change to httpx --- app.py | 63 ++++++++++++++++++++++++++++-------------------- requirements.txt | 3 ++- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/app.py b/app.py index 1e3193b..99d0b38 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,6 @@ import time import jwt -import aiohttp +import httpx import asyncio import configparser @@ -82,7 +82,6 @@ def generate_apns_token(): async def send_notification( - session, device_token, title, body @@ -105,29 +104,44 @@ async def send_notification( headers = { "authorization": f"bearer {jwt_token}", "apns-topic": topic, - "apns-push-type": "alert", - "content-type": "application/json" + "apns-push-type": "alert" } url = f"{APNS_URL}/3/device/{device_token}" - async with session.post( - url, - json=payload, - headers=headers - ) as response: + async with httpx.AsyncClient( + http2=True, + timeout=30.0 + ) as client: - if response.status != 200: + response = await client.post( + url, + json=payload, + headers=headers + ) - error_text = await response.text() + if response.status_code != 200: print( - f"APNS Fehler {response.status} " - f"für {device_token}: {error_text}" + f"APNS Fehler " + 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: - print(f"Fehler beim Senden: {e}") + + print( + f"Fehler beim Senden " + f"an {device_token}: {e}" + ) @app.route("/api/registerDeviceToken", methods=["POST"]) def register_device_token(): @@ -219,22 +233,19 @@ def notify(): 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( - session=session, - device_token=token.device_token, - title=title, - body=notification_text - ) + tasks.append( + send_notification( + device_token=token.device_token, + title=title, + body=notification_text ) + ) - await asyncio.gather(*tasks) + await asyncio.gather(*tasks) asyncio.run(send_all()) diff --git a/requirements.txt b/requirements.txt index e7aead7..8eb5419 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,5 @@ flask flask_sqlalchemy aiohttp pyjwt -cryptography \ No newline at end of file +cryptography +httpx[http2] \ No newline at end of file