Skip to content

Commit 3f5aec2

Browse files
author
marcel corso gonzalez
authored
Merge branch 'master' into drop-python2-support
2 parents 7f0e25b + c4decdb commit 3f5aec2

File tree

6 files changed

+81
-24
lines changed

6 files changed

+81
-24
lines changed

README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,6 @@ To run examples with arguments, try:
7878
$ python ./examples/voice_create_webhook.py --accessKey accessKeyWhichNotExist --url https://example.com --title HELLO_WEBHOOK --token HELLO_TOKEN
7979
```
8080

81-
Conversations WhatsApp Sandbox
82-
-------------
83-
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.
84-
85-
```python
86-
client = messagebird.Client('YOUR_ACCESS_KEY', features=[messagebird.Feature.ENABLE_CONVERSATIONS_API_WHATSAPP_SANDBOX])
87-
```
88-
8981
Documentation
9082
-------------
9183
Complete documentation, instructions, and examples are available at:

examples/verify_create_email.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import sys, os
2+
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
3+
4+
import messagebird
5+
6+
# ACCESS_KEY = ''
7+
# RECIPIENT = ''
8+
# ORIGINATOR = ''
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+
RECIPIENT
18+
except NameError:
19+
print('You need to set a RECIPIENT constant in this file')
20+
sys.exit(1)
21+
22+
try:
23+
ORIGINATOR
24+
except NameError:
25+
print('You need to set a ORIGINATOR constant in this file')
26+
sys.exit(1)
27+
28+
try:
29+
# Create a MessageBird client with the specified ACCESS_KEY.
30+
client = messagebird.Client(ACCESS_KEY)
31+
32+
# Create a new Verify.
33+
verify = client.verify_create_email(RECIPIENT, ORIGINATOR, {
34+
'subject': 'your code'
35+
})
36+
37+
# Print the object information.
38+
print('\nThe following information was returned as a Verify object:\n')
39+
print(' id : %s' % verify.id)
40+
print(' href : %s' % verify.href)
41+
print(' recipient : %s' % verify.recipient)
42+
print(' reference : %s' % verify.reference)
43+
print(' messages : %s' % verify.messages)
44+
print(' status : %s' % verify.status)
45+
print(' createdDatetime : %s' % verify.createdDatetime)
46+
print(' validUntilDatetime : %s\n' % verify.validUntilDatetime)
47+
48+
except messagebird.client.ErrorException as e:
49+
print('\nAn error occured while requesting a Verify object:\n')
50+
51+
for error in e.errors:
52+
print(' code : %d' % error.code)
53+
print(' description : %s' % error.description)
54+
print(' parameter : %s\n' % error.parameter)

messagebird/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
from messagebird.client import Client, ErrorException, Feature
1+
from messagebird.client import Client, ErrorException
22
from messagebird.signed_request import SignedRequest

messagebird/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66

77
class Base:
88
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)
9+
if data is not None:
10+
for name, value in list(data.items()):
11+
if hasattr(self, name) and not callable(getattr(self, name)):
12+
setattr(self, name, value)
1213

1314
return self
1415

messagebird/client.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import sys
22
import json
33
import io
4-
import enum
54

65
from messagebird.balance import Balance
76
from messagebird.call import Call
@@ -32,8 +31,6 @@
3231
REST_TYPE = 'rest'
3332

3433
CONVERSATION_API_ROOT = 'https://conversations.messagebird.com/v1/'
35-
CONVERSATION_API_WHATSAPP_SANDBOX_ROOT = 'https://whatsapp-sandbox.messagebird.com/v1/'
36-
3734
CONVERSATION_PATH = 'conversations'
3835
CONVERSATION_MESSAGES_PATH = 'messages'
3936
CONVERSATION_WEB_HOOKS_PATH = 'webhooks'
@@ -65,24 +62,18 @@ def __init__(self, errorMessage):
6562
super(SignleErrorException, self).__init__(errorMessage)
6663

6764

68-
class Feature(enum.Enum):
69-
ENABLE_CONVERSATIONS_API_WHATSAPP_SANDBOX = 1
70-
7165

72-
class Client:
73-
74-
def __init__(self, access_key, http_client=None, features=[]):
66+
class Client(object):
67+
def __init__(self, access_key, http_client=None):
7568
self.access_key = access_key
7669
self.http_client = http_client
7770

78-
self.conversation_api_root = CONVERSATION_API_WHATSAPP_SANDBOX_ROOT if Feature.ENABLE_CONVERSATIONS_API_WHATSAPP_SANDBOX in features else CONVERSATION_API_ROOT
79-
8071
def _get_http_client(self, type=REST_TYPE):
8172
if self.http_client:
8273
return self.http_client
8374

8475
if type == CONVERSATION_TYPE:
85-
return HttpClient(self.conversation_api_root, self.access_key, USER_AGENT)
76+
return HttpClient(CONVERSATION_API_ROOT, self.access_key, USER_AGENT)
8677

8778
if type == VOICE_TYPE:
8879
return HttpClient(VOICE_API_ROOT, self.access_key, USER_AGENT)
@@ -294,6 +285,17 @@ def verify_create(self, recipient, params=None):
294285
params.update({'recipient': recipient})
295286
return Verify().load(self.request('verify', 'POST', params))
296287

288+
def verify_create_email(self, recipient, originator, params=None):
289+
"""Create a new email verification."""
290+
if params is None:
291+
params = {}
292+
params.update({
293+
'type' : 'email',
294+
'recipient': recipient,
295+
'originator': originator
296+
})
297+
return Verify().load(self.request('verify', 'POST', params))
298+
297299
def verify_verify(self, id, token):
298300
"""Verify the token of a specific verification."""
299301
return Verify().load(self.request('verify/' + str(id), params={'token': token}))

tests/test_verify.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ def test_verify_create(self):
2424

2525
http_client.request.assert_called_once_with('verify', 'POST', {'recipient': '31612345678'})
2626

27+
def test_verify_create_email(self):
28+
http_client = Mock()
29+
http_client.request.return_value = '{}'
30+
31+
Client('', http_client).verify_create_email('[email protected]', '[email protected]')
32+
33+
http_client.request.assert_called_once_with('verify', 'POST', {'recipient': '[email protected]', 'originator': '[email protected]', 'type': 'email'})
34+
2735
def test_verify_verify(self):
2836
http_client = Mock()
2937
http_client.request.return_value = '{"id": "verify-id","href": "https://rest.messagebird.com/verify/verify-id","recipient": 31612345678,"reference": "MyReference","messages": {"href": "https://rest.messagebird.com/messages/63b168423592d681641eb07b76226648"},"status": "verified","createdDatetime": "2017-05-30T12:39:50+00:00","validUntilDatetime": "2017-05-30T12:40:20+00:00"}'

0 commit comments

Comments
 (0)