Coverage for dakweb/routers/information.py: 70%

25 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2026-11-21 08:51 +0000

1# SPDX-License-Identifier: GPL-2.0-or-later 

2# © 2026, Ansgar 🙀 <ansgar@debian.org> 

3 

4import datetime 

5from email.utils import format_datetime 

6from typing import Annotated 

7 

8from fastapi import APIRouter, Depends, HTTPException, Path, Response, status 

9from fastapi.responses import PlainTextResponse 

10from sqlalchemy import select 

11from sqlalchemy.orm import Session 

12 

13from daklib.dbconn import Information 

14from dakweb.routers.deps import get_db 

15 

16router = APIRouter() 

17 

18 

19def _get_information(response: Response, db: Session, key: str) -> str: 

20 """ 

21 return `key` from the `Information` table or raise a `HTTPException` if not found 

22 """ 

23 row = db.execute( 

24 select(Information).where(Information.key == key) 

25 ).scalar_one_or_none() 

26 if row is None or row.value is None: 

27 raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Not found") 

28 

29 response.headers["Last-Modified"] = format_datetime( 

30 row.modified.astimezone(datetime.UTC), usegmt=True 

31 ) 

32 return row.value 

33 

34 

35@router.get("/dinstall.status", response_class=PlainTextResponse) 

36def get_dinstall_status(response: Response, db: Session = Depends(get_db)) -> str: 

37 return _get_information(response, db, Information.KEY_DINSTALL_STATUS) 

38 

39 

40@router.get( 

41 "/api/information/v1/mirrorstart/{archive}", response_class=PlainTextResponse 

42) 

43def get_mirrorstart( 

44 archive: Annotated[str, Path(max_length=128, pattern=r"^[a-z0-9-]+$")], 

45 response: Response, 

46 db: Session = Depends(get_db), 

47) -> str: 

48 return _get_information(response, db, f"mirrorstart.{archive}") 

49 

50 

51@router.get("/api/information/v1/trace/{archive}", response_class=PlainTextResponse) 

52def get_trace( 

53 archive: Annotated[str, Path(max_length=128, pattern=r"^[a-z0-9-]+$")], 

54 response: Response, 

55 db: Session = Depends(get_db), 

56) -> str: 

57 return _get_information(response, db, f"trace.{archive}")