Skip to content

Commit 5ae0c1f

Browse files
committed
Add read webhook method
1 parent d14c0fa commit 5ae0c1f

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
webhook = client.read_webhook(WEBHOOK_ID)
27+
28+
# Print the object information.
29+
print('\nThe following information was returned as a Webhook object:\n')
30+
print(' id : %s' % webhook.id)
31+
print(' events : %s' % webhook.events)
32+
print(' channel id : %s' % webhook.channelId)
33+
print(' created date : %s' % webhook.createdDatetime)
34+
print(' updated date : %s' % webhook.updatedDatetime)
35+
36+
except messagebird.client.ErrorException as e:
37+
print('\nAn error occured while requesting a Webhook object:\n')
38+
39+
for error in e.errors:
40+
print(' code : %d' % error.code)
41+
print(' description : %s' % error.description)
42+
print(' parameter : %s\n' % error.parameter)

messagebird/conversation_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,6 @@ def list_webhooks(self, options=None):
7575

7676
return ConversationWebhookList().load(self.client.request(uri))
7777

78-
def read_webhook(self):
79-
return self.access_key
78+
def read_webhook(self, id):
79+
uri = CONVERSATION_WEB_HOOKS_PATH + '/' + str(id)
80+
return ConversationWebhook().load(self.client.request(uri))

tests/test_conversation_webhook.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,14 @@ def test_conversation_webhook_list_pagination(self):
5252
params = {'offset': 1, 'limit': 2}
5353
ConversationClient('', http_client).list_webhooks(params)
5454
http_client.request.assert_called_once_with('webhooks?offset=1&limit=2', 'GET', None)
55+
56+
def test_conversation_webhook_read(self):
57+
http_client = Mock()
58+
http_client.request.return_value = '{"id":"5031e2da142d401c93fbc38518ebb604","url":"https://example.com","channelId":"c0dae31e440145e094c4708b7d908842","events":["conversation.created","conversation.updated"],"status":"enabled","createdDatetime":"2019-04-03T08:41:37Z","updatedDatetime":null}'
59+
60+
webhook = ConversationClient('', http_client).read_webhook('webhook-id')
61+
62+
http_client.request.assert_called_once_with('webhooks/webhook-id', 'GET', None)
63+
self.assertEqual(datetime(2019, 4, 3, 8, 41, 37), webhook.createdDatetime)
64+
self.assertEqual(None, webhook.updatedDatetime)
65+
self.assertEqual(['conversation.created', 'conversation.updated'], webhook.events)

0 commit comments

Comments
 (0)