|
1 | | -class TableauAuth(object): |
| 1 | +class Credentials: |
| 2 | + def __init__(self, site_id=None, user_id_to_impersonate=None): |
| 3 | + self.site_id = site_id or "" |
| 4 | + self.user_id_to_impersonate = user_id_to_impersonate or None |
| 5 | + |
| 6 | + @property |
| 7 | + def credentials(self): |
| 8 | + credentials = "Credentials can be username/password, Personal Access Token, or JWT" |
| 9 | + +"This method returns values to set as an attribute on the credentials element of the request" |
| 10 | + |
| 11 | + def __repr__(self): |
| 12 | + display = "All Credentials types must have a debug display that does not print secrets" |
| 13 | + |
| 14 | + |
| 15 | +def deprecate_site_attribute(): |
| 16 | + import warnings |
| 17 | + |
| 18 | + warnings.warn( |
| 19 | + "TableauAuth(..., site=...) is deprecated, " "please use TableauAuth(..., site_id=...) instead.", |
| 20 | + DeprecationWarning, |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +# The traditional auth type: username/password |
| 25 | +class TableauAuth(Credentials): |
2 | 26 | def __init__(self, username, password, site=None, site_id=None, user_id_to_impersonate=None): |
3 | 27 | if site is not None: |
4 | | - import warnings |
5 | | - |
6 | | - warnings.warn( |
7 | | - "TableauAuth(..., site=...) is deprecated, " "please use TableauAuth(..., site_id=...) instead.", |
8 | | - DeprecationWarning, |
9 | | - ) |
| 28 | + deprecate_site_attribute() |
10 | 29 | site_id = site |
| 30 | + super().__init__(site_id, user_id_to_impersonate) |
11 | 31 | if password is None: |
12 | 32 | raise TabError("Must provide a password when using traditional authentication") |
13 | | - |
14 | | - self.user_id_to_impersonate = user_id_to_impersonate |
15 | 33 | self.password = password |
16 | | - self.site_id = site_id if site_id is not None else "" |
17 | 34 | self.username = username |
18 | 35 |
|
19 | 36 | @property |
20 | | - def site(self): |
21 | | - import warnings |
| 37 | + def credentials(self): |
| 38 | + return {"name": self.username, "password": self.password} |
22 | 39 |
|
23 | | - warnings.warn( |
24 | | - "TableauAuth.site is deprecated, use TableauAuth.site_id instead.", |
25 | | - DeprecationWarning, |
26 | | - ) |
| 40 | + def __repr__(self): |
| 41 | + return "<Credentials username={} password={}>".format(self.username, "<redacted>") |
| 42 | + |
| 43 | + @property |
| 44 | + def site(self): |
| 45 | + deprecate_site_attribute() |
27 | 46 | return self.site_id |
28 | 47 |
|
29 | 48 | @site.setter |
30 | 49 | def site(self, value): |
31 | | - import warnings |
32 | | - |
33 | | - warnings.warn( |
34 | | - "TableauAuth.site is deprecated, use TableauAuth.site_id instead.", |
35 | | - DeprecationWarning, |
36 | | - ) |
| 50 | + deprecate_site_attribute() |
37 | 51 | self.site_id = value |
38 | 52 |
|
| 53 | + |
| 54 | +class PersonalAccessTokenAuth(Credentials): |
| 55 | + def __init__(self, token_name, personal_access_token, site_id=None): |
| 56 | + super().__init__(site_id=site_id) |
| 57 | + self.token_name = token_name |
| 58 | + self.personal_access_token = personal_access_token |
| 59 | + |
39 | 60 | @property |
40 | 61 | def credentials(self): |
41 | | - return {"name": self.username, "password": self.password} |
| 62 | + return { |
| 63 | + "personalAccessTokenName": self.token_name, |
| 64 | + "personalAccessTokenSecret": self.personal_access_token, |
| 65 | + } |
| 66 | + |
| 67 | + def __repr__(self): |
| 68 | + return "<PersonalAccessToken name={} token={} site={}>".format( |
| 69 | + self.token_name, self.personal_access_token[:2] + "...", self.site_id |
| 70 | + ) |
0 commit comments