mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-11 01:12:43 +00:00
[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:
@@ -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:
|
||||||
|
|||||||
@@ -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:
|
||||||
|
|||||||
@@ -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:
|
||||||
|
|||||||
@@ -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:
|
||||||
|
|||||||
@@ -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:
|
||||||
|
|||||||
@@ -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:
|
||||||
|
|||||||
@@ -495,21 +495,25 @@ 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."
|
||||||
)
|
)
|
||||||
|
for key, headers in headers_dict.items():
|
||||||
|
def get_header(name, default=None):
|
||||||
|
return headers_dict[key].get(name, default)
|
||||||
http_response = HTTPResponse(
|
http_response = HTTPResponse(
|
||||||
status=200,
|
status=200,
|
||||||
reason='OK',
|
reason='OK',
|
||||||
data=file_data,
|
data=file_data,
|
||||||
getheaders=get_headers,
|
getheaders=get_headers(headers),
|
||||||
getheader=get_header
|
getheader=get_header
|
||||||
)
|
)
|
||||||
# deserialize response to a file
|
# deserialize response to a file
|
||||||
|
|||||||
Reference in New Issue
Block a user