-
Notifications
You must be signed in to change notification settings - Fork 447
Expand file tree
/
Copy pathserver.py
More file actions
315 lines (268 loc) · 11 KB
/
server.py
File metadata and controls
315 lines (268 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
from tableauserverclient.helpers.logging import logger
import requests
import urllib3
from defusedxml.ElementTree import fromstring, ParseError
from packaging.version import Version
from tableauserverclient.server.endpoint import (
Sites,
Views,
Users,
Groups,
Workbooks,
Datasources,
Projects,
Auth,
Schedules,
ServerInfo,
Tasks,
Subscriptions,
Jobs,
Metadata,
Databases,
Tables,
Flows,
FlowTasks,
Webhooks,
DataAccelerationReport,
Favorites,
DataAlerts,
Fileuploads,
FlowRuns,
Metrics,
Endpoint,
CustomViews,
LinkedTasks,
GroupSets,
Tags,
VirtualConnections,
)
from tableauserverclient.server.exceptions import (
ServerInfoEndpointNotFoundError,
EndpointUnavailableError,
)
from tableauserverclient.server.endpoint.exceptions import NotSignedInError
from tableauserverclient.namespace import Namespace
_PRODUCT_TO_REST_VERSION = {
"10.0": "2.3",
"9.3": "2.2",
"9.2": "2.1",
"9.1": "2.0",
"9.0": "2.0",
}
minimum_supported_server_version = "2.3"
default_server_version = "2.4" # first version that dropped the legacy auth endpoint
class Server:
"""
In the Tableau REST API, the server (https://MY-SERVER/) is the base or core
of the URI that makes up the various endpoints or methods for accessing
resources on the server (views, workbooks, sites, users, data sources, etc.)
The TSC library provides a Server class that represents the server. You
create a server instance to sign in to the server and to call the various
methods for accessing resources.
The Server class contains the attributes that represent the server on
Tableau Server. After you create an instance of the Server class, you can
sign in to the server and call methods to access all of the resources on the
server.
Parameters
----------
server_address : str
Specifies the address of the Tableau Server or Tableau Cloud (for
example, https://MY-SERVER/).
use_server_version : bool
Specifies the version of the REST API to use (for example, '2.5'). When
you use the TSC library to call methods that access Tableau Server, the
version is passed to the endpoint as part of the URI
(https://MY-SERVER/api/2.5/). Each release of Tableau Server supports
specific versions of the REST API. New versions of the REST API are
released with Tableau Server. By default, the value of version is set to
'2.3', which corresponds to Tableau Server 10.0. You can view or set
this value. You might need to set this to a different value, for
example, if you want to access features that are supported by the server
and a later version of the REST API. For more information, see REST API
Versions.
Examples
--------
>>> import tableauserverclient as TSC
>>> # create a instance of server
>>> server = TSC.Server('https://MY-SERVER')
>>> # sign in, etc.
>>> # change the REST API version to match the server
>>> server.use_server_version()
>>> # or change the REST API version to match a specific version
>>> # for example, 2.8
>>> # server.version = '2.8'
"""
class PublishMode:
"""
Enumerates the options that specify what happens when you publish a
workbook or data source. The options are Overwrite, Append, or
CreateNew.
"""
Append = "Append"
Overwrite = "Overwrite"
CreateNew = "CreateNew"
Replace = "Replace"
def __init__(self, server_address, use_server_version=False, http_options=None, session_factory=None):
self._auth_token = None
self._site_id = None
self._user_id = None
# TODO: this needs to change to default to https, but without breaking existing code
if not server_address.startswith("http://") and not server_address.startswith("https://"):
server_address = "http://" + server_address
self._server_address: str = server_address
self._session_factory = session_factory or requests.session
self.auth = Auth(self)
self.views = Views(self)
self.users = Users(self)
self.sites = Sites(self)
self.groups = Groups(self)
self.jobs = Jobs(self)
self.workbooks = Workbooks(self)
self.datasources = Datasources(self)
self.favorites = Favorites(self)
self.flows = Flows(self)
self.flow_tasks = FlowTasks(self)
self.projects = Projects(self)
self.schedules = Schedules(self)
self.server_info = ServerInfo(self)
self.tasks = Tasks(self)
self.subscriptions = Subscriptions(self)
self.metadata = Metadata(self)
self.databases = Databases(self)
self.tables = Tables(self)
self.webhooks = Webhooks(self)
self.data_acceleration_report = DataAccelerationReport(self)
self.data_alerts = DataAlerts(self)
self.fileuploads = Fileuploads(self)
self._namespace = Namespace()
self.flow_runs = FlowRuns(self)
self.metrics = Metrics(self)
self.custom_views = CustomViews(self)
self.linked_tasks = LinkedTasks(self)
self.group_sets = GroupSets(self)
self.tags = Tags(self)
self.virtual_connections = VirtualConnections(self)
self._session = self._session_factory()
self._http_options = dict() # must set this before making a server call
if http_options:
self.add_http_options(http_options)
self.validate_connection_settings() # does not make an actual outgoing request
self.version = default_server_version
if use_server_version:
self.use_server_version() # this makes a server call
def validate_connection_settings(self):
try:
params = Endpoint(self).set_parameters(self._http_options, None, None, None, None)
Endpoint.set_user_agent(params)
if not self._server_address.startswith("http://") and not self._server_address.startswith("https://"):
self._server_address = "http://" + self._server_address
self._session.prepare_request(requests.Request("GET", url=self._server_address, params=self._http_options))
except Exception as req_ex:
raise ValueError("Server connection settings not valid", req_ex)
def __repr__(self):
return f"<TableauServerClient [Connection: {self.baseurl}, {self.server_info.serverInfo}]>"
def add_http_options(self, options_dict: dict):
try:
self._http_options.update(options_dict)
if "verify" in options_dict.keys() and self._http_options.get("verify") is False:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# would be nice if you could turn them back on
except Exception as be:
# expected errors on invalid input:
# 'set' object has no attribute 'keys', 'list' object has no attribute 'keys'
# TypeError: cannot convert dictionary update sequence element #0 to a sequence (input is a tuple)
raise ValueError(f"Invalid http options given: {options_dict}")
def clear_http_options(self):
self._http_options = dict()
def _clear_auth(self):
self._site_id = None
self._user_id = None
self._auth_token = None
self._site_url = None
self._session = self._session_factory()
def _set_auth(self, site_id, user_id, auth_token, site_url=None):
self._site_id = site_id
self._user_id = user_id
self._auth_token = auth_token
self._site_url = site_url
def _get_legacy_version(self):
# the serverInfo call was introduced in 2.4, earlier than that we have this different call
response = self._session.get(self.server_address + "/auth?format=xml")
try:
info_xml = fromstring(response.content)
except ParseError as parseError:
logger.info(parseError)
logger.info("Could not read server version info. The server may not be running or configured.")
return self.version
prod_version = info_xml.find(".//product_version").text
version = _PRODUCT_TO_REST_VERSION.get(prod_version, minimum_supported_server_version)
return version
def _determine_highest_version(self):
try:
old_version = self.version
version = self.server_info.get().rest_api_version
except ServerInfoEndpointNotFoundError as e:
logger.info(f"Could not get version info from server: {e.__class__}{e}")
version = self._get_legacy_version()
except EndpointUnavailableError as e:
logger.info(f"Could not get version info from server: {e.__class__}{e}")
version = self._get_legacy_version()
except Exception as e:
logger.info(f"Could not get version info from server: {e.__class__}{e}")
version = None
logger.info(f"versions: {version}, {old_version}")
return version or old_version
def use_server_version(self):
self.version = self._determine_highest_version()
def use_highest_version(self):
self.use_server_version()
logger.info("use use_server_version instead", DeprecationWarning)
def check_at_least_version(self, target: str):
server_version = Version(self.version or "2.4")
target_version = Version(target)
return server_version >= target_version
def assert_at_least_version(self, comparison: str, reason: str):
if not self.check_at_least_version(comparison):
error = f"{reason} is not available in API version {self.version}. Requires {comparison}"
raise EndpointUnavailableError(error)
@property
def baseurl(self):
return f"{self._server_address}/api/{str(self.version)}"
@property
def namespace(self):
return self._namespace()
@property
def auth_token(self):
if self._auth_token is None:
error = "Missing authentication token. You must sign in first."
raise NotSignedInError(error)
return self._auth_token
@property
def site_id(self):
if self._site_id is None:
error = "Missing site ID. You must sign in first."
raise NotSignedInError(error)
return self._site_id
@property
def site_url(self):
if self._site_url is None:
error = "Missing site URL. You must sign in first."
raise NotSignedInError(error)
return self._site_url
@property
def user_id(self):
if self._user_id is None:
error = "Missing user ID. You must sign in first."
raise NotSignedInError(error)
return self._user_id
@property
def server_address(self):
return self._server_address
@property
def http_options(self):
return self._http_options
@property
def session(self):
return self._session
def is_signed_in(self):
return self._auth_token is not None