Skip to content

Commit

Permalink
Improve retry logic
Browse files Browse the repository at this point in the history
  • Loading branch information
swails committed Apr 5, 2020
1 parent 919139d commit 6446595
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions wideq/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import datetime
import requests
import logging
from functools import lru_cache
from typing import Any, Dict, List, Tuple
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

GATEWAY_URL = 'https://kic.lgthinq.com:46030/api/common/gatewayUriList'
APP_KEY = 'wideq'
Expand Down Expand Up @@ -59,6 +62,32 @@ def get_wideq_logger() -> logging.Logger:
LOGGER = get_wideq_logger()


@lru_cache(maxsize=5)
def get_retry_session():
# See https://www.peterbe.com/plog/best-practice-with-retries-with-requests
# for the source of this retry mechanism
num_retries = 100 # fail *eventually*, but effectively retry in perpetuity
backoff_factor = 0.5
status_forcelist = (500, 502, 504)
session = requests.Session()
session.headers = {
'x-thinq-application-key': APP_KEY,
'x-thinq-security-key': SECURITY_KEY,
'Accept': 'application/json',
}
retry = Retry(
total=num_retries,
read=num_retries,
connect=num_retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session


def set_log_level(level: int):
logger = get_wideq_logger()
logger.setLevel(level)
Expand Down Expand Up @@ -163,18 +192,14 @@ def lgedm_post(url, data=None, access_token=None, session_id=None):
authenticated requests. They are not required, for example, to load
the gateway server data or to start a session.
"""

headers = {
'x-thinq-application-key': APP_KEY,
'x-thinq-security-key': SECURITY_KEY,
'Accept': 'application/json',
}
headers=dict()
if access_token:
headers['x-thinq-token'] = access_token
if session_id:
headers['x-thinq-jsessionId'] = session_id

res = requests.post(url, json={DATA_ROOT: data}, headers=headers)
session = get_retry_session()
res = session.post(url, json={DATA_ROOT: data}, headers=headers)
out = res.json()[DATA_ROOT]

# Check for API errors.
Expand Down Expand Up @@ -266,7 +291,7 @@ def refresh_auth(oauth_root, refresh_token):
'Accept': 'application/json',
}

res = requests.post(token_url, data=data, headers=headers)
res = get_retry_session().post(token_url, data=data, headers=headers)
res_data = res.json()

if res_data['status'] != 1:
Expand Down

0 comments on commit 6446595

Please sign in to comment.