163-10-158-small-fixes #12

Open
sergiogimenez wants to merge 3 commits from 163-10-158-small-fixes into main
Showing only changes of commit ef17701a68 - Show all commits

View file

@ -17,6 +17,7 @@ import locale
import logging
from datetime import datetime
import time
SNAPSHOT_BASE = {
@ -31,7 +32,6 @@ SNAPSHOT_BASE = {
}
## Utility Functions ##
def logs(f):
def wrapper(*args, **kwargs):
@ -364,7 +364,7 @@ def generate_qr_code(url, disable_qr):
# TODO sanitize url, if url is like this, it fails
# url = 'http://127.0.0.1:8000/api/snapshot/'
def send_snapshot_to_devicehub(snapshot, token, url, ev_uuid, legacy, disable_qr):
def send_snapshot_to_devicehub(snapshot, token, url, ev_uuid, legacy, disable_qr, max_retries=5):
url_components = urllib.parse.urlparse(url)
ev_path = f"evidence/{ev_uuid}"
components = (url_components.scheme, url_components.netloc, ev_path, '', '', '')
@ -374,6 +374,9 @@ def send_snapshot_to_devicehub(snapshot, token, url, ev_uuid, legacy, disable_qr
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
retries = 0
while retries < max_retries:
try:
data = snapshot.encode('utf-8')
request = urllib.request.Request(url, data=data, headers=headers)
@ -398,11 +401,21 @@ def send_snapshot_to_devicehub(snapshot, token, url, ev_uuid, legacy, disable_qr
else:
generate_qr_code(ev_url, disable_qr)
print("url: {}".format(ev_url))
return
else:
logger.error(_("Snapshot %s not remotely sent to URL '%s'. Server responded with error:\n %s"), ev_uuid, url, response_text)
logger.error(
_("Snapshot %s not remotely sent to URL '%s'. Server responded with error:\n %s"), ev_uuid, url, response_text)
except Exception as e:
logger.error(_("Snapshot not remotely sent to URL '%s'. Do you have internet? Is your server up & running? Is the url token authorized?\n %s"), url, e)
logger.error(
_("Snapshot not remotely sent to URL '%s'. Do you have internet? Is your server up & running? Is the url token authorized?\n %s"), url, e)
retries += 1
if retries < max_retries:
logger.info(_("Retrying... (%d/%d)"), retries, max_retries)
time.sleep(5) # TODO arbitrary number of seconds.
logger.error(
_("Failed to send snapshot to URL '%s' after %d attempts"), url, max_retries)
def load_config(config_file="settings.ini"):