Commit 02160fba83
02160fba83e20e8e684cb5c50f2a2e5e1aa7f903
parent: 428a546c8a
Verified · cmc
cmc <hello@cleberg.net> · 2026-10-22 19:08 UTC
Add Qlty config; pass an APNSTarget to send_alert
The device token, environment, and topic always come from one Device.
Layout: unified · split
.qlty/.gitignore
added
+7
| @@ -0,0 +1,7 @@ |
| |
1 | * |
| |
2 | !configs |
| |
3 | !configs/** |
| |
4 | !hooks |
| |
5 | !hooks/** |
| |
6 | !qlty.toml |
| |
7 | !.gitignore |
.qlty/qlty.toml
added
+44
| @@ -0,0 +1,44 @@ |
| |
1 | config_version = "0" |
| |
2 | |
| |
3 | exclude_patterns = [ |
| |
4 | "**/vendor/**", |
| |
5 | "**/node_modules/**", |
| |
6 | "**/testdata/**", |
| |
7 | "**/dist/**", |
| |
8 | "**/.build/**", |
| |
9 | "**/build/**", |
| |
10 | "**/*.min.*", |
| |
11 | "**/generated/**", |
| |
12 | ".claude/**", |
| |
13 | "**/Pods/**", |
| |
14 | ] |
| |
15 | |
| |
16 | test_patterns = [ |
| |
17 | "**/test/**", |
| |
18 | "**/tests/**", |
| |
19 | "**/Tests/**", |
| |
20 | "**/*_test.*", |
| |
21 | "**/test_*.*", |
| |
22 | "**/*.test.*", |
| |
23 | "**/*Tests.swift", |
| |
24 | "e2e/**", |
| |
25 | ] |
| |
26 | |
| |
27 | [[source]] |
| |
28 | name = "default" |
| |
29 | default = true |
| |
30 | |
| |
31 | [[plugin]] |
| |
32 | name = "osv-scanner" |
| |
33 | |
| |
34 | [[plugin]] |
| |
35 | name = "trufflehog" |
| |
36 | |
| |
37 | # Early returns are guard clauses, which keep the happy path unindented. |
| |
38 | [smells.return_statements] |
| |
39 | enabled = false |
| |
40 | |
| |
41 | # Total file complexity is the sum of its functions, which function_complexity |
| |
42 | # already checks. |
| |
43 | [smells.file_complexity] |
| |
44 | enabled = false |
app/apns.py
+12 −6
| @@ -2,6 +2,7 @@ from __future__ import annotations |
| 2 | |
2 | |
| 3 | import json |
3 | import json |
| 4 | import time |
4 | import time |
| |
5 | from dataclasses import dataclass |
| 5 | from pathlib import Path |
6 | from pathlib import Path |
| 6 | |
7 | |
| 7 | import httpx |
8 | import httpx |
| @@ -10,6 +11,13 @@ import jwt |
| 10 | from app.config import settings |
11 | from app.config import settings |
| 11 | |
12 | |
| 12 | |
13 | |
| |
14 | @dataclass(frozen=True) |
| |
15 | class APNSTarget: |
| |
16 | device_token: str |
| |
17 | apns_env: str |
| |
18 | topic: str |
| |
19 | |
| |
20 | |
| 13 | class APNSClient: |
21 | class APNSClient: |
| 14 | def __init__(self) -> None: |
22 | def __init__(self) -> None: |
| 15 | self._key_text = Path(settings.apns_private_key_path).read_text() |
23 | self._key_text = Path(settings.apns_private_key_path).read_text() |
| @@ -35,16 +43,14 @@ class APNSClient: |
| 35 | |
43 | |
| 36 | async def send_alert( |
44 | async def send_alert( |
| 37 | self, |
45 | self, |
| |
46 | target: APNSTarget, |
| 38 | *, |
47 | *, |
| 39 | device_token: str, |
| |
| 40 | apns_env: str, |
| |
| 41 | topic: str, |
| |
| 42 | title: str, |
48 | title: str, |
| 43 | body: str, |
49 | body: str, |
| 44 | payload: dict, |
50 | payload: dict, |
| 45 | ) -> tuple[bool, str | None, str | None]: |
51 | ) -> tuple[bool, str | None, str | None]: |
| 46 | host = "https://api.push.apple.com" if apns_env == "production" else "https://api.sandbox.push.apple.com" |
52 | host = "https://api.push.apple.com" if target.apns_env == "production" else "https://api.sandbox.push.apple.com" |
| 47 | url = f"{host}/3/device/{device_token}" |
53 | url = f"{host}/3/device/{target.device_token}" |
| 48 | merged_payload = { |
54 | merged_payload = { |
| 49 | "aps": { |
55 | "aps": { |
| 50 | "alert": {"title": title, "body": body}, |
56 | "alert": {"title": title, "body": body}, |
| @@ -56,7 +62,7 @@ class APNSClient: |
| 56 | url, |
62 | url, |
| 57 | headers={ |
63 | headers={ |
| 58 | "authorization": f"bearer {self._bearer_token()}", |
64 | "authorization": f"bearer {self._bearer_token()}", |
| 59 | "apns-topic": topic, |
65 | "apns-topic": target.topic, |
| 60 | "apns-push-type": "alert", |
66 | "apns-push-type": "alert", |
| 61 | "apns-priority": "10", |
67 | "apns-priority": "10", |
| 62 | }, |
68 | }, |
app/routers/test.py
+2 −4
| @@ -2,7 +2,7 @@ from fastapi import APIRouter, Depends, HTTPException |
| 2 | from sqlalchemy import select |
2 | from sqlalchemy import select |
| 3 | from sqlalchemy.ext.asyncio import AsyncSession |
3 | from sqlalchemy.ext.asyncio import AsyncSession |
| 4 | |
4 | |
| 5 | from app.apns import APNSClient |
5 | from app.apns import APNSClient, APNSTarget |
| 6 | from app.db import get_db |
6 | from app.db import get_db |
| 7 | from app.models import Device |
7 | from app.models import Device |
| 8 | from app.security import require_api_key |
8 | from app.security import require_api_key |
| @@ -20,9 +20,7 @@ async def test_push(device_id: int, db: AsyncSession = Depends(get_db)) -> dict: |
| 20 | client = APNSClient() |
20 | client = APNSClient() |
| 21 | try: |
21 | try: |
| 22 | ok, apns_id, error = await client.send_alert( |
22 | ok, apns_id, error = await client.send_alert( |
| 23 | device_token=device.apns_token, |
23 | APNSTarget(device.apns_token, device.apns_env, device.bundle_id), |
| 24 | apns_env=device.apns_env, |
| |
| 25 | topic=device.bundle_id, |
| |
| 26 | title="Hutch test", |
24 | title="Hutch test", |
| 27 | body="Test push from hutch-notify", |
25 | body="Test push from hutch-notify", |
| 28 | payload={"event_type": "test", "deep_link": "hutch://home"}, |
26 | payload={"event_type": "test", "deep_link": "hutch://home"}, |