Skip to content

Commit

Permalink
Merge pull request #2 from vote/chore/add-retry
Browse files Browse the repository at this point in the history
Add retry to requests
  • Loading branch information
benweissmann authored Aug 25, 2020
2 parents 5d7e71f + af712b0 commit 55c8c82
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions app/fill.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
SignatureBoundingBox,
)
from PIL import Image # type:ignore
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

retry_strategy = Retry(
total=3,
status_forcelist=[429, 500, 502, 503, 504],
method_whitelist=["HEAD", "GET", "OPTIONS", "PUT"],
backoff_factor=1,
)
adapter = HTTPAdapter(max_retries=retry_strategy)
http = requests.Session()
http.mount("https://", adapter)


# Add our bin/ folder to PATH
os.environ[
Expand Down Expand Up @@ -65,7 +78,7 @@ def handler(event: Any, context: Any):
signature = None
signature_url = event.get("signature")
if signature_url:
sig_response = requests.get(signature_url)
sig_response = http.get(signature_url)
sig_response.raise_for_status()
signature = Image.open(BytesIO(sig_response.content))

Expand All @@ -81,7 +94,7 @@ def handler(event: Any, context: Any):
)

with template.fill(data, signature) as filled_pdf:
res = requests.put(url=output_url, data=filled_pdf)
res = http.put(url=output_url, data=filled_pdf)

res.raise_for_status()

Expand Down

0 comments on commit 55c8c82

Please sign in to comment.