8000 Merge pull request #1330 from jorwoods/jorwoods/download_hotfix · ivanB1975/server-client-python@666fcd8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 666fcd8

Browse files
authored
Merge pull request tableau#1330 from jorwoods/jorwoods/download_hotfix
Jorwoods/1312 download hotfix
2 parents b5f5caa + 19a9f51 commit 666fcd8

File tree

8 files changed

+59
-1
lines changed

8 files changed

+59
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var/
2424
*.egg-info/
2525
.installed.cfg
2626
*.egg
27+
pip-wheel-metadata/
2728

2829
# PyInstaller
2930
# Usually these files are written by a python script from a template
@@ -89,6 +90,7 @@ env.py
8990
# virtualenv
9091
venv/
9192
ENV/
93+
.venv/
9294

9395
# Spyder project settings
9496
.spyderproject
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from copy import deepcopy
2+
from urllib.parse import unquote_plus
3+
4+
5+
def fix_filename(params):
6+
if "filename*" not in params:
7+
return params
8+
9+
params = deepcopy(params)
10+
filename = params["filename*"]
11+
prefix = "UTF-8''"
12+
if filename.startswith(prefix):
13+
filename = filename[len(prefix) :]
14+
15+
params["filename"] = unquote_plus(filename)
16+
del params["filename*"]
17+
return params

tableauserverclient/server/endpoint/datasources_endpoint.py

Lines changed: 3 additions & 0 deletions
Original file li 8000 ne numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from pathlib import Path
99
from typing import List, Mapping, Optional, Sequence, Tuple, TYPE_CHECKING, Union
1010

11+
from tableauserverclient.helpers.headers import fix_filename
12+
1113
if TYPE_CHECKING:
1214
from tableauserverclient.server import Server
1315
from tableauserverclient.models import PermissionsRule
@@ -441,6 +443,7 @@ def download_revision(
441443
filepath.write(chunk)
442444
return_path = filepath
443445
else:
446+
params = fix_filename(params)
444447
filename = to_filename(os.path.basename(params["filename"]))
445448
download_path = make_download_path(filepath, filename)
446449
with open(download_path, "wb") as f:

tableauserverclient/server/endpoint/flows_endpoint.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from pathlib import Path
88
from typing import Iterable, List, Optional, TYPE_CHECKING, Tuple, Union
99

10+
from tableauserverclient.helpers.headers import fix_filename
11+
1012
from .dqw_endpoint import _DataQualityWarningEndpoint
1113
from .endpoint import QuerysetEndpoint, api
1214
from .exceptions import InternalServerError, MissingRequiredFieldError
@@ -124,6 +126,7 @@ def download(self, flow_id: str, filepath: Optional[PathOrFileW] = None) -> Path
124126
filepath.write(chunk)
125127
return_path = filepath
126128
else:
129+
params = fix_filename(params)
127130
filename = to_filename(os.path.basename(params["filename"]))
128131
download_path = make_download_path(filepath, filename)
129132
with open(download_path, "wb") as f:

tableauserverclient/server/endpoint/workbooks_endpoint.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from contextlib import closing
77
from pathlib import Path
88

9+
from tableauserverclient.helpers.headers import fix_filename
10+
911
from .endpoint import QuerysetEndpoint, api, parameter_added_in
1012
from .exceptions import InternalServerError, MissingRequiredFieldError
1113
from .permissions_endpoint import _PermissionsEndpoint
@@ -455,7 +457,7 @@ def _get_workbook_revisions(
455457
def download_revision(
456458
self,
457459
workbook_id: str,
458-
revision_number: str,
460+
revision_number: Optional[str],
459461
filepath: Optional[PathOrFileW] = None,
460462
include_extract: bool = True,
461463
no_extract: Optional[bool] = None,
@@ -487,6 +489,7 @@ def download_revision(
487489
filepath.write(chunk)
488490
return_path = filepath
489491
else:
492+
params = fix_filename(params)
490493
filename = to_filename(os.path.basename(params["filename"]))
491494
download_path = make_download_path(filepath, filename)
492495
with open(download_path, "wb") as f:

test/test_datasource.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,3 +696,14 @@ def test_download_revision(self) -> None:
696696
)
697697
file_path = self.server.datasources.download_revision("9dbd2263-16b5-46e1-9c43-a76bb8ab65fb", "3", td)
698698
self.assertTrue(os.path.exists(file_path))
699+
700+
def test_bad_download_response(self) -> None:
701+
with requests_mock.mock() as m, tempfile.TemporaryDirectory() as td:
702+
m.get(
703+
self.baseurl + "/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/content",
704+
headers={
705+
"Content-Disposition": '''name="tableau_datasource"; filename*=UTF-8''"Sample datasource.tds"'''
706+
},
707+
)
708+
file_path = self.server.datasources.download("9dbd2263-16b5-46e1-9c43-a76bb8ab65fb", td)
709+
self.assertTrue(os.path.exists(file_path))

test/test_flow.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import requests_mock
3+
import tempfile
34
import unittest
45

56
from io import BytesIO
@@ -203,3 +204,12 @@ def test_refresh(self):
203204
self.assertEqual(refresh_job.flow_run.id, "e0c3067f-2333-4eee-8028-e0a56ca496f6")
204205
self.assertEqual(refresh_job.flow_run.flow_id, "92967d2d-c7e2-46d0-8847-4802df58f484")
205206
self.assertEqual(format_datetime(refresh_job.flow_run.started_at), "2018-05-22T13:00:29Z")
207+
208+
def test_bad_download_response(self) -> None:
209+
with requests_mock.mock() as m, tempfile.TemporaryDirectory() as td:
210+
m.get(
211+
self.baseurl + "/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/content",
212+
headers={"Content-Disposition": '''name="tableau_flow"; filename*=UTF-8''"Sample flow.tfl"'''},
213+
)
214+
file_path = self.server.flows.download("9dbd2263-16b5-46e1-9c43-a76bb8ab65fb", td)
215+
self.assertTrue(os.path.exists(file_path))

test/test_workbook.py

Lines changed: 9 additions & 0 deletions
32AA
Original file line numberDiff line numberDiff line change
@@ -932,3 +932,12 @@ def test_download_revision(self) -> None:
932932
)
933933
file_path = self.server.workbooks.download_revision("9dbd2263-16b5-46e1-9c43-a76bb8ab65fb", "3", td)
934934
self.assertTrue(os.path.exists(file_path))
935+
936+
def test_bad_download_response(self) -> None:
937+
with requests_mock.mock() as m, tempfile.TemporaryDirectory() as td:
938+
m.get(
939+
self.baseurl + "/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/content",
940+
headers={"Content-Disposition": '''name="tableau_workbook"; filename*=UTF-8''"Sample workbook.twb"'''},
941+
)
942+
file_path = self.server.workbooks.download("9dbd2263-16b5-46e1-9c43-a76bb8ab65fb", td)
943+
self.assertTrue(os.path.exists(file_path))

0 commit comments

Comments
 (0)
0