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

Tested the code and made changes according to the bugs found

parent 07b1a950
......@@ -181,10 +181,14 @@ class elog():
if isinstance(requestToElog, requests.Request):
with requests.Session() as s:
s.auth = requestToElog.auth
s.request("GET", requestToElog.url)
prep = s.prepare_request(requestToElog)
settings = s.merge_environment_settings(prep.url, {}, None, None, None)
resp = s.send(prep, **settings)
try:
s.request("GET", requestToElog.url)
prep = s.prepare_request(requestToElog)
settings = s.merge_environment_settings(prep.url, {}, None, None, None)
resp = s.send(prep, **settings)
except Exception as e:
logging.error(traceback.format_exc())
return False
if resp.ok:
print("Elog Entry successfully transmitted to:")
print("{}".format(requestToElog.url))
......@@ -208,6 +212,7 @@ class elog():
# #################################
def api_get(self, name, params=None):
"""Send GET request."""
result = None
success = False
retries = 0
while True:
......@@ -223,12 +228,29 @@ class elog():
break
if retries >=10:
logging.warning('Elog timeout')
break
return result
def api_post(self, name, data, files=None):
"""Send POST request."""
result = self._b2rc_session.post(self._b2rc_url + name,
result = None
success = False
retries = 0
while True:
try:
result = self._b2rc_session.post(self._b2rc_url + name,
data=data, files=files)
result.raise_for_status()
success = True
except Exception as e:
logging.error(traceback.format_exc())
time.sleep(10)
retries += 1
if success:
break
if retries >=10:
logging.warning('Elog timeout')
break
return result
def logout(self):
......@@ -247,6 +269,9 @@ class elog():
result = self.api_post('/api/v1/login', data={'resume': token})
else:
result = self.api_post('/api/v1/login', data={'user': self.usernameb2rc, 'password': self.passwordb2rc})
if not result:
print('Could not reach b2rc')
return False
if result.status_code != 200 or result.json()['status'] != 'success':
print('B2rc api: Invalid authentication')
return False
......@@ -308,6 +333,7 @@ class elog():
break
if retries >=10:
logging.warning('B2rc posting timeout')
break
return result.text
def postMessage(self, msg, ch, alias=None, avatar=None, emoji=None):
......@@ -321,30 +347,20 @@ class elog():
data['emoji'] = ':'.join(('', str(emoji).strip(':'), ''))
if self.avatar and self.avatar.startswith('http'):
data['avatar'] = self.avatar
result = self.api_post('/api/v1/chat.postMessage', data)
if not result:
print('Could not reach b2rc')
return False
success = False
retries = 0
while True:
try:
result = self.api_post('/api/v1/chat.postMessage', data)
result.raise_for_status()
success = True
except Exception as e:
logging.error(traceback.format_exc())
time.sleep(10)
retries += 1
if success:
break
if retries >=10:
logging.warning('B2rc posting timeout')
return result.json()
return result.json()['success']
def postReport(self, url):
'''Define message text and channel.'''
text = '{} {}\n[{}]({})'.format(
self.starttime, self.type, url, url)
result = self.postMessage(text, '#bgnet')
if result['success'] is not True:
if result is not True:
raise RuntimeError('postReport: Sending shift report to b2rc failed')
return result
......@@ -356,7 +372,7 @@ class elog():
try:
html = self.search_elog()
except Exception as e:
print('search elog failed')
logging.error('An exception occured ')
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
......@@ -364,4 +380,5 @@ class elog():
self.postReport(last_report_url)
except Exception as e:
print('RocketChat: Sending shift report to b2rc failed: {}'.format(e))
return False
return True
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