Merge "vFW and vDNS support added to azure-plugin"
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / utils / http.py
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License.  You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """
17 HTTP utilities.
18 """
19
20 import os
21 import tempfile
22
23 import requests
24
25
26 def download_file(url, destination=None, logger=None, progress_handler=None):
27     """
28     Download file.
29
30     :param url: URL from which to download
31     :type url: basestring
32     :param destination: path where the file should be saved or ``None`` to auto-generate
33     :type destination: basestring
34     :returns: path where the file was saved
35     :rtype: basestring
36     :raises exceptions.IOError:
37     :raises requests.exceptions.RequestException:
38     """
39     chunk_size = 1024
40
41     if not destination:
42         file_descriptor, destination = tempfile.mkstemp()
43         os.close(file_descriptor)
44     if logger:
45         logger.info('Downloading {0} to {1}...'.format(url, destination))
46
47     response = requests.get(url, stream=True)
48     final_url = response.url
49     if final_url != url and logger:
50         logger.debug('Redirected to {0}'.format(final_url))
51
52     read_bytes = 0
53     total_size = int(response.headers['Content-Length']) \
54         if 'Content-Length' in response.headers else None
55     try:
56         with open(destination, 'wb') as destination_file:
57             for chunk in response.iter_content(chunk_size):
58                 destination_file.write(chunk)
59                 if total_size and progress_handler:
60                     # Only showing progress bar if we have the total content length
61                     read_bytes += chunk_size
62                     progress_handler(read_bytes, total_size)
63     finally:
64         response.close()
65
66     return destination