Spaces:
Sleeping
Sleeping
File size: 14,293 Bytes
4b9d59b | 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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | """
Tableau Integration Adapter
Provides OAuth-based integration with Tableau for analytics and data visualization.
"""
import logging
import os
import httpx
from typing import Dict, Any, List, Optional
from datetime import datetime, timedelta
from urllib.parse import urlencode
logger = logging.getLogger(__name__)
class TableauAdapter:
"""
Adapter for Tableau OAuth integration.
Supports:
- OAuth 2.0 authentication (PAT - Personal Access Token)
- Workbook and data source management
- Project and view access
- Query and embedding operations
"""
def __init__(self, db, workspace_id: str):
self.db = db
self.workspace_id = workspace_id
self.service_name = "tableau"
self.base_url = os.getenv("TABLEAU_SERVER_URL", "https://online.tableau.com")
# OAuth credentials from environment
self.client_id = os.getenv("TABLEAU_CLIENT_ID")
self.client_secret = os.getenv("TABLEAU_CLIENT_SECRET")
self.redirect_uri = os.getenv("TABLEAU_REDIRECT_URI")
self.pat_name = os.getenv("TABLEAU_PAT_NAME")
self.pat_secret = os.getenv("TABLEAU_PAT_SECRET")
# Token storage
self._access_token: Optional[str] = None
_refresh_token: Optional[str] = None
self._token_expires_at: Optional[datetime] = None
self._site_id: Optional[str] = None
async def get_oauth_url(self) -> str:
"""
Generate Tableau OAuth authorization URL.
Returns:
Authorization URL to redirect user to Tableau OAuth consent screen
"""
if not self.client_id:
raise ValueError("TABLEAU_CLIENT_ID not configured")
# Tableau OAuth endpoint
auth_url = f"{self.base_url}/oauth/authorize"
# Build authorization URL
params = {
"client_id": self.client_id,
"redirect_uri": self.redirect_uri,
"response_type": "code",
"state": self.workspace_id, # Use workspace_id as state
}
auth_url_with_params = f"{auth_url}?{urlencode(params)}"
logger.info(f"Generated Tableau OAuth URL for workspace {self.workspace_id}")
return auth_url_with_params
async def exchange_code_for_token(self, code: str) -> Dict[str, Any]:
"""
Exchange OAuth authorization code for access token.
Args:
code: Authorization code from OAuth callback
Returns:
Token response with access_token, refresh_token, expires_in, etc.
"""
if not self.client_id or not self.client_secret:
raise ValueError("Tableau OAuth credentials not configured")
token_url = f"{self.base_url}/oauth/token"
data = {
"grant_type": "authorization_code",
"code": code,
"redirect_uri": self.redirect_uri,
"client_id": self.client_id,
"client_secret": self.client_secret,
}
try:
async with httpx.AsyncClient() as client:
response = await client.post(token_url, json=data)
response.raise_for_status()
token_data = response.json()
# Store tokens
self._access_token = token_data.get("access_token")
_refresh_token = token_data.get("refresh_token")
# Calculate token expiration
if "expires_in" in token_data:
self._token_expires_at = datetime.now() + timedelta(
seconds=token_data["expires_in"]
)
logger.info(f"Successfully obtained Tableau access token for workspace {self.workspace_id}")
return token_data
except httpx.HTTPStatusError as e:
logger.error(f"Tableau token exchange failed: {e}")
raise
async def test_connection(self) -> bool:
"""
Test the Tableau API connection.
Returns:
True if connection successful, False otherwise
"""
if not self._access_token and not self.pat_secret:
return False
try:
async with httpx.AsyncClient() as client:
# Test by getting user info
headers = {}
if self._access_token:
headers["Authorization"] = f"Bearer {self._access_token}"
elif self.pat_secret:
# Use PAT authentication
import base64
credentials = base64.b64encode(
f"{self.pat_name}:{self.pat_secret}".encode()
).decode()
headers["X-Tableau-Auth"] = credentials
response = await client.get(
f"{self.base_url}/api/3.17/sites",
headers=headers
)
response.raise_for_status()
logger.info(f"Tableau connection test successful for workspace {self.workspace_id}")
return True
except Exception as e:
logger.error(f"Tableau connection test failed: {e}")
return False
async def get_workbooks(self, site_id: str = None) -> List[Dict[str, Any]]:
"""
Retrieve Tableau workbooks.
Args:
site_id: Site ID (content URL)
Returns:
List of workbook objects
"""
token = self._access_token
if not token and not self.pat_secret:
raise ValueError("Tableau credentials not configured")
try:
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
else:
import base64
credentials = base64.b64encode(
f"{self.pat_name}:{self.pat_secret}".encode()
).decode()
headers["X-Tableau-Auth"] = credentials
site = site_id or self._site_id or ""
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.base_url}/api/3.17/sites/{site}/workbooks",
headers=headers
)
response.raise_for_status()
data = response.json()
workbooks = data.get("workbook", {}).get("workbook", [])
# Handle single workbook case
if isinstance(workbooks, dict):
workbooks = [workbooks]
logger.info(f"Retrieved {len(workbooks)} Tableau workbooks for workspace {self.workspace_id}")
return workbooks
except Exception as e:
logger.error(f"Failed to retrieve Tableau workbooks: {e}")
raise
async def get_workbook(self, workbook_id: str, site_id: str = None) -> Dict[str, Any]:
"""
Retrieve a specific Tableau workbook by ID.
Args:
workbook_id: Workbook ID
site_id: Site ID (content URL)
Returns:
Workbook details with all views
"""
token = self._access_token
if not token and not self.pat_secret:
raise ValueError("Tableau credentials not configured")
try:
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
else:
import base64
credentials = base64.b64encode(
f"{self.pat_name}:{self.pat_secret}".encode()
).decode()
headers["X-Tableau-Auth"] = credentials
site = site_id or self._site_id or ""
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.base_url}/api/3.17/sites/{site}/workbooks/{workbook_id}",
headers=headers,
params={"includeViews": "true"}
)
response.raise_for_status()
data = response.json()
workbook = data.get("workbook", {})
logger.info(f"Retrieved Tableau workbook {workbook_id} for workspace {self.workspace_id}")
return workbook
except Exception as e:
logger.error(f"Failed to retrieve Tableau workbook {workbook_id}: {e}")
raise
async def get_views(self, workbook_id: str, site_id: str = None) -> List[Dict[str, Any]]:
"""
Retrieve all views in a Tableau workbook.
Args:
workbook_id: Workbook ID
site_id: Site ID (content URL)
Returns:
List of view objects
"""
token = self._access_token
if not token and not self.pat_secret:
raise ValueError("Tableau credentials not configured")
try:
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
else:
import base64
credentials = base64.b64encode(
f"{self.pat_name}:{self.pat_secret}".encode()
).decode()
headers["X-Tableau-Auth"] = credentials
site = site_id or self._site_id or ""
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.base_url}/api/3.17/sites/{site}/workbooks/{workbook_id}/views",
headers=headers
)
response.raise_for_status()
data = response.json()
views = data.get("view", {}).get("view", [])
# Handle single view case
if isinstance(views, dict):
views = [views]
logger.info(f"Retrieved {len(views)} Tableau views for workbook {workbook_id}")
return views
except Exception as e:
logger.error(f"Failed to retrieve Tableau views: {e}")
raise
async def get_projects(self, site_id: str = None) -> List[Dict[str, Any]]:
"""
Retrieve all Tableau projects.
Args:
site_id: Site ID (content URL)
Returns:
List of project objects
"""
token = self._access_token
if not token and not self.pat_secret:
raise ValueError("Tableau credentials not configured")
try:
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
else:
import base64
credentials = base64.b64encode(
f"{self.pat_name}:{self.pat_secret}".encode()
).decode()
headers["X-Tableau-Auth"] = credentials
site = site_id or self._site_id or ""
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.base_url}/api/3.17/sites/{site}/projects",
headers=headers
)
response.raise_for_status()
data = response.json()
projects = data.get("project", {}).get("project", [])
# Handle single project case
if isinstance(projects, dict):
projects = [projects]
logger.info(f"Retrieved {len(projects)} Tableau projects for workspace {self.workspace_id}")
return projects
except Exception as e:
logger.error(f"Failed to retrieve Tableau projects: {e}")
raise
async def query_view_image(self, view_id: str, site_id: str = None) -> str:
"""
Get the image URL for a Tableau view.
Args:
view_id: View ID
site_id: Site ID (content URL)
Returns:
Image URL for the view
"""
token = self._access_token
if not token and not self.pat_secret:
raise ValueError("Tableau credentials not configured")
try:
site = site_id or self._site_id or ""
image_url = f"{self.base_url}/api/3.17/sites/{site}/views/{view_id}/image"
if token:
# Add token as query parameter
from urllib.parse import urlencode
image_url = f"{image_url}?{urlencode({'token': token})}"
logger.info(f"Generated image URL for Tableau view {view_id}")
return image_url
except Exception as e:
logger.error(f"Failed to generate image URL for view {view_id}: {e}")
raise
async def get_datasources(self, site_id: str = None) -> List[Dict[str, Any]]:
"""
Retrieve all Tableau data sources.
Args:
site_id: Site ID (content URL)
Returns:
List of data source objects
"""
token = self._access_token
if not token and not self.pat_secret:
raise ValueError("Tableau credentials not configured")
try:
headers = {}
if token:
headers["Authorization"] = f"Bearer {token}"
else:
import base64
credentials = base64.b64encode(
f"{self.pat_name}:{self.pat_secret}".encode()
).decode()
headers["X-Tableau-Auth"] = credentials
site = site_id or self._site_id or ""
async with httpx.AsyncClient() as client:
response = await client.get(
f"{self.base_url}/api/3.17/sites/{site}/datasources",
headers=headers
)
response.raise_for_status()
data = response.json()
datasources = data.get("datasource", {}).get("datasource", [])
# Handle single datasource case
if isinstance(datasources, dict):
datasources = [datasources]
logger.info(f"Retrieved {len(datasources)} Tableau datasources for workspace {self.workspace_id}")
return datasources
except Exception as e:
logger.error(f"Failed to retrieve Tableau datasources: {e}")
raise
|