Commit b6e9e7a4 by kimth

Handle network errors more gracefully

parent ae11f918
...@@ -61,7 +61,7 @@ class XqueueInterface: ...@@ -61,7 +61,7 @@ class XqueueInterface:
def __init__(self, url=XQUEUE_URL, auth=XQUEUE_LMS_AUTH): def __init__(self, url=XQUEUE_URL, auth=XQUEUE_LMS_AUTH):
self.url = url self.url = url
self.auth = auth self.auth = auth
self.s = requests.session() self.session = requests.session()
self._login() self._login()
def send_to_queue(self, header, body, file_to_upload=None): def send_to_queue(self, header, body, file_to_upload=None):
...@@ -86,30 +86,31 @@ class XqueueInterface: ...@@ -86,30 +86,31 @@ class XqueueInterface:
return (error, msg) return (error, msg)
def _login(self): def _login(self):
try: payload = { 'username': self.auth['username'],
r = self.s.post(self.url+'/xqueue/login/', data={ 'username': self.auth['username'], 'password': self.auth['password'] }
'password': self.auth['password'] }) return self._http_post(self.url+'/xqueue/login/', payload)
except requests.exceptions.ConnectionError, err:
log.error(err)
return (1, 'cannot connect to server')
return parse_xreply(r.text)
def _send_to_queue(self, header, body, file_to_upload=None): def _send_to_queue(self, header, body, file_to_upload=None):
payload = {'xqueue_header': header, payload = {'xqueue_header': header,
'xqueue_body' : body} 'xqueue_body' : body}
files = None files = None
if file_to_upload is not None: if file_to_upload is not None:
files = { file_to_upload.name: file_to_upload } files = { file_to_upload.name: file_to_upload }
return self._http_post(self.url+'/xqueue/submit/', payload, files)
def _http_post(self, url, data, files=None):
try: try:
r = self.s.post(self.url+'/xqueue/submit/', data=payload, files=files) r = self.session.post(url, data=data, files=files)
except requests.exceptions.ConnectionError, err: except requests.exceptions.ConnectionError, err:
log.error(err) log.error(err)
return (1, 'cannot connect to server') return (1, 'cannot connect to server')
if r.status_code not in [200]:
return (1, 'unexpected HTTP status code [%d]' % r.status_code)
return parse_xreply(r.text) return parse_xreply(r.text)
......
Markdown is supported
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