diff --git a/tableauserverclient/__init__.py b/tableauserverclient/__init__.py index c5840d7b6..30ec47981 100644 --- a/tableauserverclient/__init__.py +++ b/tableauserverclient/__init__.py @@ -4,7 +4,7 @@ SiteItem, TableauAuth, UserItem, ViewItem, WorkbookItem, UnpopulatedPropertyError, \ HourlyInterval, DailyInterval, WeeklyInterval, MonthlyInterval, IntervalItem, TaskItem, \ SubscriptionItem -from .server import RequestOptions, ImageRequestOptions, PDFRequestOptions, Filter, Sort, \ +from .server import RequestOptions, CSVRequestOptions, ImageRequestOptions, PDFRequestOptions, Filter, Sort, \ Server, ServerResponseError, MissingRequiredFieldError, NotSignedInError, Pager from ._version import get_versions __version__ = get_versions()['version'] diff --git a/tableauserverclient/server/__init__.py b/tableauserverclient/server/__init__.py index 12a640723..704fdb66a 100644 --- a/tableauserverclient/server/__init__.py +++ b/tableauserverclient/server/__init__.py @@ -1,5 +1,5 @@ from .request_factory import RequestFactory -from .request_options import ImageRequestOptions, PDFRequestOptions, RequestOptions +from .request_options import CSVRequestOptions, ImageRequestOptions, PDFRequestOptions, RequestOptions from .filter import Filter from .sort import Sort from .. import ConnectionItem, DatasourceItem, JobItem, \ diff --git a/tableauserverclient/server/endpoint/views_endpoint.py b/tableauserverclient/server/endpoint/views_endpoint.py index 0335ce781..62cd3af50 100644 --- a/tableauserverclient/server/endpoint/views_endpoint.py +++ b/tableauserverclient/server/endpoint/views_endpoint.py @@ -105,7 +105,7 @@ def csv_fetcher(): def _get_view_csv(self, view_item, req_options): url = "{0}/{1}/data".format(self.baseurl, view_item.id) - with closing(self.get_request(url, parameters={"stream": True})) as server_response: + with closing(self.get_request(url, request_object=req_options, parameters={"stream": True})) as server_response: csv = server_response.iter_content(1024) return csv diff --git a/tableauserverclient/server/request_options.py b/tableauserverclient/server/request_options.py index 37f23f54c..b7d5c591d 100644 --- a/tableauserverclient/server/request_options.py +++ b/tableauserverclient/server/request_options.py @@ -62,12 +62,37 @@ def apply_query_params(self, url): return "{0}?{1}".format(url, '&'.join(params)) -class ImageRequestOptions(RequestOptionsBase): +class _FilterOptionsBase(RequestOptionsBase): + """ Provide a basic implementation of adding view filters to the url """ + def __init__(self): + self.view_filters = [] + + def apply_query_params(self, url): + raise NotImplementedError() + + def vf(self, name, value): + self.view_filters.append((name, value)) + return self + + def _append_view_filters(self, params): + for name, value in self.view_filters: + params.append('vf_{}={}'.format(name, value)) + + +class CSVRequestOptions(_FilterOptionsBase): + def apply_query_params(self, url): + params = [] + self._append_view_filters(params) + return "{0}?{1}".format(url, '&'.join(params)) + + +class ImageRequestOptions(_FilterOptionsBase): # if 'high' isn't specified, the REST API endpoint returns an image with standard resolution class Resolution: High = 'high' def __init__(self, imageresolution=None): + super(ImageRequestOptions, self).__init__() self.image_resolution = imageresolution def apply_query_params(self, url): @@ -75,11 +100,12 @@ def apply_query_params(self, url): if self.image_resolution: params.append('resolution={0}'.format(self.image_resolution)) + self._append_view_filters(params) + return "{0}?{1}".format(url, '&'.join(params)) -class PDFRequestOptions(RequestOptionsBase): - # if 'high' isn't specified, the REST API endpoint returns an image with standard resolution +class PDFRequestOptions(_FilterOptionsBase): class PageType: A3 = "a3" A4 = "a4" @@ -100,6 +126,7 @@ class Orientation: Landscape = "landscape" def __init__(self, page_type=None, orientation=None): + super(PDFRequestOptions, self).__init__() self.page_type = page_type self.orientation = orientation @@ -111,4 +138,6 @@ def apply_query_params(self, url): if self.orientation: params.append('orientation={0}'.format(self.orientation)) + self._append_view_filters(params) + return "{0}?{1}".format(url, '&'.join(params))