def _save_file_locally(self, archive_target_path):
        url_key = self._get_data_url_key()
        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(self._get_kind()))
            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, self._get_kind()))
            except ValueError:
                raise manager_exceptions.BadParametersError(
                    "URL {0} is malformed - can't download {1} archive".format(
                        data_url, self._get_kind()))

        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(self._get_kind(),
                                                       url_key))
            uploaded_file_data = request.data
            with open(archive_target_path, 'w') as f:
                f.write(uploaded_file_data)
Beispiel #2
0
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 ValueError:
            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)
 def _save_file_from_url(archive_target_path, data_url, data_type):
     if any([request.data,
             'Transfer-Encoding' in request.headers,
             'blueprint_archive' in request.files]):
         raise manager_exceptions.BadParametersError(
             "Can't pass both a {0} URL via query parameters, request body"
             ", multi-form and chunked.".format(data_type))
     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 ValueError:
         raise manager_exceptions.BadParametersError(
                 "URL {0} is malformed - can't download {1} archive"
                 .format(data_url, data_type))