Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add retry for fetch_url #8958

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions docs-website/download_historical_versions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import tarfile
import time
import urllib.request

repo_url = "https://api.github.com/repos/datahub-project/static-assets"
Expand All @@ -16,17 +17,30 @@ def download_file(url, destination):
f.write(chunk)


def fetch_urls(repo_url: str, folder_path: str, file_format: str):
def fetch_urls(
repo_url: str, folder_path: str, file_format: str, max_retries=3, retry_delay=5
):
api_url = f"{repo_url}/contents/{folder_path}"
response = urllib.request.urlopen(api_url)
data = response.read().decode("utf-8")
urls = [
file["download_url"]
for file in json.loads(data)
if file["name"].endswith(file_format)
]
print(urls)
return urls
for attempt in range(max_retries + 1):
try:
response = urllib.request.urlopen(api_url)
if response.status == 403 or (500 <= response.status < 600):
raise Exception(f"HTTP Error {response.status}: {response.reason}")
data = response.read().decode("utf-8")
urls = [
file["download_url"]
for file in json.loads(data)
if file["name"].endswith(file_format)
]
print(urls)
return urls
except Exception as e:
if attempt < max_retries:
print(f"Attempt {attempt + 1}/{max_retries}: {e}")
time.sleep(retry_delay)
else:
print(f"Max retries reached. Unable to fetch data.")
raise


def extract_tar_file(destination_path):
Expand Down
Loading