Add notifications API endpoint

This commit is contained in:
thePR0M3TH3AN
2025-07-14 11:43:09 -04:00
parent 306480896e
commit d87d9ed59f
3 changed files with 45 additions and 1 deletions

View File

@@ -31,6 +31,7 @@ Keep this token secret. Every request must include it in the `Authorization` hea
- `GET /api/v1/totp/export` Export all TOTP entries as JSON. - `GET /api/v1/totp/export` Export all TOTP entries as JSON.
- `GET /api/v1/totp` Return current TOTP codes and remaining time. - `GET /api/v1/totp` Return current TOTP codes and remaining time.
- `GET /api/v1/stats` Return statistics about the active seed profile. - `GET /api/v1/stats` Return statistics about the active seed profile.
- `GET /api/v1/notifications` Retrieve and clear queued notifications.
- `GET /api/v1/parent-seed` Reveal the parent seed or save it with `?file=`. - `GET /api/v1/parent-seed` Reveal the parent seed or save it with `?file=`.
- `GET /api/v1/nostr/pubkey` Fetch the Nostr public key for the active seed. - `GET /api/v1/nostr/pubkey` Fetch the Nostr public key for the active seed.
- `POST /api/v1/checksum/verify` Verify the checksum of the running script. - `POST /api/v1/checksum/verify` Verify the checksum of the running script.
@@ -186,7 +187,16 @@ Get profile stats such as entry counts with `GET /api/v1/stats`:
```bash ```bash
curl -H "Authorization: Bearer <token>" \ curl -H "Authorization: Bearer <token>" \
http://127.0.0.1:8000/api/v1/stats http://127.0.0.1:8000/api/v1/stats
```
### Checking Notifications
Get queued messages with `GET /api/v1/notifications`:
```bash
curl -H "Authorization: Bearer <token>" \
http://127.0.0.1:8000/api/v1/notifications
``` ```
### Changing the Master Password ### Changing the Master Password

View File

@@ -6,6 +6,7 @@ import os
import tempfile import tempfile
from pathlib import Path from pathlib import Path
import secrets import secrets
import queue
from typing import Any, List, Optional from typing import Any, List, Optional
from fastapi import FastAPI, Header, HTTPException, Request, Response from fastapi import FastAPI, Header, HTTPException, Request, Response
@@ -379,6 +380,21 @@ def get_profile_stats(authorization: str | None = Header(None)) -> dict:
return _pm.get_profile_stats() return _pm.get_profile_stats()
@app.get("/api/v1/notifications")
def get_notifications(authorization: str | None = Header(None)) -> List[dict]:
"""Return and clear queued notifications."""
_check_token(authorization)
assert _pm is not None
notes = []
while True:
try:
note = _pm.notifications.get_nowait()
except queue.Empty:
break
notes.append({"level": note.level, "message": note.message})
return notes
@app.get("/api/v1/parent-seed") @app.get("/api/v1/parent-seed")
def get_parent_seed( def get_parent_seed(
authorization: str | None = Header(None), file: str | None = None authorization: str | None = Header(None), file: str | None = None

View File

@@ -0,0 +1,18 @@
from test_api import client
from types import SimpleNamespace
import queue
import seedpass.api as api
def test_notifications_endpoint(client):
cl, token = client
api._pm.notifications = queue.Queue()
api._pm.notifications.put(SimpleNamespace(message="m1", level="INFO"))
api._pm.notifications.put(SimpleNamespace(message="m2", level="WARNING"))
res = cl.get("/api/v1/notifications", headers={"Authorization": f"Bearer {token}"})
assert res.status_code == 200
assert res.json() == [
{"level": "INFO", "message": "m1"},
{"level": "WARNING", "message": "m2"},
]
assert api._pm.notifications.empty()