Tilføj retries til URL-refresh ved manglende @microsoft.graph.downloadUrl i API svar
This commit is contained in:
@@ -112,22 +112,37 @@ def get_drive_id(app, site_id, drive_name):
|
|||||||
|
|
||||||
# --- Punkt 2: Resume / Chunked Download logic ---
|
# --- Punkt 2: Resume / Chunked Download logic ---
|
||||||
def get_fresh_download_url(app, drive_id, item_id):
|
def get_fresh_download_url(app, drive_id, item_id):
|
||||||
"""Fetches a fresh download URL for a specific item ID with token refresh support."""
|
"""Fetches a fresh download URL for a specific item ID with retries and robust error handling."""
|
||||||
url = f"https://graph.microsoft.com/v1.0/drives/{drive_id}/items/{item_id}?$select=id,@microsoft.graph.downloadUrl"
|
url = f"https://graph.microsoft.com/v1.0/drives/{drive_id}/items/{item_id}"
|
||||||
|
|
||||||
try:
|
for attempt in range(3):
|
||||||
headers = get_headers(app)
|
try:
|
||||||
response = requests.get(url, headers=headers, timeout=60)
|
headers = get_headers(app)
|
||||||
|
|
||||||
if response.status_code == 401:
|
|
||||||
logger.info("Access Token expired. Forcing refresh...")
|
|
||||||
headers = get_headers(app, force_refresh=True)
|
|
||||||
response = requests.get(url, headers=headers, timeout=60)
|
response = requests.get(url, headers=headers, timeout=60)
|
||||||
|
|
||||||
response.raise_for_status()
|
if response.status_code == 401:
|
||||||
return response.json().get('@microsoft.graph.downloadUrl'), None
|
logger.info(f"Access Token expired during refresh (Attempt {attempt+1}). Forcing refresh...")
|
||||||
except Exception as e:
|
headers = get_headers(app, force_refresh=True)
|
||||||
return None, str(e)
|
response = requests.get(url, headers=headers, timeout=60)
|
||||||
|
|
||||||
|
response.raise_for_status()
|
||||||
|
data = response.json()
|
||||||
|
download_url = data.get('@microsoft.graph.downloadUrl')
|
||||||
|
|
||||||
|
if download_url:
|
||||||
|
return download_url, None
|
||||||
|
|
||||||
|
# If item exists but URL is missing, it might be a transient SharePoint issue
|
||||||
|
logger.warning(f"Attempt {attempt+1}: '@microsoft.graph.downloadUrl' missing for {item_id}. Retrying in 1s...")
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
if attempt == 2:
|
||||||
|
return None, str(e)
|
||||||
|
logger.warning(f"Attempt {attempt+1} failed: {e}. Retrying...")
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
return None, "Item returned but '@microsoft.graph.downloadUrl' was missing after 3 attempts."
|
||||||
|
|
||||||
def download_single_file(app, drive_id, item_id, local_path, expected_size, display_name, remote_hash=None, initial_url=None):
|
def download_single_file(app, drive_id, item_id, local_path, expected_size, display_name, remote_hash=None, initial_url=None):
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user