working
This commit is contained in:
@@ -170,5 +170,72 @@ async def download_activity(activity_id: str, db: Session = Depends(get_db)):
|
||||
# Re-raise HTTP exceptions as-is
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Error in download_activity for ID {activity_id}: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=f"Error downloading activity: {str(e)}")
|
||||
raise HTTPException(status_code=500, detail=f"Error downloading activity: {str(e)}")
|
||||
|
||||
# Import necessary auth dependencies
|
||||
from ..models.api_token import APIToken
|
||||
import garth
|
||||
import json
|
||||
from garth.auth_tokens import OAuth1Token, OAuth2Token
|
||||
|
||||
def _verify_garmin_session(db: Session):
|
||||
"""Helper to load token from DB and verify session with Garmin (Inline for now)."""
|
||||
token_record = db.query(APIToken).filter_by(token_type='garmin').first()
|
||||
if not (token_record and token_record.garth_oauth1_token and token_record.garth_oauth2_token):
|
||||
return False
|
||||
|
||||
try:
|
||||
oauth1_dict = json.loads(token_record.garth_oauth1_token)
|
||||
oauth2_dict = json.loads(token_record.garth_oauth2_token)
|
||||
|
||||
domain = oauth1_dict.get('domain')
|
||||
if domain:
|
||||
garth.configure(domain=domain)
|
||||
|
||||
garth.client.oauth1_token = OAuth1Token(**oauth1_dict)
|
||||
garth.client.oauth2_token = OAuth2Token(**oauth2_dict)
|
||||
|
||||
# Simple check or full profile get?
|
||||
# garth.UserProfile.get() # strict check
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Garth session load failed: {e}")
|
||||
return False
|
||||
|
||||
@router.post("/activities/{activity_id}/redownload")
|
||||
async def redownload_activity_endpoint(activity_id: str, db: Session = Depends(get_db)):
|
||||
"""
|
||||
Trigger a re-download of the activity file from Garmin.
|
||||
"""
|
||||
try:
|
||||
logger.info(f"Request to redownload activity {activity_id}")
|
||||
|
||||
from ..services.garmin.client import GarminClient
|
||||
from ..services.sync_app import SyncApp
|
||||
|
||||
# Verify Auth
|
||||
if not _verify_garmin_session(db):
|
||||
raise HTTPException(status_code=401, detail="Garmin not authenticated or tokens invalid. Please go to Setup.")
|
||||
|
||||
garmin_client = GarminClient()
|
||||
# Double check connection?
|
||||
if not garmin_client.check_connection():
|
||||
# Try refreshing? For now just fail if token load wasn't enough
|
||||
# But usually token load is enough.
|
||||
pass
|
||||
|
||||
sync_app = SyncApp(db, garmin_client)
|
||||
|
||||
success = sync_app.redownload_activity(activity_id)
|
||||
|
||||
if success:
|
||||
return {"message": f"Successfully redownloaded activity {activity_id}", "status": "success"}
|
||||
else:
|
||||
raise HTTPException(status_code=500, detail="Failed to redownload activity. Check logs for details.")
|
||||
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Error in redownload_activity_endpoint: {e}")
|
||||
raise HTTPException(status_code=500, detail=f"Error processing redownload: {str(e)}")
|
||||
Reference in New Issue
Block a user