Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Fractalis
fractalis
Commits
0bce0de6
Commit
0bce0de6
authored
Aug 14, 2018
by
Sascha Herzinger
Browse files
More explicit error messages for pic-sure API
parent
50b05a11
Changes
1
Hide whitespace changes
Inline
Side-by-side
fractalis/data/etls/picsure/shared.py
View file @
0bce0de6
from
time
import
sleep
import
logging
import
requests
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
:
r
=
requests
.
post
(
url
=
'{}/queryService/runQuery'
.
format
(
server
),
...
...
@@ -15,14 +26,14 @@ def submit_query(query: str, server: str, token: str) -> int:
},
verify
=
app
.
config
[
'ETL_VERIFY_SSL_CERT'
]
)
r
.
raise_for_status
()
raise_for_status
(
r
)
result_id
=
r
.
json
()[
'resultId'
]
return
result_id
def
wait_for_completion
(
result_id
:
int
,
server
,
token
):
def
_check_status
():
r
eturn
requests
.
get
(
r
=
requests
.
get
(
url
=
'{}/resultService/resultStatus/{}'
.
format
(
server
,
result_id
),
headers
=
{
...
...
@@ -30,9 +41,24 @@ def wait_for_completion(result_id: int, server, token):
'Authorization'
:
'Bearer {}'
.
format
(
token
)
},
verify
=
app
.
config
[
'ETL_VERIFY_SSL_CERT'
]
).
json
()
while
_check_status
()[
'status'
]
==
'RUNNING'
:
sleep
(
1
)
)
raise_for_status
(
r
)
# 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
):
...
...
@@ -45,5 +71,9 @@ def get_data(result_id, server, token):
},
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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment