Skip to content

Commit 884a6ab

Browse files
t8y8Russell Hay
authored andcommitted
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 b62e43b commit 884a6ab

5 files changed

Lines changed: 25 additions & 21 deletions

File tree

tableauserverclient/server/endpoint/datasources_endpoint.py

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

7171
# Download 1 datasource by id
7272
@api(version="2.0")
73-
@parameter_added_in(version="2.5", parameters=['extract_only'])
74-
def download(self, datasource_id, filepath=None, extract_only=False):
73+
@parameter_added_in(no_extract='2.5')
74+
def download(self, datasource_id, filepath=None, no_extract=False):
7575
if not datasource_id:
7676
error = "Datasource ID undefined."
7777
raise ValueError(error)
7878
url = "{0}/{1}/content".format(self.baseurl, datasource_id)
7979

80-
if extract_only:
80+
if no_extract:
8181
url += "?includeExtract=False"
8282

8383
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
@@ -76,14 +76,14 @@ def update(self, workbook_item):
7676

7777
# Download workbook contents with option of passing in filepath
7878
@api(version="2.0")
79-
@parameter_added_in(version="2.5", parameters=['extract_only'])
80-
def download(self, workbook_id, filepath=None, extract_only=False):
79+
@parameter_added_in(no_extract='2.5')
80+
def download(self, workbook_id, filepath=None, no_extract=False):
8181
if not workbook_id:
8282
error = "Workbook ID undefined."
8383
raise ValueError(error)
8484
url = "{0}/{1}/content".format(self.baseurl, workbook_id)
8585

86-
if extract_only:
86+
if no_extract:
8787
url += "?includeExtract=False"
8888

8989
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
@@ -173,7 +173,7 @@ def test_download_extract_only(self):
173173
m.get(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/content?includeExtract=False',
174174
headers={'Content-Disposition': 'name="tableau_datasource"; filename="Sample datasource.tds"'},
175175
complete_qs=True)
176-
file_path = self.server.datasources.download('9dbd2263-16b5-46e1-9c43-a76bb8ab65fb', extract_only=True)
176+
file_path = self.server.datasources.download('9dbd2263-16b5-46e1-9c43-a76bb8ab65fb', no_extract=True)
177177
self.assertTrue(os.path.exists(file_path))
178178
os.remove(file_path)
179179

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)