Skip to content

Commit 01eca16

Browse files
authored
Reword no_extract and update signature of added_in (#144)
`parameter_added_in` got a facelift and now takes keyword arguments, where the value represents the version for that keyword. It looks a little cleaner and makes the checking code much simpler. Also renamed the `extract_only` to be `no_extract` because it means the opposite of `extract_only`, oops.
1 parent c967cef commit 01eca16

File tree

5 files changed

+25
-21
lines changed

5 files changed

+25
-21
lines changed

tableauserverclient/server/endpoint/datasources_endpoint.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ def delete(self, datasource_id):
6565

6666
# Download 1 datasource by id
6767
@api(version="2.0")
68-
@parameter_added_in(version="2.5", parameters=['extract_only'])
69-
def download(self, datasource_id, filepath=None, extract_only=False):
68+
@parameter_added_in(no_extract='2.5')
69+
def download(self, datasource_id, filepath=None, no_extract=False):
7070
if not datasource_id:
7171
error = "Datasource ID undefined."
7272
raise ValueError(error)
7373
url = "{0}/{1}/content".format(self.baseurl, datasource_id)
7474

75-
if extract_only:
75+
if no_extract:
7676
url += "?includeExtract=False"
7777

7878
with closing(self.get_request(url, parameters={'stream': True})) as server_response:

tableauserverclient/server/endpoint/endpoint.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,34 +109,38 @@ def wrapper(self, *args, **kwargs):
109109
return _decorator
110110

111111

112-
def parameter_added_in(version, parameters):
112+
def parameter_added_in(**params):
113113
'''Annotate minimum versions for new parameters or request options on an endpoint.
114114
115115
The api decorator documents when an endpoint was added, this decorator annotates
116116
keyword arguments on endpoints that may control functionality added after an endpoint was introduced.
117117
118118
The REST API will ignore invalid parameters in most cases, so this raises a warning instead of throwing
119-
an exception
119+
an exception.
120+
121+
Args:
122+
Key/value pairs of the form `parameter`=`version`. Kwargs.
123+
Raises:
124+
UserWarning
125+
Returns:
126+
None
120127
121128
Example:
122129
>>> @api(version="2.0")
123-
>>> @parameter_added_in(version="2.5", parameters=['extract_only'])
130+
>>> @parameter_added_in(no_extract='2.5')
124131
>>> def download(self, workbook_id, filepath=None, extract_only=False):
125132
>>> ...
126133
'''
127134
def _decorator(func):
128135
@wraps(func)
129136
def wrapper(self, *args, **kwargs):
130-
params = set(parameters)
131-
invalid_params = params & set(kwargs)
132-
133-
if invalid_params:
134-
import warnings
135-
server_version = Version(self.parent_srv.version or "0.0")
136-
minimum_supported = Version(version)
137-
if server_version < minimum_supported:
138-
error = "The parameter(s) {!r} are not available in {} and will be ignored. Added in {}".format(
139-
invalid_params, server_version, minimum_supported)
137+
import warnings
138+
server_ver = Version(self.parent_srv.version or "0.0")
139+
params_to_check = set(params) & set(kwargs)
140+
for p in params_to_check:
141+
min_ver = Version(str(params[p]))
142+
if server_ver < min_ver:
143+
error = "{!r} not available in {}, it will be ignored. Added in {}".format(p, server_ver, min_ver)
140144
warnings.warn(error)
141145
return func(self, *args, **kwargs)
142146
return wrapper

tableauserverclient/server/endpoint/workbooks_endpoint.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,14 @@ def update(self, workbook_item):
9393

9494
# Download workbook contents with option of passing in filepath
9595
@api(version="2.0")
96-
@parameter_added_in(version="2.5", parameters=['extract_only'])
97-
def download(self, workbook_id, filepath=None, extract_only=False):
96+
@parameter_added_in(no_extract='2.5')
97+
def download(self, workbook_id, filepath=None, no_extract=False):
9898
if not workbook_id:
9999
error = "Workbook ID undefined."
100100
raise ValueError(error)
101101
url = "{0}/{1}/content".format(self.baseurl, workbook_id)
102102

103-
if extract_only:
103+
if no_extract:
104104
url += "?includeExtract=False"
105105

106106
with closing(self.get_request(url, parameters={"stream": True})) as server_response:

test/test_datasource.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def test_download_extract_only(self):
154154
m.get(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/content?includeExtract=False',
155155
headers={'Content-Disposition': 'name="tableau_datasource"; filename="Sample datasource.tds"'},
156156
complete_qs=True)
157-
file_path = self.server.datasources.download('9dbd2263-16b5-46e1-9c43-a76bb8ab65fb', extract_only=True)
157+
file_path = self.server.datasources.download('9dbd2263-16b5-46e1-9c43-a76bb8ab65fb', no_extract=True)
158158
self.assertTrue(os.path.exists(file_path))
159159
os.remove(file_path)
160160

test/test_workbook.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def test_download_extract_only(self):
180180
headers={'Content-Disposition': 'name="tableau_workbook"; filename="RESTAPISample.twbx"'},
181181
complete_qs=True)
182182
# Technically this shouldn't download a twbx, but we are interested in the qs, not the file
183-
file_path = self.server.workbooks.download('1f951daf-4061-451a-9df1-69a8062664f2', extract_only=True)
183+
file_path = self.server.workbooks.download('1f951daf-4061-451a-9df1-69a8062664f2', no_extract=True)
184184
self.assertTrue(os.path.exists(file_path))
185185
os.remove(file_path)
186186

0 commit comments

Comments
 (0)