Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
XNAT-Group
XNAT_Query_Client_XSA
Commits
34015164
Commit
34015164
authored
Jun 08, 2015
by
Franziska Koehn
Browse files
prepare_rest and download_file_iter added
parent
d6c63353
Changes
1
Hide whitespace changes
Inline
Side-by-side
xsa/queries.py
View file @
34015164
...
...
@@ -138,6 +138,44 @@ def download_file(url, creds, path):
raise
xsa
.
errors
.
DownloadError
(
"Error downloading file %s, Status Code %s"
%
(
url
,
e
))
return
False
def
download_file_iter
(
url
,
creds
):
"""
Downloads a file from given 'url'.
Returns Iter of response
Raises xsa.errors.WritingError and xsa.errors.DownloadError
**Parameters**
:url: str, host/REST-API with filled values
:creds: tuple(user-name, user-password), credentials
"""
import
requests
from
base64
import
b64encode
from
requests.auth
import
HTTPBasicAuth
user
,
passw
=
creds
try
:
response
=
requests
.
get
(
url
,
stream
=
True
,
auth
=
HTTPBasicAuth
(
user
,
passw
))
if
not
response
.
ok
:
raise
ValueError
(
response
.
status_code
)
return
response
.
iter_content
(
1024
)
except
IOError
as
e
:
raise
xsa
.
errors
.
WritingError
(
"Error writing file %s, %s"
%
(
path
,
e
))
except
ValueError
as
e
:
raise
xsa
.
errors
.
DownloadError
(
"Error downloading file %s, Status Code %s"
%
(
url
,
e
))
def
prepare_rest
(
result
,
rest
,
host
):
import
re
def
subfunc
(
f
):
key
=
f
.
group
(
0
).
strip
(
"{}"
)
return
result
[
key
]
ret
=
re
.
sub
(
'\{\w+\}'
,
subfunc
,
rest
)
return
"%s%s"
%
(
host
,
ret
)
def
retry_qry
(
fun
):
def
retry
(
*
args
,
**
kwargs
):
last_exception
=
None
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a 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