Files
eamco_auto_api/app/routers/main.py

50 lines
1.5 KiB
Python

from fastapi import APIRouter, HTTPException
from database import session
from app.script.fuel_estimator import FuelEstimator
from app.script.temp_getter import fetch_and_store_daily_temp
router = APIRouter(
prefix="/main",
tags=["main"],
responses={404: {"description": "Not found"}},
)
@router.get("/temp", status_code=200)
def update_temp_manually():
"""
Manually triggers the fetch and storage of today's temperature.
This is useful for testing or for manual intervention if the cron job fails.
"""
try:
success = fetch_and_store_daily_temp()
if success:
session.commit()
return {"ok": True, "message": "Temperature updated or already exists."}
else:
# The function already rolled back, so just return an error
return HTTPException(status_code=500, detail="Failed to fetch temperature from the weather API.")
except Exception as e:
session.rollback()
raise HTTPException(status_code=500, detail=f"An unexpected server error occurred: {str(e)}")
@router.get("/update", status_code=200)
def update_all_customer_fuel_levels():
"""
This endpoint triggers the daily update for all customers.
It should be called once per day by a cron job or scheduler.
"""
try:
estimator = FuelEstimator()
result = estimator.run_daily_update()
return result
except Exception as e:
session.rollback()
# Log the exception e
return {"ok": False, "message": "An internal error occurred."}