Skip to content

Commit

Permalink
Merge pull request #324 from fronzbot/http-adapter
Browse files Browse the repository at this point in the history
Modify session to use HTTPAdapter and handle retries
  • Loading branch information
fronzbot authored Jul 2, 2020
2 parents f6dc22f + a030f46 commit 391373b
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions blinkpy/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import logging
from functools import partial
from requests import Request, Session, exceptions
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from blinkpy import api
from blinkpy.helpers import util
from blinkpy.helpers.constants import BLINK_URL, LOGIN_ENDPOINT, TIMEOUT
Expand Down Expand Up @@ -56,6 +58,16 @@ def header(self):
def create_session(self):
"""Create a session for blink communication."""
sess = Session()
assert_status_hook = [
lambda response, *args, **kwargs: response.raise_for_status()
]
sess.hooks["response"] = assert_status_hook
retry = Retry(
total=3, backoff_factor=1, status_forcelist=[429, 500, 502, 503, 504]
)
adapter = HTTPAdapter(max_retries=retry)
sess.mount("https://", adapter)
sess.mount("http://", adapter)
sess.get = partial(sess.get, timeout=TIMEOUT)
return sess

Expand Down

0 comments on commit 391373b

Please sign in to comment.