Skip to content

Commit

Permalink
Merge branch 'development' into feature/filesize_limit_env
Browse files Browse the repository at this point in the history
  • Loading branch information
jacalata authored Oct 10, 2024
2 parents 06c65ed + 9495fe8 commit 01627c3
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 18 deletions.
5 changes: 5 additions & 0 deletions samples/explore_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def main():
if args.publish:
if default_project is not None:
new_datasource = TSC.DatasourceItem(default_project.id)
new_datasource.description = "Published with a description"
new_datasource = server.datasources.publish(
new_datasource, args.publish, TSC.Server.PublishMode.Overwrite
)
Expand All @@ -72,6 +73,10 @@ def main():
print(f"\nConnections for {sample_datasource.name}: ")
print([f"{connection.id}({connection.datasource_name})" for connection in sample_datasource.connections])

# Demonstrate that description is editable
sample_datasource.description = "Description updated by TSC"
server.datasources.update(sample_datasource)

# Add some tags to the datasource
original_tag_set = set(sample_datasource.tags)
sample_datasource.tags.update("a", "b", "c", "d")
Expand Down
2 changes: 2 additions & 0 deletions tableauserverclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
PDFRequestOptions,
RequestOptions,
MissingRequiredFieldError,
FailedSignInError,
NotSignedInError,
ServerResponseError,
Filter,
Expand All @@ -79,6 +80,7 @@
"DatabaseItem",
"DataFreshnessPolicyItem",
"DatasourceItem",
"FailedSignInError",
"FavoriteItem",
"FlowItem",
"FlowRunItem",
Expand Down
4 changes: 2 additions & 2 deletions tableauserverclient/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=
return None, None
else:
if verbose:
print("unable to find command, tried {}".format(commands))
print(f"unable to find command, tried {commands}")
return None, None
stdout = p.communicate()[0].strip()
if sys.version_info[0] >= 3:
Expand Down Expand Up @@ -131,7 +131,7 @@ def versions_from_parentdir(parentdir_prefix, root, verbose):
root = os.path.dirname(root) # up a level

if verbose:
print("Tried directories {} but none started with prefix {}".format(str(rootdirs), parentdir_prefix))
print(f"Tried directories {str(rootdirs)} but none started with prefix {parentdir_prefix}")
raise NotThisMethod("rootdir doesn't start with parentdir_prefix")


Expand Down
8 changes: 3 additions & 5 deletions tableauserverclient/models/server_info_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ def from_response(cls, resp, ns):
try:
parsed_response = fromstring(resp)
except xml.etree.ElementTree.ParseError as error:
logger.info(f"Unexpected response for ServerInfo: {resp}")
logger.info(error)
logger.exception(f"Unexpected response for ServerInfo: {resp}")
return cls("Unknown", "Unknown", "Unknown")
except Exception as error:
logger.info(f"Unexpected response for ServerInfo: {resp}")
logger.info(error)
return cls("Unknown", "Unknown", "Unknown")
logger.exception(f"Unexpected response for ServerInfo: {resp}")
raise error

product_version_tag = parsed_response.find(".//t:productVersion", namespaces=ns)
rest_api_version_tag = parsed_response.find(".//t:restApiVersion", namespaces=ns)
Expand Down
110 changes: 110 additions & 0 deletions tableauserverclient/models/tableau_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,43 @@ def deprecate_site_attribute():

# The traditional auth type: username/password
class TableauAuth(Credentials):
"""
The TableauAuth class defines the information you can set in a sign-in
request. The class members correspond to the attributes of a server request
or response payload. To use this class, create a new instance, supplying
user name, password, and site information if necessary, and pass the
request object to the Auth.sign_in method.
Parameters
----------
username : str
The user name for the sign-in request.
password : str
The password for the sign-in request.
site_id : str, optional
This corresponds to the contentUrl attribute in the Tableau REST API.
The site_id is the portion of the URL that follows the /site/ in the
URL. For example, "MarketingTeam" is the site_id in the following URL
MyServer/#/site/MarketingTeam/projects. To specify the default site on
Tableau Server, you can use an empty string '' (single quotes, no
space). For Tableau Cloud, you must provide a value for the site_id.
user_id_to_impersonate : str, optional
Specifies the id (not the name) of the user to sign in as. This is not
available for Tableau Online.
Examples
--------
>>> import tableauserverclient as TSC
>>> tableau_auth = TSC.TableauAuth('USERNAME', 'PASSWORD', site_id='CONTENTURL')
>>> server = TSC.Server('https://SERVER_URL', use_server_version=True)
>>> server.auth.sign_in(tableau_auth)
"""

def __init__(
self, username: str, password: str, site_id: Optional[str] = None, user_id_to_impersonate: Optional[str] = None
) -> None:
Expand All @@ -55,6 +92,43 @@ def __repr__(self):

# A Tableau-generated Personal Access Token
class PersonalAccessTokenAuth(Credentials):
"""
The PersonalAccessTokenAuth class defines the information you can set in a sign-in
request. The class members correspond to the attributes of a server request
or response payload. To use this class, create a new instance, supplying
token name, token secret, and site information if necessary, and pass the
request object to the Auth.sign_in method.
Parameters
----------
token_name : str
The name of the personal access token.
personal_access_token : str
The personal access token secret for the sign in request.
site_id : str, optional
This corresponds to the contentUrl attribute in the Tableau REST API.
The site_id is the portion of the URL that follows the /site/ in the
URL. For example, "MarketingTeam" is the site_id in the following URL
MyServer/#/site/MarketingTeam/projects. To specify the default site on
Tableau Server, you can use an empty string '' (single quotes, no
space). For Tableau Cloud, you must provide a value for the site_id.
user_id_to_impersonate : str, optional
Specifies the id (not the name) of the user to sign in as. This is not
available for Tableau Online.
Examples
--------
>>> import tableauserverclient as TSC
>>> tableau_auth = TSC.PersonalAccessTokenAuth("token_name", "token_secret", site_id='CONTENTURL')
>>> server = TSC.Server('https://SERVER_URL', use_server_version=True)
>>> server.auth.sign_in(tableau_auth)
"""

def __init__(
self,
token_name: str,
Expand Down Expand Up @@ -88,6 +162,42 @@ def __repr__(self):

# A standard JWT generated specifically for Tableau
class JWTAuth(Credentials):
"""
The JWTAuth class defines the information you can set in a sign-in
request. The class members correspond to the attributes of a server request
or response payload. To use this class, create a new instance, supplying
an encoded JSON Web Token, and site information if necessary, and pass the
request object to the Auth.sign_in method.
Parameters
----------
token : str
The encoded JSON Web Token.
site_id : str, optional
This corresponds to the contentUrl attribute in the Tableau REST API.
The site_id is the portion of the URL that follows the /site/ in the
URL. For example, "MarketingTeam" is the site_id in the following URL
MyServer/#/site/MarketingTeam/projects. To specify the default site on
Tableau Server, you can use an empty string '' (single quotes, no
space). For Tableau Cloud, you must provide a value for the site_id.
user_id_to_impersonate : str, optional
Specifies the id (not the name) of the user to sign in as. This is not
available for Tableau Online.
Examples
--------
>>> import jwt
>>> import tableauserverclient as TSC
>>> jwt_token = jwt.encode(...)
>>> tableau_auth = TSC.JWTAuth(token, site_id='CONTENTURL')
>>> server = TSC.Server('https://SERVER_URL', use_server_version=True)
>>> server.auth.sign_in(tableau_auth)
"""

def __init__(self, jwt: str, site_id: Optional[str] = None, user_id_to_impersonate: Optional[str] = None) -> None:
if jwt is None:
raise TabError("Must provide a JWT token when using JWT authentication")
Expand Down
3 changes: 2 additions & 1 deletion tableauserverclient/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from tableauserverclient.server.sort import Sort
from tableauserverclient.server.server import Server
from tableauserverclient.server.pager import Pager
from tableauserverclient.server.endpoint.exceptions import NotSignedInError
from tableauserverclient.server.endpoint.exceptions import FailedSignInError, NotSignedInError

from tableauserverclient.server.endpoint import (
Auth,
Expand Down Expand Up @@ -57,6 +57,7 @@
"Sort",
"Server",
"Pager",
"FailedSignInError",
"NotSignedInError",
"Auth",
"CustomViews",
Expand Down
57 changes: 57 additions & 0 deletions tableauserverclient/server/endpoint/auth_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ def sign_in(self, auth_req: "Credentials") -> contextmgr:
optionally a user_id to impersonate.
Creates a context manager that will sign out of the server upon exit.
Parameters
----------
auth_req : Credentials
The credentials object to use for signing in. Can be a TableauAuth,
PersonalAccessTokenAuth, or JWTAuth object.
Returns
-------
contextmgr
A context manager that will sign out of the server upon exit.
Examples
--------
>>> import tableauserverclient as TSC
>>> # create an auth object
>>> tableau_auth = TSC.TableauAuth('USERNAME', 'PASSWORD')
>>> # create an instance for your server
>>> server = TSC.Server('https://SERVER_URL')
>>> # call the sign-in method with the auth object
>>> server.auth.sign_in(tableau_auth)
"""
url = f"{self.baseurl}/signin"
signin_req = RequestFactory.Auth.signin_req(auth_req)
Expand Down Expand Up @@ -70,14 +94,17 @@ def sign_in(self, auth_req: "Credentials") -> contextmgr:
# The distinct methods are mostly useful for explicitly showing api version support for each auth type
@api(version="3.6")
def sign_in_with_personal_access_token(self, auth_req: "Credentials") -> contextmgr:
"""Passthrough to sign_in method"""
return self.sign_in(auth_req)

@api(version="3.17")
def sign_in_with_json_web_token(self, auth_req: "Credentials") -> contextmgr:
"""Passthrough to sign_in method"""
return self.sign_in(auth_req)

@api(version="2.0")
def sign_out(self) -> None:
"""Sign out of current session."""
url = f"{self.baseurl}/signout"
# If there are no auth tokens you're already signed out. No-op
if not self.parent_srv.is_signed_in():
Expand All @@ -88,6 +115,33 @@ def sign_out(self) -> None:

@api(version="2.6")
def switch_site(self, site_item: "SiteItem") -> contextmgr:
"""
Switch to a different site on the server. This will sign out of the
current site and sign in to the new site. If used as a context manager,
will sign out of the new site upon exit.
Parameters
----------
site_item : SiteItem
The site to switch to.
Returns
-------
contextmgr
A context manager that will sign out of the new site upon exit.
Examples
--------
>>> import tableauserverclient as TSC
>>> # Find the site you want to switch to
>>> new_site = server.sites.get_by_id("9a8b7c6d-5e4f-3a2b-1c0d-9e8f7a6b5c4d")
>>> # switch to the new site
>>> with server.auth.switch_site(new_site):
>>> # do something on the new site
>>> pass
"""
url = f"{self.baseurl}/switchSite"
switch_req = RequestFactory.Auth.switch_req(site_item.content_url)
try:
Expand All @@ -109,6 +163,9 @@ def switch_site(self, site_item: "SiteItem") -> contextmgr:

@api(version="3.10")
def revoke_all_server_admin_tokens(self) -> None:
"""
Revokes all personal access tokens for all server admins on the server.
"""
url = f"{self.baseurl}/revokeAllServerAdminTokens"
self.post_request(url, "")
logger.info("Revoked all tokens for all server admins")
5 changes: 3 additions & 2 deletions tableauserverclient/server/endpoint/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from tableauserverclient.server.request_options import RequestOptions

from tableauserverclient.server.endpoint.exceptions import (
FailedSignInError,
ServerResponseError,
InternalServerError,
NonXMLResponseError,
Expand Down Expand Up @@ -140,7 +141,7 @@ def _make_request(
self._check_status(server_response, url)

loggable_response = self.log_response_safely(server_response)
logger.debug("Server response from {0}".format(url))
logger.debug(f"Server response from {url}")
# uncomment the following to log full responses in debug mode
# BE CAREFUL WHEN SHARING THESE RESULTS - MAY CONTAIN YOUR SENSITIVE DATA
# logger.debug(loggable_response)
Expand All @@ -160,7 +161,7 @@ def _check_status(self, server_response: "Response", url: Optional[str] = None):
try:
if server_response.status_code == 401:
# TODO: catch this in server.py and attempt to sign in again, in case it's a session expiry
raise NotSignedInError(server_response.content, url)
raise FailedSignInError.from_response(server_response.content, self.parent_srv.namespace, url)

raise ServerResponseError.from_response(server_response.content, self.parent_srv.namespace, url)
except ParseError:
Expand Down
24 changes: 20 additions & 4 deletions tableauserverclient/server/endpoint/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
from defusedxml.ElementTree import fromstring
from typing import Optional
from typing import Mapping, Optional, TypeVar


def split_pascal_case(s: str) -> str:
return "".join([f" {c}" if c.isupper() else c for c in s]).strip()


class TableauError(Exception):
pass


class ServerResponseError(TableauError):
def __init__(self, code, summary, detail, url=None):
T = TypeVar("T")


class XMLError(TableauError):
def __init__(self, code: str, summary: str, detail: str, url: Optional[str] = None) -> None:
self.code = code
self.summary = summary
self.detail = detail
Expand All @@ -18,7 +25,7 @@ def __str__(self):
return f"\n\n\t{self.code}: {self.summary}\n\t\t{self.detail}"

@classmethod
def from_response(cls, resp, ns, url=None):
def from_response(cls, resp, ns, url):
# Check elements exist before .text
parsed_response = fromstring(resp)
try:
Expand All @@ -33,6 +40,10 @@ def from_response(cls, resp, ns, url=None):
return error_response


class ServerResponseError(XMLError):
pass


class InternalServerError(TableauError):
def __init__(self, server_response, request_url: Optional[str] = None):
self.code = server_response.status_code
Expand All @@ -51,6 +62,11 @@ class NotSignedInError(TableauError):
pass


class FailedSignInError(XMLError, NotSignedInError):
def __str__(self):
return f"{split_pascal_case(self.__class__.__name__)}: {super().__str__()}"


class ItemTypeNotAllowed(TableauError):
pass

Expand Down
Loading

0 comments on commit 01627c3

Please sign in to comment.