Skip to content

Commit d14c0fa

Browse files
committed
Add delete, list webhook methods
1 parent ecb55a5 commit d14c0fa

File tree

7 files changed

+139
-9
lines changed

7 files changed

+139
-9
lines changed

examples/conversation_create_webhook.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@
3333
})
3434

3535
# Print the object information.
36-
print('\nThe following information was returned as a Conversation List object:\n')
36+
print('\nThe following information was returned as a Webhook object:\n')
3737
print(' id : %s' % webhook.id)
3838
print(' events : %s' % webhook.events)
3939
print(' channel id : %s' % webhook.channelId)
4040
print(' created date : %s' % webhook.createdDatetime)
4141
print(' updated date : %s' % webhook.updatedDatetime)
4242

4343
except messagebird.client.ErrorException as e:
44-
print('\nAn error occured while requesting a Message object:\n')
44+
print('\nAn error occured while requesting a Webhook object:\n')
4545

4646
for error in e.errors:
4747
print(' code : %d' % error.code)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
3+
import sys, os
4+
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
5+
6+
import messagebird
7+
8+
# ACCESS_KEY = ''
9+
# WEBHOOK_ID = ''
10+
11+
try:
12+
ACCESS_KEY
13+
except NameError:
14+
print('You need to set an ACCESS_KEY constant in this file')
15+
sys.exit(1)
16+
17+
try:
18+
WEBHOOK_ID
19+
except NameError:
20+
print('You need to set an WEBHOOK_ID constant in this file')
21+
sys.exit(1)
22+
23+
try:
24+
client = messagebird.ConversationClient(ACCESS_KEY)
25+
26+
client.delete_webhook(WEBHOOK_ID)
27+
28+
# Print the object information.
29+
print('\nWebhook has been deleted:\n')
30+
31+
except messagebird.client.ErrorException as e:
32+
print('\nAn error occured while requesting a Webhook object:\n')
33+
34+
for error in e.errors:
35+
print(' code : %d' % error.code)
36+
print(' description : %s' % error.description)
37+
print(' parameter : %s\n' % error.parameter)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python
2+
3+
import sys, os
4+
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
5+
6+
import messagebird
7+
8+
# ACCESS_KEY = ''
9+
10+
try:
11+
ACCESS_KEY
12+
except NameError:
13+
print('You need to set an ACCESS_KEY constant in this file')
14+
sys.exit(1)
15+
16+
try:
17+
client = messagebird.ConversationClient(ACCESS_KEY)
18+
19+
webhookList = client.list()
20+
21+
itemIds = []
22+
for msgItem in webhookList.items:
23+
itemIds.append(msgItem.id)
24+
25+
# Print the object information.
26+
print('\nThe following information was returned as a Conversation Webhook List object:\n')
27+
print(' conversation ids : %s' % itemIds)
28+
print(' offset : %s' % webhookList.offset)
29+
print(' limit : %s' % webhookList.limit)
30+
print(' totalCount : %s' % webhookList.totalCount)
31+
32+
except messagebird.client.ErrorException as e:
33+
print('\nAn error occured while requesting a Conversation Webhook List object:\n')
34+
35+
for error in e.errors:
36+
print(' code : %d' % error.code)
37+
print(' description : %s' % error.description)
38+
print(' parameter : %s\n' % error.parameter)

messagebird/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def __init__(self, access_key, http_client=None):
3838
def request(self, path, method='GET', params=None):
3939
"""Builds a request, gets a response and decodes it."""
4040
response_text = self.http_client.request(path, method, params)
41+
42+
if not response_text:
43+
return response_text
44+
4145
response_json = json.loads(response_text)
4246

4347
if 'errors' in response_json:

messagebird/conversation_client.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from messagebird.http_client import HttpClient
44
from messagebird.conversation_message import ConversationMessage, ConversationMessageList
55
from messagebird.conversation import Conversation, ConversationList
6-
from messagebird.conversation_webhook import ConversationWebhook
6+
from messagebird.conversation_webhook import ConversationWebhook, ConversationWebhookList
77

88
try:
99
from urllib.parse import urlencode
@@ -64,11 +64,16 @@ def read_message(self, message_id):
6464
def create_webhook(self, webhook_create_request):
6565
return ConversationWebhook().load(self.client.request(CONVERSATION_WEB_HOOKS_PATH, 'POST', webhook_create_request))
6666

67-
def delete_webhook(self):
68-
return self.access_key
67+
def delete_webhook(self, id):
68+
uri = CONVERSATION_WEB_HOOKS_PATH + '/' + str(id)
69+
self.client.request(uri, 'DELETE')
6970

70-
def list_webhooks(self):
71-
return self.access_key
71+
def list_webhooks(self, options=None):
72+
uri = CONVERSATION_WEB_HOOKS_PATH
73+
if options is not None:
74+
uri += '?' + urlencode(options)
75+
76+
return ConversationWebhookList().load(self.client.request(uri))
7277

7378
def read_webhook(self):
7479
return self.access_key

messagebird/conversation_webhook.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,26 @@ def updatedDatetime(self):
3030

3131
@updatedDatetime.setter
3232
def updatedDatetime(self, value):
33-
self._updatedDatetime = self.value_to_time(value)
33+
self._updatedDatetime = self.value_to_time(value, '%Y-%m-%dT%H:%M:%SZ')
34+
35+
36+
class ConversationWebhookList(Base):
37+
def __init__(self):
38+
self.offset = None
39+
self.limit = None
40+
self.count = None
41+
self.totalCount = None
42+
self._items = None
43+
44+
@property
45+
def items(self):
46+
return self._items
47+
48+
@items.setter
49+
def items(self, value):
50+
items = []
51+
if isinstance(value, list):
52+
for item in value:
53+
items.append(ConversationWebhook().load(item))
54+
55+
self._items = items

tests/test_conversation_webhook.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,28 @@ def test_conversation_webhook_create(self):
2727

2828
ConversationClient('', http_client).create_webhook(webhookRequestData)
2929

30-
http_client.request.assert_called_once_with('webhooks', 'POST', webhookRequestData)
30+
http_client.request.assert_called_once_with('webhooks', 'POST', webhookRequestData)
31+
32+
def test_conversation_webhook_delete(self):
33+
http_client = Mock()
34+
http_client.request.return_value = ''
35+
36+
ConversationClient('', http_client).delete_webhook('webhook-id')
37+
38+
http_client.request.assert_called_once_with('webhooks/webhook-id', 'DELETE', None)
39+
40+
41+
def test_conversation_webhook_list(self):
42+
http_client = Mock()
43+
http_client.request.return_value = '{"offset":0,"limit":10,"count":2,"totalCount":2,"items":[{"id":"57b96dbe0fda40f0a814f5e3268c30a9","contactId":"8846d44229094c20813cf9eea596e680","contact":{"id":"8846d44229094c20813cf9eea596e680","href":"https://contacts.messagebird.com/v2/contacts/8846d44229094c20813cf9eea596e680","msisdn":31617110163,"displayName":"31617110163","firstName":"","lastName":"","customDetails":{},"attributes":{},"createdDatetime":"2019-04-02T08:54:39Z","updatedDatetime":"2019-04-02T08:54:40Z"},"channels":[{"id":"c0dae31e440145e094c4708b7d908842","name":"test","platformId":"sms","status":"active","createdDatetime":"2019-04-01T15:25:12Z","updatedDatetime":"0001-01-01T00:00:00Z"}],"status":"active","createdDatetime":"2019-04-02T08:54:38Z","updatedDatetime":"2019-04-02T14:24:09.192202886Z","lastReceivedDatetime":"2019-04-02T14:24:09.14826339Z","lastUsedChannelId":"c0dae31e440145e094c4708b7d908842","messages":{"totalCount":2,"href":"https://conversations.messagebird.com/v1/conversations/57b96dbe0fda40f0a814f5e3268c30a9/messages"}},{"id":"07e823fdb36a462fb5e187d6d7b96493","contactId":"459a35432b0c4195abbdae353eb19359","contact":{"id":"459a35432b0c4195abbdae353eb19359","href":"https://contacts.messagebird.com/v2/contacts/459a35432b0c4195abbdae353eb19359","msisdn":31615164888,"displayName":"31615164888","firstName":"","lastName":"","customDetails":{},"attributes":{},"createdDatetime":"2019-04-02T08:19:37Z","updatedDatetime":"2019-04-02T08:19:38Z"},"channels":[{"id":"c0dae31e440145e094c4708b7d908842","name":"test","platformId":"sms","status":"active","createdDatetime":"2019-04-01T15:25:12Z","updatedDatetime":"0001-01-01T00:00:00Z"}],"status":"active","createdDatetime":"2019-04-02T08:19:37Z","updatedDatetime":"2019-04-03T07:35:47.35395356Z","lastReceivedDatetime":"2019-04-02T12:02:22.707634424Z","lastUsedChannelId":"c0dae31e440145e094c4708b7d908842","messages":{"totalCount":16,"href":"https://conversations.messagebird.com/v1/conversations/07e823fdb36a462fb5e187d6d7b96493/messages"}}]}'
44+
45+
ConversationClient('', http_client).list_webhooks()
46+
http_client.request.assert_called_once_with('webhooks', 'GET', None)
47+
48+
def test_conversation_webhook_list_pagination(self):
49+
http_client = Mock()
50+
http_client.request.return_value = '{"offset":0,"limit":10,"count":2,"totalCount":2,"items":[{"id":"57b96dbe0fda40f0a814f5e3268c30a9","contactId":"8846d44229094c20813cf9eea596e680","contact":{"id":"8846d44229094c20813cf9eea596e680","href":"https://contacts.messagebird.com/v2/contacts/8846d44229094c20813cf9eea596e680","msisdn":31617110163,"displayName":"31617110163","firstName":"","lastName":"","customDetails":{},"attributes":{},"createdDatetime":"2019-04-02T08:54:39Z","updatedDatetime":"2019-04-02T08:54:40Z"},"channels":[{"id":"c0dae31e440145e094c4708b7d908842","name":"test","platformId":"sms","status":"active","createdDatetime":"2019-04-01T15:25:12Z","updatedDatetime":"0001-01-01T00:00:00Z"}],"status":"active","createdDatetime":"2019-04-02T08:54:38Z","updatedDatetime":"2019-04-02T14:24:09.192202886Z","lastReceivedDatetime":"2019-04-02T14:24:09.14826339Z","lastUsedChannelId":"c0dae31e440145e094c4708b7d908842","messages":{"totalCount":2,"href":"https://conversations.messagebird.com/v1/conversations/57b96dbe0fda40f0a814f5e3268c30a9/messages"}},{"id":"07e823fdb36a462fb5e187d6d7b96493","contactId":"459a35432b0c4195abbdae353eb19359","contact":{"id":"459a35432b0c4195abbdae353eb19359","href":"https://contacts.messagebird.com/v2/contacts/459a35432b0c4195abbdae353eb19359","msisdn":31615164888,"displayName":"31615164888","firstName":"","lastName":"","customDetails":{},"attributes":{},"createdDatetime":"2019-04-02T08:19:37Z","updatedDatetime":"2019-04-02T08:19:38Z"},"channels":[{"id":"c0dae31e440145e094c4708b7d908842","name":"test","platformId":"sms","status":"active","createdDatetime":"2019-04-01T15:25:12Z","updatedDatetime":"0001-01-01T00:00:00Z"}],"status":"active","createdDatetime":"2019-04-02T08:19:37Z","updatedDatetime":"2019-04-03T07:35:47.35395356Z","lastReceivedDatetime":"2019-04-02T12:02:22.707634424Z","lastUsedChannelId":"c0dae31e440145e094c4708b7d908842","messages":{"totalCount":16,"href":"https://conversations.messagebird.com/v1/conversations/07e823fdb36a462fb5e187d6d7b96493/messages"}}]}'
51+
52+
params = {'offset': 1, 'limit': 2}
53+
ConversationClient('', http_client).list_webhooks(params)
54+
http_client.request.assert_called_once_with('webhooks?offset=1&limit=2', 'GET', None)

0 commit comments

Comments
 (0)