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)
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)
def _save_file_from_chunks(archive_target_path, data_type): if any([request.data, 'blueprint_archive' in request.files]): raise manager_exceptions.BadParametersError( "Can't pass both a {0} URL via request body , multi-form " "and chunked.".format(data_type)) with open(archive_target_path, 'w') as f: for buffered_chunked in chunked.decode(request.input_stream): f.write(buffered_chunked)
def _save_file_from_chunks(archive_target_path, data_type): if request.data or 'blueprint_archive' in request.files: raise manager_exceptions.BadParametersError( "Can pass {0} as only one of: request body, multi-form or " "chunked.".format(data_type)) with open(archive_target_path, 'w') as f: for buffered_chunked in chunked.decode(request.input_stream): f.write(buffered_chunked)
def _save_file_locally(self, archive_file_name): # save uploaded file if 'Transfer-Encoding' in request.headers: with open(archive_file_name, '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 application archive in request body') uploaded_file_data = request.data with open(archive_file_name, 'w') as f: f.write(uploaded_file_data)
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_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)