Retry to connect to the API in case of failure
This commit is contained in:
parent
729cfcb7ac
commit
2aae2ecb37
1 changed files with 27 additions and 1 deletions
|
@ -6,6 +6,7 @@ import stat
|
||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
import smtplib
|
import smtplib
|
||||||
|
import time
|
||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
from requests.exceptions import HTTPError
|
from requests.exceptions import HTTPError
|
||||||
|
@ -27,7 +28,8 @@ class Re2oAPIClient:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, hostname, username, password, token_file=None,
|
def __init__(self, hostname, username, password, token_file=None,
|
||||||
use_tls=True, log_level=logging.CRITICAL+10):
|
use_tls=True, log_level=logging.CRITICAL+10, max_retries=0,
|
||||||
|
wait_time=10):
|
||||||
"""Creates an API client.
|
"""Creates an API client.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -43,6 +45,10 @@ class Re2oAPIClient:
|
||||||
(recommended for production). The default is `True`.
|
(recommended for production). The default is `True`.
|
||||||
log_level: Control the logging level to use. The default is
|
log_level: Control the logging level to use. The default is
|
||||||
`logging.CRITICAL+10`. So nothing is logged.
|
`logging.CRITICAL+10`. So nothing is logged.
|
||||||
|
max_retries: The maximum number of retries if the client cannot
|
||||||
|
connect to the API. The default is 0.
|
||||||
|
wait_time: The time to wait between two API tests. It is ignored if
|
||||||
|
`max_retries` is 0. The default value is 10 seconds.
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
requests.exceptions.ConnectionError: Unable to resolve the
|
requests.exceptions.ConnectionError: Unable to resolve the
|
||||||
|
@ -76,6 +82,9 @@ class Re2oAPIClient:
|
||||||
self.hostname = hostname
|
self.hostname = hostname
|
||||||
self._username = username
|
self._username = username
|
||||||
self._password = password
|
self._password = password
|
||||||
|
|
||||||
|
self._check_api(max_retries, wait_time)
|
||||||
|
|
||||||
# Try to fetch token from token file else get a new one from the
|
# Try to fetch token from token file else get a new one from the
|
||||||
# server
|
# server
|
||||||
try:
|
try:
|
||||||
|
@ -83,6 +92,23 @@ class Re2oAPIClient:
|
||||||
except exceptions.APIClientGenericError:
|
except exceptions.APIClientGenericError:
|
||||||
self._force_renew_token()
|
self._force_renew_token()
|
||||||
|
|
||||||
|
def _check_api(self, max_retries, wait_time):
|
||||||
|
"""Checks if the API is responding to our requests"""
|
||||||
|
api_root = self.get_url_for("")
|
||||||
|
self.log.debug(f'Trying to connect to "{api_root}"')
|
||||||
|
for i in range(max_retries + 1):
|
||||||
|
try:
|
||||||
|
r = requests.get(api_root)
|
||||||
|
r.raise_for_status()
|
||||||
|
return
|
||||||
|
except Exception as e:
|
||||||
|
self.log.warning(f"[{i}/{max_retries}] Unable to get an answer from {api_root}: {e}")
|
||||||
|
|
||||||
|
if i == max_retries:
|
||||||
|
raise e
|
||||||
|
else:
|
||||||
|
time.sleep(wait_time)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def need_renew_token(self):
|
def need_renew_token(self):
|
||||||
"""The token needs to be renewed.
|
"""The token needs to be renewed.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue