[Python] Add Support for Content-Disposition Header without filename (#11055)

* [Python] Add Support for CD header w/o filename

* updated documentation per PR instructions

* fixed formatting and updated samples and docs

* revert docs changes

* regenerate python samples

* updated sample python test
This commit is contained in:
Cameron Koegel
2022-03-08 12:10:49 -05:00
committed by GitHub
parent b1c6513015
commit 94b1440074
7 changed files with 83 additions and 37 deletions

View File

@@ -8,6 +8,7 @@ import os
import pprint import pprint
import re import re
import tempfile import tempfile
import uuid
from dateutil.parser import parse from dateutil.parser import parse
@@ -1082,7 +1083,13 @@ def deserialize_file(response_data, configuration, content_disposition=None):
if content_disposition: if content_disposition:
filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?',
content_disposition).group(1) content_disposition,
flags=re.I)
if filename is not None:
filename = filename.group(1)
else:
filename = "default_" + str(uuid.uuid4())
path = os.path.join(os.path.dirname(path), filename) path = os.path.join(os.path.dirname(path), filename)
with open(path, "wb") as f: with open(path, "wb") as f:

View File

@@ -16,6 +16,7 @@ import os
import pprint import pprint
import re import re
import tempfile import tempfile
import uuid
from dateutil.parser import parse from dateutil.parser import parse
@@ -1399,7 +1400,13 @@ def deserialize_file(response_data, configuration, content_disposition=None):
if content_disposition: if content_disposition:
filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?',
content_disposition).group(1) content_disposition,
flags=re.I)
if filename is not None:
filename = filename.group(1)
else:
filename = "default_" + str(uuid.uuid4())
path = os.path.join(os.path.dirname(path), filename) path = os.path.join(os.path.dirname(path), filename)
with open(path, "wb") as f: with open(path, "wb") as f:

View File

@@ -16,6 +16,7 @@ import os
import pprint import pprint
import re import re
import tempfile import tempfile
import uuid
from dateutil.parser import parse from dateutil.parser import parse
@@ -1399,7 +1400,13 @@ def deserialize_file(response_data, configuration, content_disposition=None):
if content_disposition: if content_disposition:
filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?',
content_disposition).group(1) content_disposition,
flags=re.I)
if filename is not None:
filename = filename.group(1)
else:
filename = "default_" + str(uuid.uuid4())
path = os.path.join(os.path.dirname(path), filename) path = os.path.join(os.path.dirname(path), filename)
with open(path, "wb") as f: with open(path, "wb") as f:

View File

@@ -16,6 +16,7 @@ import os
import pprint import pprint
import re import re
import tempfile import tempfile
import uuid
from dateutil.parser import parse from dateutil.parser import parse
@@ -1399,7 +1400,13 @@ def deserialize_file(response_data, configuration, content_disposition=None):
if content_disposition: if content_disposition:
filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?',
content_disposition).group(1) content_disposition,
flags=re.I)
if filename is not None:
filename = filename.group(1)
else:
filename = "default_" + str(uuid.uuid4())
path = os.path.join(os.path.dirname(path), filename) path = os.path.join(os.path.dirname(path), filename)
with open(path, "wb") as f: with open(path, "wb") as f:

View File

@@ -16,6 +16,7 @@ import os
import pprint import pprint
import re import re
import tempfile import tempfile
import uuid
from dateutil.parser import parse from dateutil.parser import parse
@@ -1399,7 +1400,13 @@ def deserialize_file(response_data, configuration, content_disposition=None):
if content_disposition: if content_disposition:
filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?',
content_disposition).group(1) content_disposition,
flags=re.I)
if filename is not None:
filename = filename.group(1)
else:
filename = "default_" + str(uuid.uuid4())
path = os.path.join(os.path.dirname(path), filename) path = os.path.join(os.path.dirname(path), filename)
with open(path, "wb") as f: with open(path, "wb") as f:

View File

@@ -16,6 +16,7 @@ import os
import pprint import pprint
import re import re
import tempfile import tempfile
import uuid
from dateutil.parser import parse from dateutil.parser import parse
@@ -1399,7 +1400,13 @@ def deserialize_file(response_data, configuration, content_disposition=None):
if content_disposition: if content_disposition:
filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?', filename = re.search(r'filename=[\'"]?([^\'"\s]+)[\'"]?',
content_disposition).group(1) content_disposition,
flags=re.I)
if filename is not None:
filename = filename.group(1)
else:
filename = "default_" + str(uuid.uuid4())
path = os.path.join(os.path.dirname(path), filename) path = os.path.join(os.path.dirname(path), filename)
with open(path, "wb") as f: with open(path, "wb") as f:

View File

@@ -495,42 +495,46 @@ class TestFakeApi(unittest.TestCase):
# sample from http://www.jtricks.com/download-text # sample from http://www.jtricks.com/download-text
file_name = 'content.txt' file_name = 'content.txt'
headers = {'Content-Disposition': 'attachment; filename={}'.format(file_name), 'Content-Type': 'text/plain'} headers_dict = {
def get_headers(): 'with_filename': {'Content-Disposition': 'attachment; filename={}'.format(file_name), 'Content-Type': 'text/plain'},
return headers 'no_filename': {'Content-Disposition': 'attachment;', 'Content-Type': 'text/plain'}
def get_header(name, default=None): }
return headers.get(name, default) def get_headers(*args):
return args
file_data = ( file_data = (
"You are reading text file that was supposed to be downloaded\r\n" "You are reading text file that was supposed to be downloaded\r\n"
"to your hard disk. If your browser offered to save you the file," "to your hard disk. If your browser offered to save you the file,"
"\r\nthen it handled the Content-Disposition header correctly." "\r\nthen it handled the Content-Disposition header correctly."
) )
http_response = HTTPResponse( for key, headers in headers_dict.items():
status=200, def get_header(name, default=None):
reason='OK', return headers_dict[key].get(name, default)
data=file_data, http_response = HTTPResponse(
getheaders=get_headers, status=200,
getheader=get_header reason='OK',
) data=file_data,
# deserialize response to a file getheaders=get_headers(headers),
mock_response = RESTResponse(http_response) getheader=get_header
with patch.object(RESTClientObject, 'request') as mock_method: )
mock_method.return_value = mock_response # deserialize response to a file
try: mock_response = RESTResponse(http_response)
file_object = self.api.download_attachment(file_name='download-text') with patch.object(RESTClientObject, 'request') as mock_method:
self.assert_request_called_with( mock_method.return_value = mock_response
mock_method, try:
'http://www.jtricks.com/download-text', file_object = self.api.download_attachment(file_name='download-text')
http_method='GET', self.assert_request_called_with(
accept='text/plain', mock_method,
content_type=None, 'http://www.jtricks.com/download-text',
) http_method='GET',
self.assertTrue(isinstance(file_object, file_type)) accept='text/plain',
self.assertFalse(file_object.closed) content_type=None,
self.assertEqual(file_object.read(), file_data.encode('utf-8')) )
finally: self.assertTrue(isinstance(file_object, file_type))
file_object.close() self.assertFalse(file_object.closed)
os.unlink(file_object.name) self.assertEqual(file_object.read(), file_data.encode('utf-8'))
finally:
file_object.close()
os.unlink(file_object.name)
def test_upload_download_file(self): def test_upload_download_file(self):
test_file_dir = os.path.realpath( test_file_dir = os.path.realpath(