Coverage for src/paperap/tests/create_samples.py: 0%
43 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-12 23:40 -0400
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-12 23:40 -0400
1"""
3The integration testing framework gathers sample data in a better way than this script,
4but in case it's needed in the future, I'm leaving it here for now.
6 ----------------------------------------------------------------------------
8 METADATA:
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
18 ----------------------------------------------------------------------------
20 LAST MODIFIED:
22 2025-03-11 By Jess Mann
24"""
25from __future__ import annotations
27import json
28import logging
29import os
30from pathlib import Path
32import requests
33from dotenv import load_dotenv
34from requests.auth import HTTPBasicAuth
36load_dotenv()
38# Configuration
39API_BASE_URL = os.getenv("PAPERLESS_BASE_URL") + "/api/" # type: ignore
40TOKEN = os.getenv("PAPERLESS_TOKEN")
41SAVE_DIR = Path("tests/sample_data")
42# token auth
43HEADERS = {"Authorization": f"Token {TOKEN}"}
45# Ensure save directory exists
46SAVE_DIR.mkdir(parents=True, exist_ok=True)
48# Logging setup
49logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
51def 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()
57def 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()
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
70 with filename.open("w", encoding="utf-8") as file:
71 json.dump(data, file, indent=4)
73 logging.info(f"Saved sample data for {endpoint_name} to {filename}")
75 except requests.RequestException as e:
76 logging.error(f"Failed to fetch {endpoint_name}: {e}")
78def main() -> None:
79 """Main function to extract and store API data."""
80 logging.info("Fetching API root...")
81 api_root = fetch_api_root()
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)
88 logging.info("Data collection complete.")
90if __name__ == "__main__":
91 main()