Commit 0bce0de6 authored by Sascha Herzinger's avatar Sascha Herzinger
Browse files

More explicit error messages for pic-sure API

parent 50b05a11
from time import sleep from time import sleep
import logging
import requests import requests
from fractalis import app from fractalis import app
logger = logging.getLogger(__name__)
def raise_for_status(r):
if r.status_code != requests.codes.ok:
error = f'PIC-SURE API reported an error: [{r.status_code}] {r.text}'
logger.exception(error)
raise RuntimeError(error)
def submit_query(query: str, server: str, token: str) -> int: def submit_query(query: str, server: str, token: str) -> int:
r = requests.post( r = requests.post(
url='{}/queryService/runQuery'.format(server), url='{}/queryService/runQuery'.format(server),
...@@ -15,14 +26,14 @@ def submit_query(query: str, server: str, token: str) -> int: ...@@ -15,14 +26,14 @@ def submit_query(query: str, server: str, token: str) -> int:
}, },
verify=app.config['ETL_VERIFY_SSL_CERT'] verify=app.config['ETL_VERIFY_SSL_CERT']
) )
r.raise_for_status() raise_for_status(r)
result_id = r.json()['resultId'] result_id = r.json()['resultId']
return result_id return result_id
def wait_for_completion(result_id: int, server, token): def wait_for_completion(result_id: int, server, token):
def _check_status(): def _check_status():
return requests.get( r = requests.get(
url='{}/resultService/resultStatus/{}'.format( url='{}/resultService/resultStatus/{}'.format(
server, result_id), server, result_id),
headers={ headers={
...@@ -30,9 +41,24 @@ def wait_for_completion(result_id: int, server, token): ...@@ -30,9 +41,24 @@ def wait_for_completion(result_id: int, server, token):
'Authorization': 'Bearer {}'.format(token) 'Authorization': 'Bearer {}'.format(token)
}, },
verify=app.config['ETL_VERIFY_SSL_CERT'] verify=app.config['ETL_VERIFY_SSL_CERT']
).json() )
while _check_status()['status'] == 'RUNNING': raise_for_status(r)
sleep(1) # noinspection PyBroadException
try:
return r.json()['status']
except Exception:
return r.text
while True:
status = _check_status()
if status == 'RUNNING':
sleep(1)
continue
elif status == 'AVAILABLE':
break
else:
error = f'PIC-SURE API reported an error: {status}'
logger.exception(error)
raise RuntimeError(error)
def get_data(result_id, server, token): def get_data(result_id, server, token):
...@@ -45,5 +71,9 @@ def get_data(result_id, server, token): ...@@ -45,5 +71,9 @@ def get_data(result_id, server, token):
}, },
verify=app.config['ETL_VERIFY_SSL_CERT'] verify=app.config['ETL_VERIFY_SSL_CERT']
) )
r.raise_for_status() raise_for_status(r)
if not r.text:
error = f'PIC-SURE API returned no data.'
logger.exception(error)
raise RuntimeError(error)
return r.text return 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