8000 Adds in maxage param to csv and pdf export options (#635) · rshide/server-client-python@ccd5a4f · GitHub
[go: up one dir, main page]

Skip to content

Commit ccd5a4f

Browse files
author
Chris Shin
authored
Adds in maxage param to csv and pdf export options (tableau#635)
* Adds in maxage param to csv and pdf export options * Fixes style issue * Adding named param to test to be clear
1 parent 7409d30 commit ccd5a4f

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

tableauserverclient/models/property_decorators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def property_is_int(range, allowed=None):
8484
def property_type_decorator(func):
8585
@wraps(func)
8686
def wrapper(self, value):
87-
error = "Invalid priority defined: {}.".format(value)
87+
error = "Invalid property defined: '{}'. Integer value expected.".format(value)
8888

8989
if range is None:
9090
if isinstance(value, int):

tableauserverclient/server/request_options.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from ..models.property_decorators import property_is_int
2+
3+
14
class RequestOptionsBase(object):
25
def apply_query_params(self, url):
36
raise NotImplementedError()
@@ -100,8 +103,24 @@ def _append_view_filters(self, params):
100103

101104

102105
class CSVRequestOptions(_FilterOptionsBase):
106+
def __init__(self, maxage=None):
107+
super(CSVRequestOptions, self).__init__()
108+
self.max_age = maxage
109+
110+
@property
111+
def max_age(self):
112+
return self._max_age
113+
114+
@max_age.setter
115+
@property_is_int(range=(0, 240))
116+
def max_age(self, value):
117+
self._max_age = value
118+
103119
def apply_query_params(self, url):
104120
params = []
121+
if self.max_age != 0:
122+
params.append('maxAge={0}'.format(self.max_age))
123+
105124
self._append_view_filters(params)
106125
return "{0}?{1}".format(url, '&'.join(params))
107126

@@ -116,11 +135,20 @@ def __init__(self, imageresolution=None, maxage=None):
116135
self.image_resolution = imageresolution
117136
self.max_age = maxage
118137

138+
@property
139+
def max_age(self):
140+
return self._max_age
141+
142+
@max_age.setter
143+
@property_is_int(range=(0, 240))
144+
def max_age(self, value):
145+
self._max_age = value
146+
119147
def apply_query_params(self, url):
120148
params = []
121149
if self.image_resolution:
122150
params.append('resolution={0}'.format(self.image_resolution))
123-
if self.max_age:
151+
if self.max_age != 0:
124152
params.append('maxAge={0}'.format(self.max_age))
125153

126154
self._append_view_filters(params)
@@ -148,10 +176,20 @@ class Orientation:
148176
Portrait = "portrait"
149177
Landscape = "landscape"
150178

151-
def __init__(self, page_type=None, orientation=None):
179+
def __init__(self, page_type=None, orientation=None, maxage=0):
152180
super(PDFRequestOptions, self).__init__()
153181
self.page_type = page_type
154182
self.orientation = orientation
183+
self.max_age = maxage
184+
185+
@property
186+
def max_age(self):
187+
return self._max_age
188+
189+
@max_age.setter
190+
@property_is_int(range=(0, 240))
191+
def max_age(self, value):
192+
self._max_age = value
155193

156194
def apply_query_params(self, url):
157195
params = []
@@ -161,6 +199,9 @@ def apply_query_params(self, url):
161199
if self.orientation:
162200
params.append('orientation={0}'.format(self.orientation))
163201

202+
if self.max_age != 0:
203+
params.append('maxAge={0}'.format(self.max_age))
204+
164205
self._append_view_filters(params)
165206

166207
return "{0}?{1}".format(url, '&'.join(params))

test/test_view.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,29 +129,30 @@ def test_populate_image(self):
129129
self.server.views.populate_image(single_view)
130130
self.assertEqual(response, single_view.image)
131131

132-
def test_populate_image_high_resolution(self):
132+
def test_populate_image_with_options(self):
133133
with open(POPULATE_PREVIEW_IMAGE, 'rb') as f:
134134
response = f.read()
135135
with requests_mock.mock() as m:
136-
m.get(self.baseurl + '/d79634e1-6063-4ec9-95ff-50acbf609ff5/image?resolution=high', content=response)
136+
m.get(self.baseurl + '/d79634e1-6063-4ec9-95ff-50acbf609ff5/image?resolution=high&maxAge=10',
137+
content=response)
137138
single_view = TSC.ViewItem()
138139
single_view._id = 'd79634e1-6063-4ec9-95ff-50acbf609ff5'
139-
req_option = TSC.ImageRequestOptions(imageresolution=TSC.ImageRequestOptions.Resolution.High)
140+
req_option = TSC.ImageRequestOptions(imageresolution=TSC.ImageRequestOptions.Resolution.High, maxage=10)
140141
self.server.views.populate_image(single_view, req_option)
141142
self.assertEqual(response, single_view.image)
142143

143144
def test_populate_pdf(self):
144145
with open(POPULATE_PDF, 'rb') as f:
145146
response = f.read()
146147
with requests_mock.mock() as m:
147-
m.get(self.baseurl + '/d79634e1-6063-4ec9-95ff-50acbf609ff5/pdf?type=letter&orientation=portrait',
148+
m.get(self.baseurl + '/d79634e1-6063-4ec9-95ff-50acbf609ff5/pdf?type=letter&orientation=portrait&maxAge=5',
148149
content=response)
149150
single_view = TSC.ViewItem()
150151
single_view._id = 'd79634e1-6063-4ec9-95ff-50acbf609ff5'
151152

152153
size = TSC.PDFRequestOptions.PageType.Letter
153154
orientation = TSC.PDFRequestOptions.Orientation.Portrait
154-
req_option = TSC.PDFRequestOptions(size, orientation)
155+
req_option = TSC.PDFRequestOptions(size, orientation, 5)
155156

156157
self.server.views.populate_pdf(single_view, req_option)
157158
self.assertEqual(response, single_view.pdf)
@@ -160,10 +161,11 @@ def test_populate_csv(self):
160161
with open(POPULATE_CSV, 'rb') as f:
161162
response = f.read()
162163
with requests_mock.mock() as m:
163-
m.get(self.baseurl + '/d79634e1-6063-4ec9-95ff-50acbf609ff5/data', content=response)
164+
m.get(self.baseurl + '/d79634e1-6063-4ec9-95ff-50acbf609ff5/data?maxAge=1', content=response)
164165
single_view = TSC.ViewItem()
165166
single_view._id = 'd79634e1-6063-4ec9-95ff-50acbf609ff5'
166-
self.server.views.populate_csv(single_view)
167+
request_option = TSC.CSVRequestOptions(maxage=1)
168+
self.server.views.populate_csv(single_view, request_option)
167169

168170
csv_file = b"".join(single_view.csv)
169171
self.assertEqual(response, csv_file)

0 commit comments

Comments
 (0)
0