From 3ce2471211c06c61e78863f690e5355eb7db1bea Mon Sep 17 00:00:00 2001 From: Jac Fitzgerald Date: Thu, 13 Jun 2024 23:24:33 -0700 Subject: [PATCH] create union type for RequestOptions --- .../datasources_and_workbooks_command.py | 12 ++++++++---- .../datasources_and_workbooks/export_command.py | 6 ++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/tabcmd/commands/datasources_and_workbooks/datasources_and_workbooks_command.py b/tabcmd/commands/datasources_and_workbooks/datasources_and_workbooks_command.py index 30ea4776..52ad76ba 100644 --- a/tabcmd/commands/datasources_and_workbooks/datasources_and_workbooks_command.py +++ b/tabcmd/commands/datasources_and_workbooks/datasources_and_workbooks_command.py @@ -1,3 +1,4 @@ +from typing import Union import urllib import tableauserverclient as TSC @@ -6,6 +7,9 @@ from tabcmd.commands.server import Server from tabcmd.execution.localize import _ +# TODO: expose a base type for these +RequestOptions = Union[TSC.PDFRequestOptions, TSC.CSVRequestOptions, TSC.ImageRequestOptions] + class DatasourcesAndWorkbooks(Server): """ @@ -59,7 +63,7 @@ def get_ds_by_content_url(logger, server, datasource_content_url) -> TSC.Datasou return matching_datasources[0] @staticmethod - def apply_values_from_url_params(logger, request_options: TSC.PDFRequestOptions, url) -> None: + def apply_values_from_url_params(logger, request_options: RequestOptions, url: str) -> None: logger.debug(url) try: if "?" in url: @@ -83,7 +87,7 @@ def apply_values_from_url_params(logger, request_options: TSC.PDFRequestOptions, # this is called from within from_url_params, for each view_filter value @staticmethod - def apply_encoded_filter_value(logger, request_options, value): + def apply_encoded_filter_value(logger, request_options: RequestOptions, value: str) -> None: # the REST API doesn't appear to have the option to disambiguate with "Parameters." value = value.replace("Parameters.", "") # the filter values received from the url are already url encoded. tsc will encode them again. @@ -96,14 +100,14 @@ def apply_encoded_filter_value(logger, request_options, value): # from apply_options, which expects an un-encoded input, # or from apply_url_params via apply_encoded_filter_value which decodes the input @staticmethod - def apply_filter_value(logger, request_options: TSC.PDFRequestOptions, value: str) -> None: + def apply_filter_value(logger, request_options: RequestOptions, value: str) -> None: logger.debug("handling filter param {}".format(value)) data_filter = value.split("=") request_options.vf(data_filter[0], data_filter[1]) # this is called from within from_url_params, for each param value @staticmethod - def apply_options_in_url(logger, request_options: TSC.PDFRequestOptions, value: str) -> None: + def apply_options_in_url(logger, request_options: RequestOptions, value: str) -> None: logger.debug("handling url option {}".format(value)) setting = value.split("=") if ":iid" == setting[0]: diff --git a/tabcmd/commands/datasources_and_workbooks/export_command.py b/tabcmd/commands/datasources_and_workbooks/export_command.py index cfecb387..d8102fcb 100644 --- a/tabcmd/commands/datasources_and_workbooks/export_command.py +++ b/tabcmd/commands/datasources_and_workbooks/export_command.py @@ -4,7 +4,8 @@ from tabcmd.commands.constants import Errors from tabcmd.execution.localize import _ from tabcmd.execution.logger_config import log -from .datasources_and_workbooks_command import DatasourcesAndWorkbooks +from .datasources_and_workbooks_command import DatasourcesAndWorkbooks, RequestOptions + pagesize = TSC.PDFRequestOptions.PageType # type alias for brevity @@ -120,8 +121,9 @@ def run_command(cls, args): except Exception as e: Errors.exit_with_error(logger, "Error saving to file", e) + # TODO should make the ability to pass in filters as args available in GET command too? @staticmethod - def apply_filters_from_args(request_options: TSC.PDFRequestOptions, args, logger=None) -> None: + def apply_filters_from_args(request_options: RequestOptions, args, logger) -> None: if args.filter: logger.debug("filter = {}".format(args.filter)) params = args.filter.split("&")