Commit ae600e35 authored by Yannik Buch's avatar Yannik Buch
Browse files

Added additional catching of exceptions as well as retries when it fails. I...

Added additional catching of exceptions as well as retries when it fails. I also edited the other exception that occured. I also changed the author behaviour. Reports in b2rc should now appear as written by bgnet bot
parent 72cba381
......@@ -113,7 +113,6 @@ class elog():
self._b2rc_session = None
self.usernameb2rc = ''
self.passwordb2rc = ''
self.alias = ''
if self.postMessageOfReport:
self._b2rc_session = requests.Session()
......@@ -126,7 +125,7 @@ class elog():
try:
self.alias = lines[2]
except BaseException:
self.alias = ''
self.alias = 'BGNet Bot'
else:
self.usernameb2rc = input("Username:")
self.passwordb2rc = getpass.getpass("Password:")
......@@ -207,8 +206,20 @@ class elog():
# #################################
def api_get(self, name, params=None):
"""Send GET request."""
result = self._b2rc_session.get(self._b2rc_url + name, params=params)
result.raise_for_status()
success = False
retries = 0
while True:
try:
result = self._b2rc_session.get(self._b2rc_url + name, params=params)
result.raise_for_status()
success = True
except:
time.sleep(10)
retries += 1
if success:
break
if retries >=10:
result.raise_for_status()
return result
def api_post(self, name, data, files=None):
......@@ -276,8 +287,20 @@ class elog():
with requests.Session() as s:
s.auth = requests.auth.HTTPBasicAuth(self.username, self.password)
url_str = "{}://{}:{}/{}/".format(self.prefix, self.hostname, self.port, self.logbook)
result = s.get(url_str)
result.raise_for_status()
success = False
retries = 0
while True:
try:
result = s.get(url_str)
result.raise_for_status()
success = True
except:
time.sleep(10)
retries += 1
if success:
break
if retries >=10:
result.raise_for_status()
return result.text
def postMessage(self, msg, ch, alias=None, avatar=None, emoji=None):
......@@ -294,8 +317,20 @@ class elog():
if avatar and avatar.startswith('http'):
data['avatar'] = avatar
result = self.api_post('/api/v1/chat.postMessage', data)
result.raise_for_status()
success = False
retries = 0
while True:
try:
result = self.api_post('/api/v1/chat.postMessage', data)
result.raise_for_status()
success = True
except:
time.sleep(10)
retries += 1
if success:
break
if retries >=10:
result.raise_for_status()
return result.json()
def postReport(self, url):
......@@ -315,7 +350,7 @@ class elog():
try:
html = self.search_elog()
except Exception as e:
print('search elog failed: {}' % e)
print('search elog failed')
continue
last_report_url = self.get_ref_from_html(html)
# TODO get time information and reference to uploaded report. Cant be sure that last report is this one
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment