forked from HearthSim/python-hearthstone
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stream XML data to a temp file with retries before decoding it as XML (…
…HearthSim#37) * Stream XML data to a temp file with retries before decoding it as XML * Fix locale multiplexing for load_globalstrings * Add test coverage for new XML download functions
- Loading branch information
Showing
9 changed files
with
209 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import requests | ||
|
||
|
||
class RetryException(Exception): | ||
pass | ||
|
||
|
||
def download_to_tempfile(url: str, fp) -> bool: | ||
try: | ||
with requests.get(url, stream=True) as r: | ||
if r.ok: | ||
for chunk in r.iter_content(chunk_size=8192): | ||
fp.write(chunk) | ||
|
||
return True | ||
elif 500 <= r.status_code < 600: | ||
raise RetryException() | ||
else: | ||
return False | ||
except requests.exceptions.RequestException: | ||
raise RetryException() | ||
|
||
|
||
def download_to_tempfile_retry(url: str, fp, retries: int = 3) -> bool: | ||
assert retries >= 0 | ||
|
||
try: | ||
return download_to_tempfile(url, fp) | ||
except RetryException: | ||
if retries: | ||
return download_to_tempfile_retry(url, fp, retries - 1) | ||
|
||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters