Skip to content

Commit 41a7285

Browse files
authored
Merge pull request #40 from victormb84/GROW-1082-add-view-call-method
added view call method, test and example
2 parents adb6f21 + 06fd7a2 commit 41a7285

File tree

7 files changed

+153
-1
lines changed

7 files changed

+153
-1
lines changed

examples/call.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
#CALL_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+
CALL_ID
19+
except NameError:
20+
print('You need to set a CALL_ID constant in this file')
21+
sys.exit(1)
22+
23+
try:
24+
# Create a MessageBird client with the specified ACCESS_KEY.
25+
client = messagebird.Client(ACCESS_KEY)
26+
27+
# Fetch the Message object for the specified MESSAGE_ID.
28+
call = client.call(CALL_ID)
29+
30+
# Print the object information.
31+
print('\nThe following information was returned as a Message object:\n')
32+
print(' id : %s' % call.data.id)
33+
print(' status : %s' % call.data.status)
34+
print(' source : %s' % call.data.source)
35+
print(' destination : %s' % call.data.destination)
36+
print(' createdAt : %s' % call.data.createdAt)
37+
print(' updatedAt : %s' % call.data.updatedAt)
38+
print(' endedAt : %s' % call.data.endedAt)
39+
40+
except messagebird.client.ErrorException as e:
41+
print('\nAn error occured while requesting a Message object:\n')
42+
43+
for error in e.errors:
44+
print(' code : %d' % error.code)
45+
print(' description : %s' % error.description)
46+
print(' parameter : %s\n' % error.parameter)

messagebird/call.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from messagebird.base import Base
2+
from messagebird.call_data import CallData
3+
4+
CALL_STATUS_STARTING = "starting"
5+
CALL_STATUS_ONGOING = "ongoing"
6+
CALL_STATUS_ENDED = "ended"
7+
8+
9+
class Call(Base):
10+
11+
def __init__(self):
12+
self.id = None
13+
self._data = None
14+
15+
@property
16+
def data(self):
17+
return self._data
18+
19+
@data.setter
20+
def data(self, value):
21+
self._data = CallData().load(value[0])

messagebird/call_data.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from messagebird.base import Base
2+
from messagebird.webhook import Webhook
3+
4+
class CallData(Base):
5+
6+
def __init__(self):
7+
self.id = None
8+
self.status = None
9+
self.source = None
10+
self.destination = None
11+
self._createdAt = None
12+
self._updatedAt = None
13+
self._endedAt = None
14+
self._webhook = None
15+
16+
17+
@property
18+
def updatedAt(self):
19+
return self._updatedAt
20+
21+
@updatedAt.setter
22+
def updatedAt(self, value):
23+
self._updatedAt = self.value_to_time(value, '%Y-%m-%dT%H:%M:%SZ')
24+
25+
@property
26+
def createdAt(self):
27+
return self._createdAt
28+
29+
@createdAt.setter
30+
def createdAt(self, value):
31+
self._createdAt = self.value_to_time(value, '%Y-%m-%dT%H:%M:%SZ')
32+
33+
@property
34+
def endedAt(self):
35+
return self._endedAt
36+
37+
@endedAt.setter
38+
def endedAt(self, value):
39+
self._endedAt = self.value_to_time(value, '%Y-%m-%dT%H:%M:%SZ')
40+
41+
@property
42+
def webhook(self):
43+
return self._webhook
44+
45+
@webhook.setter
46+
def webhook(self, value):
47+
self._webhook = Webhook.load(value)

messagebird/client.py

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io
44

55
from messagebird.balance import Balance
6+
from messagebird.call import Call
67
from messagebird.contact import Contact, ContactList
78
from messagebird.error import Error
89
from messagebird.group import Group, GroupList
@@ -31,6 +32,7 @@
3132
CONVERSATION_TYPE = 'conversation'
3233

3334
VOICE_API_ROOT = 'https://voice.messagebird.com'
35+
VOICE_TYPE = 'voice'
3436
VOICE_PATH = 'calls'
3537
VOICE_LEGS_PATH = 'legs'
3638
VOICE_RECORDINGS_PATH = 'recordings'
@@ -55,6 +57,9 @@ def _get_http_client(self, type=REST_TYPE):
5557
if type == REST_TYPE:
5658
return HttpClient(ENDPOINT, self.access_key, USER_AGENT)
5759

60+
if type == VOICE_TYPE:
61+
return HttpClient(VOICE_API_ROOT, self.access_key, USER_AGENT)
62+
5863
return HttpClient(CONVERSATION_API_ROOT, self.access_key, USER_AGENT)
5964

6065
def request(self, path, method='GET', params=None, type=REST_TYPE):
@@ -104,6 +109,10 @@ def balance(self):
104109
"""Retrieve your balance."""
105110
return Balance().load(self.request('balance'))
106111

112+
def call(self,id):
113+
"""Retrieve the information of a specific call"""
114+
return Call().load(self.request('calls/' + str(id), 'GET', None, VOICE_TYPE))
115+
107116
def hlr(self, id):
108117
"""Retrieve the information of a specific HLR lookup."""
109118
return HLR().load(self.request('hlr/' + str(id)))

messagebird/http_client.py

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ def request(self, path, method='GET', params=None, format=ResponseFormat.text):
2626
"""Builds a request and gets a response."""
2727
if params is None: params = {}
2828
url = urljoin(self.endpoint, path)
29-
3029
headers = {
3130
'Accept': 'application/json',
3231
'Authorization': 'AccessKey ' + self.access_key,

messagebird/webhook.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from messagebird.base import Base
2+
3+
4+
class Webhook(Base):
5+
6+
def __init__(self):
7+
self.url = None
8+
self.token = None

tests/test_call.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import unittest
2+
from messagebird import Client, ErrorException
3+
4+
try:
5+
from unittest.mock import Mock
6+
except ImportError:
7+
# mock was added to unittest in Python 3.3, but was an external library
8+
# before.
9+
from mock import Mock
10+
11+
12+
class TestCall(unittest.TestCase):
13+
14+
def test_call(self):
15+
http_client = Mock()
16+
http_client.request.return_value = '{"data":[{"id":"call-id","status":"ended","source":"16479311111","destination":"1416555555","createdAt":"2019-08-06T13:17:06Z","updatedAt":"2019-08-06T13:17:39Z","endedAt":"2019-08-06T13:17:39Z"}],"_links":{"legs":"/calls/66bd9f08-a8af-40fe-a830-652d8dabc057/legs","self":"/calls/66bd9f08-a8af-40fe-a830-652d8bca357"},"pagination":{"totalCount":0,"pageCount":0,"currentPage":0,"perPage":0}}'
17+
18+
call = Client('', http_client).call('call-id')
19+
20+
http_client.request.assert_called_once_with('calls/call-id', 'GET', None)
21+
22+
self.assertEqual('ended', call.data.status)

0 commit comments

Comments
 (0)