Skip to content

Commit

Permalink
Enhancement of the retrieve_image function.
Browse files Browse the repository at this point in the history
This enhancement permits to:
* Add additional debug messages
* Return true if the download is successful
  • Loading branch information
Pansanel committed Jun 13, 2017
1 parent c4522f2 commit ef594eb
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions cloudkeeper_os/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,25 @@
import requests
from requests.auth import HTTPBasicAuth

def retrieve_image(uri, filename, username=None, password=None, capath=None):
from oslo_log import log

LOG = log.getLogger(__name__)

def retrieve_image(uri, filename, username='', password='', capath=None):
"""Retrieve an image
"""
# TODO manage SSL case
if username and password:
auth = HTTPBasicAuth(username, password)
response = requests.get(uri, auth=auth, stream=True)
else:
response = requests.get(uri, stream=True)
LOG.info("Download image from %s" % uri)
auth = HTTPBasicAuth(username, password)
response = requests.get(uri, auth=auth, stream=True)
LOG.debug("Response: %s when accessing the image." % str(response.status_code))
if response.status_code == 200:
output = open(filename, 'wb')
# response.raw.decode_content = True
shutil.copyfileobj(response.raw, output)
output.close()
LOG.debug("Image data successfully saved to %s" % filename)
return True
else:
# TODO raise an exception
LOG.error("Failed to download image data due to HTTP error")
return False

0 comments on commit ef594eb

Please sign in to comment.