From 4b5180d9be068df654f054fcde0f353a6d4353ab Mon Sep 17 00:00:00 2001 From: Jac Fitzgerald Date: Wed, 7 Dec 2022 18:59:53 -0800 Subject: [PATCH 1/3] add option to pass specific datasources --- tableauserverclient/server/endpoint/workbooks_endpoint.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tableauserverclient/server/endpoint/workbooks_endpoint.py b/tableauserverclient/server/endpoint/workbooks_endpoint.py index 4d7a4a2b5..27e357fe6 100644 --- a/tableauserverclient/server/endpoint/workbooks_endpoint.py +++ b/tableauserverclient/server/endpoint/workbooks_endpoint.py @@ -117,12 +117,13 @@ def create_extract( # delete all the extracts on 1 workbook @api(version="3.3") - def delete_extract(self, workbook_item: WorkbookItem, includeAll: bool = True) -> None: + def delete_extract(self, workbook_item: WorkbookItem, includeAll: bool = True, datasources=None) -> None: id_ = getattr(workbook_item, "id", workbook_item) url = "{0}/{1}/deleteExtract".format(self.baseurl, id_) - datasource_req = RequestFactory.Workbook.embedded_extract_req(includeAll, None) + datasource_req = RequestFactory.Workbook.embedded_extract_req(includeAll, datasources) server_response = self.post_request(url, datasource_req) new_job = JobItem.from_response(server_response.content, self.parent_srv.namespace)[0] + return new_job # Delete 1 workbook by id @api(version="2.0") From b7aaafc8e0cf385eef520015d26deed2fb558f97 Mon Sep 17 00:00:00 2001 From: Jac Fitzgerald Date: Mon, 12 Dec 2022 18:12:42 -0800 Subject: [PATCH 2/3] mypy --- tableauserverclient/server/endpoint/workbooks_endpoint.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tableauserverclient/server/endpoint/workbooks_endpoint.py b/tableauserverclient/server/endpoint/workbooks_endpoint.py index 27e357fe6..91205d202 100644 --- a/tableauserverclient/server/endpoint/workbooks_endpoint.py +++ b/tableauserverclient/server/endpoint/workbooks_endpoint.py @@ -117,7 +117,7 @@ def create_extract( # delete all the extracts on 1 workbook @api(version="3.3") - def delete_extract(self, workbook_item: WorkbookItem, includeAll: bool = True, datasources=None) -> None: + def delete_extract(self, workbook_item: WorkbookItem, includeAll: bool = True, datasources=None) -> JobItem: id_ = getattr(workbook_item, "id", workbook_item) url = "{0}/{1}/deleteExtract".format(self.baseurl, id_) datasource_req = RequestFactory.Workbook.embedded_extract_req(includeAll, datasources) From 35bd78aea8674eaf76e6b60317a4ac23aa8ffb91 Mon Sep 17 00:00:00 2001 From: Jac Fitzgerald Date: Mon, 12 Dec 2022 20:14:32 -0800 Subject: [PATCH 3/3] mypy no-implicit-optional --- tableauserverclient/models/datasource_item.py | 2 +- tableauserverclient/models/site_item.py | 6 +++--- tableauserverclient/models/workbook_item.py | 2 +- .../server/endpoint/datasources_endpoint.py | 8 ++++---- tableauserverclient/server/endpoint/endpoint.py | 2 +- tableauserverclient/server/endpoint/exceptions.py | 3 ++- .../server/endpoint/flows_endpoint.py | 2 +- .../server/endpoint/permissions_endpoint.py | 4 ++-- .../server/endpoint/schedules_endpoint.py | 8 ++++---- .../server/endpoint/users_endpoint.py | 12 ++++++------ .../server/endpoint/workbooks_endpoint.py | 6 +++--- tableauserverclient/server/request_factory.py | 4 ++-- 12 files changed, 30 insertions(+), 29 deletions(-) diff --git a/tableauserverclient/models/datasource_item.py b/tableauserverclient/models/datasource_item.py index 37ec1449a..4a7a74c4b 100644 --- a/tableauserverclient/models/datasource_item.py +++ b/tableauserverclient/models/datasource_item.py @@ -34,7 +34,7 @@ class AskDataEnablement: Disabled = "Disabled" SiteDefault = "SiteDefault" - def __init__(self, project_id: str, name: str = None) -> None: + def __init__(self, project_id: str, name: Optional[str] = None) -> None: self._ask_data_enablement = None self._certified = None self._certification_note = None diff --git a/tableauserverclient/models/site_item.py b/tableauserverclient/models/site_item.py index 8c9e8fe8e..e6bc3af24 100644 --- a/tableauserverclient/models/site_item.py +++ b/tableauserverclient/models/site_item.py @@ -50,9 +50,9 @@ def __init__( self, name: str, content_url: str, - admin_mode: str = None, - user_quota: int = None, - storage_quota: int = None, + admin_mode: Optional[str] = None, + user_quota: Optional[int] = None, + storage_quota: Optional[int] = None, disable_subscriptions: bool = False, subscribe_others_enabled: bool = True, revision_history_enabled: bool = False, diff --git a/tableauserverclient/models/workbook_item.py b/tableauserverclient/models/workbook_item.py index 0d18e770d..6d9a21b6b 100644 --- a/tableauserverclient/models/workbook_item.py +++ b/tableauserverclient/models/workbook_item.py @@ -33,7 +33,7 @@ class WorkbookItem(object): - def __init__(self, project_id: str, name: str = None, show_tabs: bool = False) -> None: + def __init__(self, project_id: str, name: Optional[str] = None, show_tabs: bool = False) -> None: self._connections = None self._content_url = None self._webpage_url = None diff --git a/tableauserverclient/server/endpoint/datasources_endpoint.py b/tableauserverclient/server/endpoint/datasources_endpoint.py index 022523aa4..5cea8fa5c 100644 --- a/tableauserverclient/server/endpoint/datasources_endpoint.py +++ b/tableauserverclient/server/endpoint/datasources_endpoint.py @@ -80,7 +80,7 @@ def baseurl(self) -> str: # Get all datasources @api(version="2.0") - def get(self, req_options: RequestOptions = None) -> Tuple[List[DatasourceItem], PaginationItem]: + def get(self, req_options: Optional[RequestOptions] = None) -> Tuple[List[DatasourceItem], PaginationItem]: logger.info("Querying all datasources on site") url = self.baseurl server_response = self.get_request(url, req_options) @@ -135,7 +135,7 @@ def delete(self, datasource_id: str) -> None: def download( self, datasource_id: str, - filepath: FilePath = None, + filepath: Optional[FilePath] = None, include_extract: bool = True, no_extract: Optional[bool] = None, ) -> str: @@ -234,8 +234,8 @@ def publish( datasource_item: DatasourceItem, file: PathOrFile, mode: str, - connection_credentials: ConnectionCredentials = None, - connections: Sequence[ConnectionItem] = None, + connection_credentials: Optional[ConnectionCredentials] = None, + connections: Optional[Sequence[ConnectionItem]] = None, as_job: bool = False, ) -> Union[DatasourceItem, JobItem]: diff --git a/tableauserverclient/server/endpoint/endpoint.py b/tableauserverclient/server/endpoint/endpoint.py index a836b000d..e04acc595 100644 --- a/tableauserverclient/server/endpoint/endpoint.py +++ b/tableauserverclient/server/endpoint/endpoint.py @@ -78,7 +78,7 @@ def _make_request( return server_response - def _check_status(self, server_response, url: str = None): + def _check_status(self, server_response, url: Optional[str] = None): if server_response.status_code >= 500: raise InternalServerError(server_response, url) elif server_response.status_code not in Success_codes: diff --git a/tableauserverclient/server/endpoint/exceptions.py b/tableauserverclient/server/endpoint/exceptions.py index 3ce0d5e92..d7b1d5ad2 100644 --- a/tableauserverclient/server/endpoint/exceptions.py +++ b/tableauserverclient/server/endpoint/exceptions.py @@ -1,4 +1,5 @@ from defusedxml.ElementTree import fromstring +from typing import Optional class TableauError(Exception): @@ -33,7 +34,7 @@ def from_response(cls, resp, ns, url=None): class InternalServerError(TableauError): - def __init__(self, server_response, request_url: str = None): + def __init__(self, server_response, request_url: Optional[str] = None): self.code = server_response.status_code self.content = server_response.content self.url = request_url or "server" diff --git a/tableauserverclient/server/endpoint/flows_endpoint.py b/tableauserverclient/server/endpoint/flows_endpoint.py index 2c54d17c4..931c85d06 100644 --- a/tableauserverclient/server/endpoint/flows_endpoint.py +++ b/tableauserverclient/server/endpoint/flows_endpoint.py @@ -94,7 +94,7 @@ def delete(self, flow_id: str) -> None: # Download 1 flow by id @api(version="3.3") - def download(self, flow_id: str, filepath: FilePath = None) -> str: + def download(self, flow_id: str, filepath: Optional[FilePath] = None) -> str: if not flow_id: error = "Flow ID undefined." raise ValueError(error) diff --git a/tableauserverclient/server/endpoint/permissions_endpoint.py b/tableauserverclient/server/endpoint/permissions_endpoint.py index f7c2f9f13..e3e9af2a6 100644 --- a/tableauserverclient/server/endpoint/permissions_endpoint.py +++ b/tableauserverclient/server/endpoint/permissions_endpoint.py @@ -6,7 +6,7 @@ from .exceptions import MissingRequiredFieldError from ...models import TableauItem -from typing import Callable, TYPE_CHECKING, List, Union +from typing import Optional, Callable, TYPE_CHECKING, List, Union logger = logging.getLogger(__name__) @@ -82,7 +82,7 @@ def permission_fetcher(): item._set_permissions(permission_fetcher) logger.info("Populated permissions for item (ID: {0})".format(item.id)) - def _get_permissions(self, item: TableauItem, req_options: "RequestOptions" = None): + def _get_permissions(self, item: TableauItem, req_options: Optional["RequestOptions"] = None): url = "{0}/{1}/permissions".format(self.owner_baseurl(), item.id) server_response = self.get_request(url, req_options) permissions = PermissionsRule.from_response(server_response.content, self.parent_srv.namespace) diff --git a/tableauserverclient/server/endpoint/schedules_endpoint.py b/tableauserverclient/server/endpoint/schedules_endpoint.py index 21c828989..65a55bcb6 100644 --- a/tableauserverclient/server/endpoint/schedules_endpoint.py +++ b/tableauserverclient/server/endpoint/schedules_endpoint.py @@ -85,10 +85,10 @@ def create(self, schedule_item: ScheduleItem) -> ScheduleItem: def add_to_schedule( self, schedule_id: str, - workbook: "WorkbookItem" = None, - datasource: "DatasourceItem" = None, - flow: "FlowItem" = None, - task_type: str = None, + workbook: Optional["WorkbookItem"] = None, + datasource: Optional["DatasourceItem"] = None, + flow: Optional["FlowItem"] = None, + task_type: Optional[str] = None, ) -> List[AddResponse]: # There doesn't seem to be a good reason to allow one item of each type? diff --git a/tableauserverclient/server/endpoint/users_endpoint.py b/tableauserverclient/server/endpoint/users_endpoint.py index 28406ab71..3faf4d173 100644 --- a/tableauserverclient/server/endpoint/users_endpoint.py +++ b/tableauserverclient/server/endpoint/users_endpoint.py @@ -21,7 +21,7 @@ def baseurl(self) -> str: # Gets all users @api(version="2.0") - def get(self, req_options: RequestOptions = None) -> Tuple[List[UserItem], PaginationItem]: + def get(self, req_options: Optional[RequestOptions] = None) -> Tuple[List[UserItem], PaginationItem]: logger.info("Querying all users on site") if req_options is None: @@ -47,7 +47,7 @@ def get_by_id(self, user_id: str) -> UserItem: # Update user @api(version="2.0") - def update(self, user_item: UserItem, password: str = None) -> UserItem: + def update(self, user_item: UserItem, password: Optional[str] = None) -> UserItem: if not user_item.id: error = "User item missing ID." raise MissingRequiredFieldError(error) @@ -122,7 +122,7 @@ def create_from_file(self, filepath: str) -> Tuple[List[UserItem], List[Tuple[Us # Get workbooks for user @api(version="2.0") - def populate_workbooks(self, user_item: UserItem, req_options: RequestOptions = None) -> None: + def populate_workbooks(self, user_item: UserItem, req_options: Optional[RequestOptions] = None) -> None: if not user_item.id: error = "User item missing ID." raise MissingRequiredFieldError(error) @@ -133,7 +133,7 @@ def wb_pager(): user_item._set_workbooks(wb_pager) def _get_wbs_for_user( - self, user_item: UserItem, req_options: RequestOptions = None + self, user_item: UserItem, req_options: Optional[RequestOptions] = None ) -> Tuple[List[WorkbookItem], PaginationItem]: url = "{0}/{1}/workbooks".format(self.baseurl, user_item.id) server_response = self.get_request(url, req_options) @@ -147,7 +147,7 @@ def populate_favorites(self, user_item: UserItem) -> None: # Get groups for user @api(version="3.7") - def populate_groups(self, user_item: UserItem, req_options: RequestOptions = None) -> None: + def populate_groups(self, user_item: UserItem, req_options: Optional[RequestOptions] = None) -> None: if not user_item.id: error = "User item missing ID." raise MissingRequiredFieldError(error) @@ -161,7 +161,7 @@ def groups_for_user_pager(): user_item._set_groups(groups_for_user_pager) def _get_groups_for_user( - self, user_item: UserItem, req_options: RequestOptions = None + self, user_item: UserItem, req_options: Optional[RequestOptions] = None ) -> Tuple[List[GroupItem], PaginationItem]: url = "{0}/{1}/groups".format(self.baseurl, user_item.id) server_response = self.get_request(url, req_options) diff --git a/tableauserverclient/server/endpoint/workbooks_endpoint.py b/tableauserverclient/server/endpoint/workbooks_endpoint.py index 91205d202..163bb8c71 100644 --- a/tableauserverclient/server/endpoint/workbooks_endpoint.py +++ b/tableauserverclient/server/endpoint/workbooks_endpoint.py @@ -179,7 +179,7 @@ def update_connection(self, workbook_item: WorkbookItem, connection_item: Connec def download( self, workbook_id: str, - filepath: FilePath = None, + filepath: Optional[FilePath] = None, include_extract: bool = True, no_extract: Optional[bool] = None, ) -> str: @@ -251,7 +251,7 @@ def connection_fetcher(): logger.info("Populated connections for workbook (ID: {0})".format(workbook_item.id)) def _get_workbook_connections( - self, workbook_item: WorkbookItem, req_options: "RequestOptions" = None + self, workbook_item: WorkbookItem, req_options: Optional["RequestOptions"] = None ) -> List[ConnectionItem]: url = "{0}/{1}/connections".format(self.baseurl, workbook_item.id) server_response = self.get_request(url, req_options) @@ -260,7 +260,7 @@ def _get_workbook_connections( # Get the pdf of the entire workbook if its tabs are enabled, pdf of the default view if its tabs are disabled @api(version="3.4") - def populate_pdf(self, workbook_item: WorkbookItem, req_options: "RequestOptions" = None) -> None: + def populate_pdf(self, workbook_item: WorkbookItem, req_options: Optional["RequestOptions"] = None) -> None: if not workbook_item.id: error = "Workbook item missing ID." raise MissingRequiredFieldError(error) diff --git a/tableauserverclient/server/request_factory.py b/tableauserverclient/server/request_factory.py index aad8ca074..142297aa0 100644 --- a/tableauserverclient/server/request_factory.py +++ b/tableauserverclient/server/request_factory.py @@ -575,7 +575,7 @@ def add_flow_req(self, id_: Optional[str], task_type: str = TaskItem.Type.RunFlo class SiteRequest(object): - def update_req(self, site_item: "SiteItem", parent_srv: "Server" = None): + def update_req(self, site_item: "SiteItem", parent_srv: Optional["Server"] = None): xml_request = ET.Element("tsRequest") site_element = ET.SubElement(xml_request, "site") if site_item.name: @@ -683,7 +683,7 @@ def update_req(self, site_item: "SiteItem", parent_srv: "Server" = None): return ET.tostring(xml_request) # server: the site request model changes based on api version - def create_req(self, site_item: "SiteItem", parent_srv: "Server" = None): + def create_req(self, site_item: "SiteItem", parent_srv: Optional["Server"] = None): xml_request = ET.Element("tsRequest") site_element = ET.SubElement(xml_request, "site") site_element.attrib["name"] = site_item.name