Skip to content

Commit

Permalink
TST : Add multiple retry on get_url (#1626)
Browse files Browse the repository at this point in the history
This reduces CI fails due to network issues
  • Loading branch information
pubpub-zz authored Feb 12, 2023
1 parent 7a807d2 commit 4bf3e32
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import ssl
import urllib.request
from typing import List
from urllib.error import HTTPError

from pypdf.generic import DictionaryObject, IndirectObject

Expand Down Expand Up @@ -30,10 +31,19 @@ def get_pdf_from_url(url: str, name: str) -> bytes:
cache_path = os.path.join(cache_dir, name)
if not os.path.exists(cache_path):
ssl._create_default_https_context = ssl._create_unverified_context
with urllib.request.urlopen(url) as response, open(
cache_path, "wb"
) as out_file:
out_file.write(response.read())
cpt = 3
while cpt > 0:
try:
with urllib.request.urlopen(url) as response, open(
cache_path, "wb"
) as out_file:
out_file.write(response.read())
cpt = 0
except HTTPError as e:
if cpt > 0:
cpt -= 1
else:
raise e
with open(cache_path, "rb") as fp:
data = fp.read()
return data
Expand Down

0 comments on commit 4bf3e32

Please sign in to comment.