Skip to content

Commit

Permalink
Adding list webhook methods and tests. N.B. List webhooks have not ye…
Browse files Browse the repository at this point in the history
…t been released into production, hence the lack of re-building the gem and bumping the version with this commit.
  • Loading branch information
jdennes committed Dec 14, 2010
1 parent f12ec7a commit 1189fae
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
34 changes: 34 additions & 0 deletions createsend/list.py
Expand Up @@ -108,5 +108,39 @@ def update(self, title, unsubscribe_page, confirmed_opt_in, confirmation_success
"ConfirmationSuccessPage": confirmation_success_page }
response = self._put("/lists/%s.json" % self.list_id, json.dumps(body))

def webhooks(self):
"""Gets the webhooks for this list."""
response = self._get(self.uri_for("webhooks"))
return json_to_py(response)

def create_webhook(self, events, url, payload_format):
"""Creates a new webhook for the specified events (an array of strings).
Valid events are "Subscribe", "Unsubscribe", "Bounce", "Spam", and
"SubscriberUpdate". Valid payload formats are "json", and "xml"."""
body = {
"Events": events,
"Url": url,
"PayloadFormat": payload_format }
response = self._post(self.uri_for("webhooks"), json.dumps(body))
return json_to_py(response)

def test_webhook(self, webhook_id):
"""Tests that a post can be made to the endpoint specified for the webhook
identified by webhook_id."""
response = self._get(self.uri_for("webhooks/%s/test" % webhook_id))
return True # An exception will be raised if any error occurs

def delete_webhook(self, webhook_id):
"""Deletes a webhook associated with this list."""
response = self._delete("/lists/%s/webhooks/%s.json" % (self.list_id, webhook_id))

def activate_webhook(self, webhook_id):
"""Activates a webhook associated with this list."""
response = self._put(self.uri_for("webhooks/%s/activate" % webhook_id), ' ')

def deactivate_webhook(self, webhook_id):
"""De-activates a webhook associated with this list."""
response = self._put(self.uri_for("webhooks/%s/deactivate" % webhook_id), ' ')

def uri_for(self, action):
return "/lists/%s/%s.json" % (self.list_id, action)
1 change: 1 addition & 0 deletions test/fixtures/create_list_webhook.json
@@ -0,0 +1 @@
"6a783d359bd44ef62c6ca0d3eda4412a"
21 changes: 21 additions & 0 deletions test/fixtures/list_webhooks.json
@@ -0,0 +1,21 @@
[
{
"WebhookID": "943678317049bc13",
"Events": [
"Bounce",
"Spam"
],
"Url": "http://www.postbin.org/d9w8ud9wud9w",
"Status": "Active",
"PayloadFormat": "Json"
},
{
"WebhookID": "ee1b3864e5ca6161",
"Events": [
"Subscribe"
],
"Url": "http://www.postbin.org/hiuhiu2h2u",
"Status": "Active",
"PayloadFormat": "Xml"
}
]
36 changes: 36 additions & 0 deletions test/test_list.py
Expand Up @@ -128,3 +128,39 @@ def test_bounced(self):
self.assertEquals(res.Results[0].Date, "2010-10-25 13:11:00")
self.assertEquals(res.Results[0].State, "Bounced")
self.assertEquals(len(res.Results[0].CustomFields), 0)

def test_webhooks(self):
self.list.stub_request("lists/%s/webhooks.json" % self.list.list_id, "list_webhooks.json")
hooks = self.list.webhooks()
self.assertEquals(len(hooks), 2)
self.assertEquals(hooks[0].WebhookID, "943678317049bc13")
self.assertEquals(len(hooks[0].Events), 2)
self.assertEquals(hooks[0].Events[0], "Bounce")
self.assertEquals(hooks[0].Url, "http://www.postbin.org/d9w8ud9wud9w")
self.assertEquals(hooks[0].Status, "Active")
self.assertEquals(hooks[0].PayloadFormat, "Json")

def test_create_webhook(self):
self.list.stub_request("lists/%s/webhooks.json" % self.list.list_id, "create_list_webhook.json")
webhook_id = self.list.create_webhook(["Unsubscribe", "Spam"], "http://example.com/unsub", "json")
self.assertEquals(webhook_id, "6a783d359bd44ef62c6ca0d3eda4412a")

def test_test_webhook(self):
webhook_id = "jiuweoiwueoiwueowiueo"
self.list.stub_request("lists/%s/webhooks/%s/test.json" % (self.list.list_id, webhook_id), None)
self.list.test_webhook(webhook_id)

def test_delete_webhook(self):
webhook_id = "jiuweoiwueoiwueowiueo"
self.list.stub_request("lists/%s/webhooks/%s.json" % (self.list.list_id, webhook_id), None)
self.list.delete_webhook(webhook_id)

def test_activate_webhook(self):
webhook_id = "jiuweoiwueoiwueowiueo"
self.list.stub_request("lists/%s/webhooks/%s/activate.json" % (self.list.list_id, webhook_id), None)
self.list.activate_webhook(webhook_id)

def test_deactivate_webhook(self):
webhook_id = "jiuweoiwueoiwueowiueo"
self.list.stub_request("lists/%s/webhooks/%s/deactivate.json" % (self.list.list_id, webhook_id), None)
self.list.deactivate_webhook(webhook_id)

0 comments on commit 1189fae

Please sign in to comment.