-
Notifications
You must be signed in to change notification settings - Fork 421
/
subscriptions_endpoint.py
70 lines (58 loc) · 3.2 KB
/
subscriptions_endpoint.py
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
import logging
from .endpoint import Endpoint, api
from .exceptions import MissingRequiredFieldError
from tableauserverclient.server import RequestFactory
from tableauserverclient.models import SubscriptionItem, PaginationItem
from tableauserverclient.helpers.logging import logger
from typing import List, Optional, TYPE_CHECKING, Tuple
if TYPE_CHECKING:
from ..request_options import RequestOptions
class Subscriptions(Endpoint):
@property
def baseurl(self) -> str:
return "{0}/sites/{1}/subscriptions".format(self.parent_srv.baseurl, self.parent_srv.site_id)
@api(version="2.3")
def get(self, req_options: Optional["RequestOptions"] = None) -> Tuple[List[SubscriptionItem], PaginationItem]:
logger.info("Querying all subscriptions for the site")
url = self.baseurl
server_response = self.get_request(url, req_options)
pagination_item = PaginationItem.from_response(server_response.content, self.parent_srv.namespace)
all_subscriptions = SubscriptionItem.from_response(server_response.content, self.parent_srv.namespace)
return all_subscriptions, pagination_item
@api(version="2.3")
def get_by_id(self, subscription_id: str) -> SubscriptionItem:
if not subscription_id:
error = "No Subscription ID provided"
raise ValueError(error)
logger.info("Querying a single subscription by id ({})".format(subscription_id))
url = "{}/{}".format(self.baseurl, subscription_id)
server_response = self.get_request(url)
return SubscriptionItem.from_response(server_response.content, self.parent_srv.namespace)[0]
@api(version="2.3")
def create(self, subscription_item: SubscriptionItem) -> SubscriptionItem:
if not subscription_item:
error = "No Susbcription provided"
raise ValueError(error)
logger.info("Creating a subscription ({})".format(subscription_item))
url = self.baseurl
create_req = RequestFactory.Subscription.create_req(subscription_item)
server_response = self.post_request(url, create_req)
return SubscriptionItem.from_response(server_response.content, self.parent_srv.namespace)[0]
@api(version="2.3")
def delete(self, subscription_id: str) -> None:
if not subscription_id:
error = "Subscription ID undefined."
raise ValueError(error)
url = "{0}/{1}".format(self.baseurl, subscription_id)
self.delete_request(url)
logger.info("Deleted subscription (ID: {0})".format(subscription_id))
@api(version="2.3")
def update(self, subscription_item: SubscriptionItem) -> SubscriptionItem:
if not subscription_item.id:
error = "Subscription item missing ID. Subscription must be retrieved from server first."
raise MissingRequiredFieldError(error)
url = "{0}/{1}".format(self.baseurl, subscription_item.id)
update_req = RequestFactory.Subscription.update_req(subscription_item)
server_response = self.put_request(url, update_req)
logger.info("Updated subscription item (ID: {0})".format(subscription_item.id))
return SubscriptionItem.from_response(server_response.content, self.parent_srv.namespace)[0]