Skip to content

Commit

Permalink
create union type for RequestOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
jacalata committed Jun 14, 2024
1 parent f625473 commit 3ce2471
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Union
import urllib

import tableauserverclient as TSC
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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:
Expand All @@ -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.<fieldname>"
value = value.replace("Parameters.", "")
# the filter values received from the url are already url encoded. tsc will encode them again.
Expand All @@ -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]:
Expand Down
6 changes: 4 additions & 2 deletions tabcmd/commands/datasources_and_workbooks/export_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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("&")
Expand Down

0 comments on commit 3ce2471

Please sign in to comment.