partital fix

This commit is contained in:
2025-08-26 13:50:05 -07:00
parent 984f28a7c2
commit f014211d9c
3 changed files with 20 additions and 21 deletions

View File

@@ -94,14 +94,28 @@ def get_activity_details(activity_id):
@app.route('/activities/<activity_id>/download', methods=['GET'])
def download_activity(activity_id):
"""Endpoint to download activity data."""
"""Endpoint to download activity data with retry logic."""
api = init_api()
if not api:
return jsonify({"error": "Failed to connect to Garmin API"}), 500
try:
format = request.args.get('format', 'fit') # Default to FIT format
file_data = api.download_activity(activity_id, format=format)
file_data = None
# Implement exponential backoff retry (1s, 2s, 4s)
for attempt in range(3):
try:
file_data = api.download_activity(activity_id, dl_fmt=format)
break # Success, break out of retry loop
except Exception as e:
wait = 2 ** attempt
logger.warning(f"Download attempt {attempt+1}/3 failed, retrying in {wait}s: {str(e)}")
time.sleep(wait)
if file_data is None:
return jsonify({"error": "Activity download failed after 3 attempts"}), 500
return send_file(
io.BytesIO(file_data),
mimetype='application/octet-stream',

View File

@@ -1,3 +1,3 @@
garminconnect==0.2.28
garminconnect>=0.2.28
garth
Flask

View File

@@ -147,24 +147,9 @@ func (c *Client) DownloadActivity(activityID int, format string) ([]byte, error)
return nil, err
}
var resp *http.Response
reqErr := error(nil)
for i := 0; i <= c.retries; i++ {
resp, reqErr = c.httpClient.Do(req)
if reqErr != nil || (resp != nil && resp.StatusCode >= 500) {
if i < c.retries {
backoff := time.Duration(math.Pow(2, float64(i))) * time.Second
log.Printf("Download failed (attempt %d/%d), retrying in %v: %v", i+1, c.retries, backoff, reqErr)
time.Sleep(backoff)
continue
}
}
break
}
if reqErr != nil {
return nil, fmt.Errorf("download failed after %d retries: %w", c.retries, reqErr)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("download failed: %w", err)
}
defer resp.Body.Close()