radical-api/routers/tracker.py
riley f5e0ac8cac API Security, Logging, & Restructure
- Added a .gitignore
- Implemented security.py (apikeys.py from server-api)
- Implemented wrapper.py (wrapper from radicalbot
- Added proper logging using log.conf
- Split tracker API endpoints into a router
- Renamed Target classes to Tracker
2021-08-23 02:14:40 -04:00

56 lines
No EOL
1.6 KiB
Python

from datetime import datetime
from fastapi import APIRouter, Depends
from fastapi.security.api_key import APIKey
from .security import get_api_key
from core.tracker import Tracker
router = APIRouter()
@router.get("/tracker/points")
def list_points(name: str, access_token: APIKey = Depends(get_api_key)):
try:
tracker = Tracker.from_data(name)
return tracker.points
except FileNotFoundError:
return {}
@router.get("/tracker/points/add")
def add_point(name: str, value: int, date_str: str = datetime.now().isoformat(),
access_token: APIKey = Depends(get_api_key)):
try:
tracker = Tracker.from_data(name)
except FileNotFoundError:
tracker = Tracker(name, [])
tracker.add_point(date_str, value)
return tracker.to_json()
@router.put("/tracker/points/modify")
def modify_point(name: str, date_str: str, value: int, access_token: APIKey = Depends(get_api_key)):
tracker = Tracker.from_data(name)
tracker.modify_point(date_str, value)
return 200
@router.delete("/tracker/points/delete")
def delete_point(name: str, date_str: str, access_token: APIKey = Depends(get_api_key)):
tracker = Tracker.from_data(name)
tracker.delete_point(date_str)
return 200
@router.get("/tracker/rename")
def rename_tracker(name: str, new_name: str, access_token: APIKey = Depends(get_api_key)):
tracker = Tracker.from_data(name)
tracker.rename(new_name)
return 200
@router.delete("/tracker/delete")
def delete_tracker(name: str, access_token: APIKey = Depends(get_api_key)):
tracker = Tracker.from_data(name)
tracker.delete()