def save_request_content_to_file(request, archive_target_path, url_key,
                                 data_type='unknown'):
    """
    Retrieves the file specified by the request to the local machine.

    :param request: the request received by the rest client
    :param archive_target_path: the target of the archive
    :param data_type: the kind of the data (e.g. 'blueprint')
    :param url_key: if the data is passed as a url to an online resource, the
    url_key specifies what header points to the requested url.
    :return: None
    """
    if url_key in request.args:
        if request.data or 'Transfer-Encoding' in request.headers:
            raise manager_exceptions.BadParametersError(
                    "Can't pass both a {0} URL via query parameters "
                    "and {0} data via the request body at the same time"
                    .format(data_type))
        data_url = request.args[url_key]
        try:
            with contextlib.closing(urlopen(data_url)) as urlf:
                with open(archive_target_path, 'w') as f:
                    f.write(urlf.read())
        except URLError:
            raise manager_exceptions.ParamUrlNotFoundError(
                    "URL {0} not found - can't download {1} archive"
                    .format(data_url, data_type))
        except __HOLE__:
            raise manager_exceptions.BadParametersError(
                    "URL {0} is malformed - can't download {1} archive"
                    .format(data_url, data_type))

    elif 'Transfer-Encoding' in request.headers:
        with open(archive_target_path, 'w') as f:
            for buffered_chunked in chunked.decode(request.input_stream):
                f.write(buffered_chunked)
    else:
        if not request.data:
            raise manager_exceptions.BadParametersError(
                    'Missing {0} archive in request body or '
                    '"{1}" in query parameters'.format(data_type,
                                                       url_key))
        uploaded_file_data = request.data
        with open(archive_target_path, 'w') as f:
            f.write(uploaded_file_data)
dataset/ETHPy150Open cloudify-cosmo/cloudify-manager/rest-service/manager_rest/utils.py/save_request_content_to_file