Coverage for src/data_api/api/routers/libraries.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-13 19:28 +0000

1from typing import Annotated 

2 

3from fastapi import APIRouter, HTTPException, Path, Response 

4 

5from data_api.api.errors import service_errors 

6from data_api.api.schemas.errors import LIVE_ERROR_RESPONSES, ErrorResponse 

7from data_api.api.schemas.libraries import ( 

8 LibraryLostAndFound, 

9 LibraryRssItem, 

10 LibraryRssType, 

11 LibrarySpace, 

12) 

13from data_api.api.schemas.responses import SNAPSHOT_RESPONSES 

14from data_api.runtime import get_runtime 

15 

16router = APIRouter() 

17 

18 

19@router.get( 

20 "/spaces", 

21 response_model=list[LibrarySpace], 

22 operation_id="getLibrarySpaces", 

23 responses=LIVE_ERROR_RESPONSES, 

24) 

25async def get_library_space_availability(): 

26 """ 

27 取得圖書館空間使用資訊。 

28 資料來源:[圖書館空間預約系統](https://libsms.lib.nthu.edu.tw/RWDAPI_New/GetDevUseStatus.aspx) 

29 """ 

30 with service_errors(): 

31 return await get_runtime().libraries.get_space_availability() 

32 

33 

34@router.get( 

35 "/lost-and-found", 

36 response_model=list[LibraryLostAndFound], 

37 operation_id="getLibraryLostAndFound", 

38 responses=LIVE_ERROR_RESPONSES, 

39) 

40async def get_library_lost_and_found_items(): 

41 """ 

42 取得圖書館失物招領資訊。 

43 資料來源:[圖書館失物招領系統](https://adage.lib.nthu.edu.tw/find) 

44 """ 

45 with service_errors(): 

46 return await get_runtime().libraries.get_lost_and_found_items() 

47 

48 

49@router.get( 

50 "/rss/{rss_type}", 

51 response_model=list[LibraryRssItem], 

52 operation_id="getLibraryRssData", 

53 responses={ 

54 **SNAPSHOT_RESPONSES, 

55 404: {"model": ErrorResponse, "description": "RSS feed not found"}, 

56 }, 

57) 

58async def get_library_rss_data( 

59 response: Response, 

60 rss_type: Annotated[ 

61 LibraryRssType, 

62 Path( 

63 description="RSS 類型:最新消息(news)、電子資源(eresources)、展覽及活動(exhibit)、南大與人社分館(branches)", 

64 ), 

65 ], 

66): 

67 """ 

68 取得指定圖書館的 RSS 資料。 

69 資料來源:[圖書館官網 RSS](https://www.lib.nthu.edu.tw/bulletin/RSS/index.html) 

70 """ 

71 commit_hash, items = await get_runtime().libraries.get_rss_items(rss_type.value) 

72 if items is None: 

73 raise HTTPException(status_code=404, detail="找不到 RSS 資料") 

74 

75 if commit_hash is not None: 

76 response.headers["X-Data-Commit-Hash"] = commit_hash 

77 return items