mirror of
https://github.com/sstent/garminsync-go.git
synced 2026-01-25 16:42:45 +00:00
partital fix
This commit is contained in:
@@ -94,14 +94,28 @@ def get_activity_details(activity_id):
|
|||||||
|
|
||||||
@app.route('/activities/<activity_id>/download', methods=['GET'])
|
@app.route('/activities/<activity_id>/download', methods=['GET'])
|
||||||
def download_activity(activity_id):
|
def download_activity(activity_id):
|
||||||
"""Endpoint to download activity data."""
|
"""Endpoint to download activity data with retry logic."""
|
||||||
api = init_api()
|
api = init_api()
|
||||||
if not api:
|
if not api:
|
||||||
return jsonify({"error": "Failed to connect to Garmin API"}), 500
|
return jsonify({"error": "Failed to connect to Garmin API"}), 500
|
||||||
|
|
||||||
try:
|
try:
|
||||||
format = request.args.get('format', 'fit') # Default to FIT format
|
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(
|
return send_file(
|
||||||
io.BytesIO(file_data),
|
io.BytesIO(file_data),
|
||||||
mimetype='application/octet-stream',
|
mimetype='application/octet-stream',
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
garminconnect==0.2.28
|
garminconnect>=0.2.28
|
||||||
garth
|
garth
|
||||||
Flask
|
Flask
|
||||||
|
|||||||
@@ -147,24 +147,9 @@ func (c *Client) DownloadActivity(activityID int, format string) ([]byte, error)
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var resp *http.Response
|
resp, err := c.httpClient.Do(req)
|
||||||
reqErr := error(nil)
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("download failed: %w", err)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user