Skip to content

Commit 4169a41

Browse files
authored
Merge pull request #47 from sintah/ON-2
ON-2 Python sdk: CRUD Voice API resource - Webhooks
2 parents e204d35 + 352fc05 commit 4169a41

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1045
-421
lines changed

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ install:
1111
- pip install requests
1212
- pip install codecov
1313
- pip install pytest pytest-cov
14+
- pip install pycodestyle
1415
- pip install .
1516
script:
1617
- coverage run --source=messagebird -m unittest discover -s tests/
1718
- coverage report --fail-under=80
19+
- pycodestyle --statistics --ignore=E121,E123,E126,E133,E226,E241,E242,E704,W503,W504,W505,E501 ./messagebird/
20+
- pycodestyle --statistics --ignore=E121,E123,E126,E133,E226,E241,E242,E704,W503,W504,W505,E501 ./tests/
1821
matrix:
1922
allow_failures:
2023
- python: 'nightly'

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ Your balance:
7373

7474
Please see the other examples for a complete overview of all the available API calls.
7575

76+
To run examples with arguments, try:
77+
```shell script
78+
$ python ./examples/voice_create_webhook.py --accessKey accessKeyWhichNotExist --url https://example.com --title HELLO_WEBHOOK --token HELLO_TOKEN
79+
```
80+
7681
Conversations WhatsApp Sandbox
7782
-------------
7883
To use the whatsapp sandbox you need to add `messagebird.Feature.ENABLE_CONVERSATIONS_API_WHATSAPP_SANDBOX` to the list of features you want enabled. Don't forget to replace `YOUR_ACCESS_KEY` with your actual access key.

examples/voice_create_webhook.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
import argparse
3+
import messagebird
4+
from messagebird.voice_webhook import VoiceCreateWebhookRequest
5+
6+
parser = argparse.ArgumentParser()
7+
parser.add_argument('--accessKey', help='access key for MessageBird API', type=str, required=True)
8+
parser.add_argument('--url', help='url for the webhook', type=str, required=True)
9+
parser.add_argument('--title', help='title for the webhook', type=str)
10+
parser.add_argument('--token', help='token for the webhook', type=str)
11+
args = vars(parser.parse_args())
12+
13+
try:
14+
client = messagebird.Client(args['accessKey'])
15+
16+
create_webhook_request = VoiceCreateWebhookRequest(url=args['url'], title=args['title'], token=args['token'])
17+
webhook = client.voice_create_webhook(create_webhook_request)
18+
19+
# Print the object information.
20+
print('\nThe following information was returned as a Voice Webhook object:\n')
21+
print(' id : {}'.format(webhook.id))
22+
print(' token : {}'.format(webhook.token))
23+
print(' url : {}'.format(webhook.url))
24+
print(' createdAt : {}'.format(webhook.createdAt))
25+
print(' updatedAt : {}'.format(webhook.updatedAt))
26+
27+
except messagebird.client.ErrorException as e:
28+
print('An error occured while creating a Voice Webhook object:')
29+
30+
for error in e.errors:
31+
print(' code : {}'.format(error.code))
32+
print(' description : {}'.format(error.description))
33+
print(' parameter : {}\n'.format(error.parameter))
34+
35+
36+

examples/voice_delete_webhook.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
import argparse
3+
import messagebird
4+
5+
parser = argparse.ArgumentParser()
6+
parser.add_argument('--accessKey', help='access key for MessageBird API', type=str, required=True)
7+
parser.add_argument('--webhookId', help='webhook that you want to update', type=str, required=True)
8+
9+
args = vars(parser.parse_args())
10+
11+
try:
12+
client = messagebird.Client(args['accessKey'])
13+
webhook = client.voice_delete_webhook(args['webhookId'])
14+
15+
# Print the object information.
16+
print('Webhook has been deleted')
17+
18+
except messagebird.client.ErrorException as e:
19+
print('An error occured while deleting a Voice Webhook object:')
20+
21+
for error in e.errors:
22+
print(' code : {}'.format(error.code))
23+
print(' description : {}'.format(error.description))
24+
print(' parameter : {}\n'.format(error.parameter))

examples/voice_list_webhook.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
import argparse
3+
import messagebird
4+
from messagebird.voice_webhook import VoiceCreateWebhookRequest
5+
6+
parser = argparse.ArgumentParser()
7+
parser.add_argument('--accessKey', help='access key for MessageBird API', type=str, required=True)
8+
9+
args = vars(parser.parse_args())
10+
11+
try:
12+
client = messagebird.Client(args['accessKey'])
13+
14+
webhooks_list = client.voice_list_webhooks(limit=5, offset=0)
15+
16+
if webhooks_list is None or webhooks_list.data is None:
17+
print("\nNo webhooks\n")
18+
exit(0)
19+
20+
# Print the object information.
21+
print('\nThe following information was returned as a Voice Webhook objects:\n')
22+
for webhook in webhooks_list.data:
23+
print('{')
24+
print(' id : {}'.format(webhook.id))
25+
print(' token : {}'.format(webhook.token))
26+
print(' url : {}'.format(webhook.url))
27+
print(' createdAt : {}'.format(webhook.createdAt))
28+
print(' updatedAt : {}'.format(webhook.updatedAt))
29+
print('}\n')
30+
31+
except messagebird.client.ErrorException as e:
32+
print('An error occured while reading a Voice Webhook object:')
33+
34+
for error in e.errors:
35+
print(' code : {}'.format(error.code))
36+
print(' description : {}'.format(error.description))
37+
print(' parameter : {}\n'.format(error.parameter))

examples/voice_read_webhook.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
import argparse
3+
import messagebird
4+
from messagebird.voice_webhook import VoiceCreateWebhookRequest
5+
6+
parser = argparse.ArgumentParser()
7+
parser.add_argument('--accessKey', help='access key for MessageBird API', type=str, required=True)
8+
parser.add_argument('--webhookId', help='webhook that you want to update', type=str, required=True)
9+
10+
args = vars(parser.parse_args())
11+
12+
try:
13+
client = messagebird.Client(args['accessKey'])
14+
15+
webhook = client.voice_read_webhook(args['webhookId'])
16+
17+
# Print the object information.
18+
print('\nThe following information was returned as a Voice Webhook object:\n')
19+
20+
print(' id : {}'.format(webhook.id))
21+
print(' token : {}'.format(webhook.token))
22+
print(' url : {}'.format(webhook.url))
23+
print(' createdAt : {}'.format(webhook.createdAt))
24+
print(' updatedAt : {}'.format(webhook.updatedAt))
25+
26+
27+
except messagebird.client.ErrorException as e:
28+
print('An error occured while reading a Voice Webhook object:')
29+
30+
for error in e.errors:
31+
print(' code : {}'.format(error.code))
32+
print(' description : {}'.format(error.description))
33+
print(' parameter : {}\n'.format(error.parameter))

examples/voice_update_webhook.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
import argparse
3+
import messagebird
4+
from messagebird.voice_webhook import VoiceUpdateWebhookRequest
5+
6+
parser = argparse.ArgumentParser()
7+
parser.add_argument('--accessKey', help='access key for MessageBird API', type=str, required=True)
8+
parser.add_argument('--webhookId', help='webhook that you want to update', type=str, required=True)
9+
parser.add_argument('--title', help='title for the webhook', type=str)
10+
parser.add_argument('--token', help='token for the webhook', type=str)
11+
12+
args = vars(parser.parse_args())
13+
14+
try:
15+
client = messagebird.Client(args['accessKey'])
16+
17+
update_webhook_request = VoiceUpdateWebhookRequest(title=args['title'], token=args['token'])
18+
webhook = client.voice_update_webhook(args['webhookId'], update_webhook_request)
19+
20+
# Print the object information.
21+
print('\nThe following information was returned as a Voice Webhook object:\n')
22+
print(' id : {}'.format(webhook.id))
23+
print(' token : {}'.format(webhook.token))
24+
print(' url : {}'.format(webhook.url))
25+
print(' createdAt : {}'.format(webhook.createdAt))
26+
print(' updatedAt : {}'.format(webhook.updatedAt))
27+
28+
except messagebird.client.ErrorException as e:
29+
print('An error occured while updating a Voice Webhook object:')
30+
31+
for error in e.errors:
32+
print(' code : {}'.format(error.code))
33+
print(' description : {}'.format(error.description))
34+
print(' parameter : {}\n'.format(error.parameter))
35+
36+
37+

messagebird/balance.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from messagebird.base import Base
22

3+
34
class Balance(Base):
4-
def __init__(self):
5-
self.amount = None
6-
self.type = None
7-
self.payment = None
5+
def __init__(self):
6+
self.amount = None
7+
self.type = None
8+
self.payment = None

messagebird/base.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
from datetime import datetime
22

33
import dateutil.parser
4+
import json
45

56

67
class Base(object):
7-
def load(self, data):
8-
for name, value in list(data.items()):
9-
if hasattr(self, name) and not callable(getattr(self,name)):
10-
setattr(self, name, value)
8+
def load(self, data):
9+
for name, value in list(data.items()):
10+
if hasattr(self, name) and not callable(getattr(self, name)):
11+
setattr(self, name, value)
1112

12-
return self
13+
return self
1314

14-
@staticmethod
15-
def value_to_time(value, format='%Y-%m-%dT%H:%M:%S+00:00'):
16-
if value is not None:
17-
return dateutil.parser.parse(value).replace(microsecond=0)
15+
@staticmethod
16+
def value_to_time(value, format='%Y-%m-%dT%H:%M:%S+00:00'):
17+
if value is not None:
18+
return dateutil.parser.parse(value).replace(microsecond=0)

messagebird/call.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ def __init__(self):
1515
@property
1616
def data(self):
1717
return self._data
18-
18+
1919
@data.setter
2020
def data(self, value):
2121
self._data = CallData().load(value[0])
2222

2323
def __str__(self):
2424
return "\n".join([
2525
'id : %s' % self.id,
26-
'data.'+'data.'.join(str(self._data).splitlines(True)),
27-
])
26+
'data.' + 'data.'.join(str(self._data).splitlines(True)),
27+
])

messagebird/call_data.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from messagebird.base import Base
22
from messagebird.webhook import Webhook
33

4+
45
class CallData(Base):
5-
6+
67
def __init__(self):
78
self.id = None
89
self.status = None
@@ -13,27 +14,26 @@ def __init__(self):
1314
self._endedAt = None
1415
self._webhook = None
1516

16-
1717
@property
1818
def updatedAt(self):
1919
return self._updatedAt
20-
20+
2121
@updatedAt.setter
2222
def updatedAt(self, value):
2323
self._updatedAt = self.value_to_time(value, '%Y-%m-%dT%H:%M:%SZ')
2424

2525
@property
2626
def createdAt(self):
2727
return self._createdAt
28-
28+
2929
@createdAt.setter
3030
def createdAt(self, value):
3131
self._createdAt = self.value_to_time(value, '%Y-%m-%dT%H:%M:%SZ')
3232

3333
@property
3434
def endedAt(self):
3535
return self._endedAt
36-
36+
3737
@endedAt.setter
3838
def endedAt(self, value):
3939
self._endedAt = self.value_to_time(value, '%Y-%m-%dT%H:%M:%SZ')
@@ -56,4 +56,4 @@ def __str__(self):
5656
'updatedAt : %s' % self.updatedAt,
5757
'createdAt : %s' % self.createdAt,
5858
'endedAt : %s' % self.endedAt,
59-
])
59+
])

messagebird/call_flow.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def data(self, value):
6060

6161
self._data = items
6262

63+
6364
class CallFlow(Base):
6465

6566
def __init__(self):
@@ -134,4 +135,4 @@ def load(self, data):
134135
if hasattr(self, name) and not callable(getattr(self, name)):
135136
setattr(self, name, value)
136137

137-
return self
138+
return self

messagebird/call_list.py

-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,3 @@ def data(self, value):
3939
if isinstance(value, list):
4040
self.count = len(value)
4141
self.items = value
42-

0 commit comments

Comments
 (0)