-
Notifications
You must be signed in to change notification settings - Fork 436
Add subscriptions #240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add subscriptions #240
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d43dd3a
add subscription item, endpoint and added to server
999f599
wip
41e6d17
working without tests
0cb2f11
added some basic tests
49dfe76
unneeded
07f7745
add api version
d48e1fe
style guide police checking in
4d81274
see last commimt
b7acea5
delete this
1b229b8
review changes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import xml.etree.ElementTree as ET | ||
from .exceptions import UnpopulatedPropertyError | ||
from .target import Target | ||
|
||
|
||
class SubscriptionItem(object): | ||
|
||
def __init__(self, subject, schedule_id, user_id, target): | ||
self.id = None | ||
self.subject = subject | ||
self.schedule_id = schedule_id | ||
self.user_id = user_id | ||
self.target = target | ||
|
||
def __repr__(self): | ||
if self.id is not None: | ||
return "<Subscription#{id} subject({subject}) schedule_id({schedule_id}) user_id({user_id}) \ | ||
target({target})".format(**self.__dict__) | ||
else: | ||
return "<Subscription subject({subject}) schedule_id({schedule_id}) user_id({user_id}) \ | ||
target({target})".format(**self.__dict__) | ||
|
||
def _set_id(self, id_): | ||
self.id = id_ | ||
|
||
@classmethod | ||
def from_response(cls, xml, ns): | ||
parsed_response = ET.fromstring(xml) | ||
all_subscriptions_xml = parsed_response.findall( | ||
'.//t:subscription', namespaces=ns) | ||
|
||
all_subscriptions = [SubscriptionItem._parse_element(x, ns) for x in all_subscriptions_xml] | ||
return all_subscriptions | ||
|
||
@classmethod | ||
def _parse_element(cls, element, ns): | ||
schedule_id = None | ||
target = None | ||
|
||
schedule_element = element.find('.//t:schedule', namespaces=ns) | ||
content_element = element.find('.//t:content', namespaces=ns) | ||
user_element = element.find('.//t:user', namespaces=ns) | ||
|
||
if schedule_element is not None: | ||
schedule_id = schedule_element.get('id', None) | ||
|
||
if content_element is not None: | ||
target = Target(content_element.get('id', None), content_element.get('type')) | ||
|
||
if user_element is not None: | ||
user_id = user_element.get('id') | ||
|
||
id_ = element.get('id', None) | ||
subject = element.get('subject', None) | ||
sub = cls(subject, schedule_id, user_id, target) | ||
sub._set_id(id_) | ||
return sub |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
tableauserverclient/server/endpoint/subscriptions_endpoint.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from .endpoint import Endpoint, api | ||
from .exceptions import MissingRequiredFieldError | ||
from .. import RequestFactory, SubscriptionItem, PaginationItem | ||
import logging | ||
|
||
logger = logging.getLogger('tableau.endpoint.subscriptions') | ||
|
||
|
||
class Subscriptions(Endpoint): | ||
@property | ||
def baseurl(self): | ||
return "{0}/sites/{1}/subscriptions".format(self.parent_srv.baseurl, | ||
self.parent_srv.site_id) | ||
|
||
@api(version='2.3') | ||
def get(self, req_options=None): | ||
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): | ||
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): | ||
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): | ||
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<tsResponse | ||
xmlns="http://tableau.com/api" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.6.xsd"> | ||
<pagination pageNumber="1" pageSize="100" totalAvailable="2" /> | ||
<subscriptions> | ||
<subscription id="382e9a6e-0c08-4a95-b6c1-c14df7bac3e4" subject="Not Found Alert"> | ||
<content id="cdd716ca-5818-470e-8bec-086885dbadee" type="View" /> | ||
<schedule id="7617c389-cdca-4940-a66e-69956fcebf3e" name="Subscribe daily [00:00 - 04:00, Pacific US] [migrated at 1490824351877]" /> | ||
<user id="c0d5fc44-ad8c-4957-bec0-b70ed0f8df1e" name="jonsnow@winterfell.com" /> | ||
</subscription> | ||
<subscription id="23cb7630-afc8-4c8e-b6cd-83ae0322ec66" subject="Last 7 Days"> | ||
<content id="2e6b4e8f-22dd-4061-8f75-bf33703da7e5" type="View" /> | ||
<schedule id="3407cd38-7b39-4983-86a6-67a1506a5e3f" name="SSS_27212a85-6b28-41f6-8c69-29b02043d7a5" /> | ||
<user id="c0d5fc44-ad8c-4957-bec0-b70ed0f8df1e" name="jonsnow@winterfell.com" /> | ||
</subscription> | ||
</subscriptions> | ||
</tsResponse> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<tsResponse | ||
xmlns="http://tableau.com/api" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.6.xsd"> | ||
<subscription id="382e9a6e-0c08-4a95-b6c1-c14df7bac3e4" subject="Not Found Alert"> | ||
<content id="cdd716ca-5818-470e-8bec-086885dbadee" type="View" /> | ||
<schedule id="7617c389-cdca-4940-a66e-69956fcebf3e" name="Subscribe daily [00:00 - 04:00, Pacific US] [migrated at 1490824351877]" /> | ||
<user id="c0d5fc44-ad8c-4957-bec0-b70ed0f8df1e" name="jonsnow@winterfell.com" /> | ||
</subscription> | ||
</tsResponse> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import unittest | ||
import os | ||
import requests_mock | ||
import tableauserverclient as TSC | ||
|
||
TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets") | ||
|
||
GET_XML = os.path.join(TEST_ASSET_DIR, "subscription_get.xml") | ||
GET_XML_BY_ID = os.path.join(TEST_ASSET_DIR, "subscription_get_by_id.xml") | ||
|
||
|
||
class SubscriptionTests(unittest.TestCase): | ||
def setUp(self): | ||
self.server = TSC.Server("http://test") | ||
self.server.version = '2.6' | ||
|
||
# Fake Signin | ||
self.server._site_id = "dad65087-b08b-4603-af4e-2887b8aafc67" | ||
self.server._auth_token = "j80k54ll2lfMZ0tv97mlPvvSCRyD0DOM" | ||
|
||
self.baseurl = self.server.subscriptions.baseurl | ||
|
||
def test_get_subscriptions(self): | ||
with open(GET_XML, "rb") as f: | ||
response_xml = f.read().decode("utf-8") | ||
with requests_mock.mock() as m: | ||
m.get(self.baseurl, text=response_xml) | ||
all_subscriptions, pagination_item = self.server.subscriptions.get() | ||
|
||
subscription = all_subscriptions[0] | ||
self.assertEqual('382e9a6e-0c08-4a95-b6c1-c14df7bac3e4', subscription.id) | ||
self.assertEqual('View', subscription.target.type) | ||
self.assertEqual('cdd716ca-5818-470e-8bec-086885dbadee', subscription.target.id) | ||
self.assertEqual('c0d5fc44-ad8c-4957-bec0-b70ed0f8df1e', subscription.user_id) | ||
self.assertEqual('Not Found Alert', subscription.subject) | ||
self.assertEqual('7617c389-cdca-4940-a66e-69956fcebf3e', subscription.schedule_id) | ||
|
||
def test_get_subscription_by_id(self): | ||
with open(GET_XML_BY_ID, "rb") as f: | ||
response_xml = f.read().decode("utf-8") | ||
with requests_mock.mock() as m: | ||
m.get(self.baseurl + '/382e9a6e-0c08-4a95-b6c1-c14df7bac3e4', text=response_xml) | ||
subscription = self.server.subscriptions.get_by_id('382e9a6e-0c08-4a95-b6c1-c14df7bac3e4') | ||
|
||
self.assertEqual('382e9a6e-0c08-4a95-b6c1-c14df7bac3e4', subscription.id) | ||
self.assertEqual('View', subscription.target.type) | ||
self.assertEqual('cdd716ca-5818-470e-8bec-086885dbadee', subscription.target.id) | ||
self.assertEqual('c0d5fc44-ad8c-4957-bec0-b70ed0f8df1e', subscription.user_id) | ||
self.assertEqual('Not Found Alert', subscription.subject) | ||
self.assertEqual('7617c389-cdca-4940-a66e-69956fcebf3e', subscription.schedule_id) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you have the id only settable via a private method? (If other Items do this to it's fine, just asking).
The other attributes are all available via
__init__
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
modeled it after the UserItem -- because when creating a new User you don't have an id.
Same goes for Subscription.