Coverage for src/paperap/tests/create_samples.py: 0%

1"""

2

3 The integration testing framework gathers sample data in a better way than this script,

4 but in case it's needed in the future, I'm leaving it here for now.

5

6 ----------------------------------------------------------------------------

7

8 METADATA:

9

10 File: create_samples.py

11 Project: paperap

12 Created: 2025-03-11

13 Version: 0.0.5

14 Author: Jess Mann

15 Email: jess@jmann.me

16 Copyright (c) 2025 Jess Mann

17

18 ----------------------------------------------------------------------------

19

20 LAST MODIFIED:

21

22 2025-03-11 By Jess Mann

23

24"""

25 from __future__ import annotations

26

27 import json

28 import logging

29 import os

30 from pathlib import Path

31

32 import requests

33 from dotenv import load_dotenv

34 from requests.auth import HTTPBasicAuth

35

36 load_dotenv()

37

38# Configuration

39 API_BASE_URL = os.getenv("PAPERLESS_BASE_URL") + "/api/" # type: ignore

40 TOKEN = os.getenv("PAPERLESS_TOKEN")

41 SAVE_DIR = Path("tests/sample_data")

42# token auth

43 HEADERS = {"Authorization": f"Token {TOKEN}"}

44

45# Ensure save directory exists

46 SAVE_DIR.mkdir(parents=True, exist_ok=True)

47

48# Logging setup

49 logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

50

51 def fetch_api_root():

52 """Fetch the API root to determine available endpoints."""

53 response = requests.get(API_BASE_URL, headers=HEADERS)

54 response.raise_for_status()

55 return response.json()

56

57 def fetch_endpoint_data(endpoint_name, endpoint_url) -> None:

58 """Fetch a sample from the given endpoint and save the response to a file."""

59 try:

60 response = requests.get(endpoint_url, headers=HEADERS, params={"page_size": 1}) # Limit to 1 item

61 response.raise_for_status()

62 data = response.json()

63

64 # Save response to file

65 filename = SAVE_DIR / f"{endpoint_name}_list.json"

66 if filename.exists():

67 logging.warning(f"Skip overwriting existing sample data for {endpoint_name}")

68 return

69

70 with filename.open("w", encoding="utf-8") as file:

71 json.dump(data, file, indent=4)

72

73 logging.info(f"Saved sample data for {endpoint_name} to {filename}")

74

75 except requests.RequestException as e:

76 logging.error(f"Failed to fetch {endpoint_name}: {e}")

77

78 def main() -> None:

79 """Main function to extract and store API data."""

80 logging.info("Fetching API root...")

81 api_root = fetch_api_root()

82

83 for endpoint, url in api_root.items():

84 if isinstance(url, str) and url.startswith("http"):

85 logging.info(f"Fetching sample data from {endpoint}...")

86 fetch_endpoint_data(endpoint, url)

87

88 logging.info("Data collection complete.")

89

90 if __name__ == "__main__":

91 main()