Commit 34015164 authored by Franziska Koehn's avatar Franziska Koehn
Browse files

prepare_rest and download_file_iter added

parent d6c63353
......@@ -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
: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
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 ="{}")
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
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