Skip to content

Commit

Permalink
try 3 times on imcomplete read
Browse files Browse the repository at this point in the history
  • Loading branch information
asauerwein-nts committed Jun 20, 2024
1 parent 7c94350 commit 597092d
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion download_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import smtplib
from email.message import EmailMessage
import logging
from time import sleep

# setup logger
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -96,7 +97,16 @@ def base_req(url: str, method="get", json={}):
# "x-tesla-user-agent": "TeslaApp/4.28.3-2167",
}
logging.info(f"{method} Request to url: {url}")
result = sess.request(method=method, url=url, headers=headers, json=json)
for attempt in range(3):
try:
result = sess.request(method=method, url=url, headers=headers, json=json)
break
except requests.exceptions.ChunkedEncodingError:
logger.warning(f"incomplete read occured, attempt {attempt} of 3")
sleep(1)
else:
logger.error(f"giving up after 3 tries")
exit(1)
result.raise_for_status()
if "application/json" in result.headers.get("Content-Type"):
return result.json()
Expand Down

0 comments on commit 597092d

Please sign in to comment.