Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions examples/voice_messages_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python

import argparse
import messagebird
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))


parser = argparse.ArgumentParser()
parser.add_argument(
'--accessKey', help='access key for MessageBird API', type=str, required=True)

args = vars(parser.parse_args())

try:
client = messagebird.Client(args['accessKey'])

# Fetch Voice Messages List from a specific offset, and within a defined limit.
voiceMessageList = client.voice_message_list(limit=10, offset=0)

# Print the object information.
print('The following information was returned as a Voice Messages List object:')
print(' Containing the following items:')
print(voiceMessageList)
for item in voiceMessageList.items:
print(' {')
print(' id : %s' % item.id)
print(' href : %s' % item.href)
print(' originator : %s' % item.originator)
print(' body : %s' % item.body)
print(' reference : %s' % item.reference)
print(' language : %s' % item.language)
print(' voice : %s' % item.voice)
print(' repeat : %s' % item.repeat)
print(' ifMachine : %s' % item.ifMachine)
print(' scheduledDatetime : %s' % item.scheduledDatetime)
print(' createdDatetime : %s' % item.createdDatetime)
print(' recipients : %s\n' % item.recipients)
print(' },')


except messagebird.client.ErrorException as e:
print('An error occured while requesting a Voice messages object list object:')

for error in e.errors:
print(' code : %d' % error.code)
print(' description : %s' % error.description)
print(' parameter : %s\n' % error.parameter)
7 changes: 6 additions & 1 deletion messagebird/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from messagebird.message import Message, MessageList
from messagebird.mms import MMS
from messagebird.voice_webhook import VoiceWebhook, VoiceWebhookList
from messagebird.voicemessage import VoiceMessage
from messagebird.voicemessage import VoiceMessagesList, VoiceMessage
from messagebird.lookup import Lookup
from messagebird.verify import Verify
from messagebird.http_client import HttpClient, ResponseFormat
Expand Down Expand Up @@ -241,6 +241,11 @@ def voice_message(self, id):
"Retrieve the information of a specific voice message."
return VoiceMessage().load(self.request('voicemessages/' + str(id)))

def voice_message_list(self, limit=10, offset=0):
"Retrieve the information of a list of voice messages."
query = self._format_query(limit, offset)
return VoiceMessagesList().load(self.request('voicemessages?' + query, 'GET', None))

def voice_message_create(self, recipients, body, params=None):
"""Create a new voice message."""
if params is None:
Expand Down
39 changes: 39 additions & 0 deletions messagebird/voicemessage.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@
from messagebird.base import Base
from messagebird.base_list import BaseList
from messagebird.recipient import Recipient


class VoiceMessagesList(BaseList):
def __init__(self):
super(VoiceMessagesList, self).__init__(VoiceMessage)
self.perPage = None
self.currentPage = None
self.pageCount = None
self._pagination = None

@property
def data(self):
return self.items

@property
def pagination(self):
return {
"totalCount": self.totalCount,
"pageCount": self.pageCount,
"currentPage": self.currentPage,
"perPage": self.perPage
}

@pagination.setter
def pagination(self, value):
if isinstance(value, dict):
self.totalCount = value['totalCount']
self.pageCount = value['pageCount']
self.currentPage = value['currentPage']
self.perPage = value['perPage']
self.limit = self.perPage * self.currentPage
self.offset = self.perPage * (self.currentPage - 1)

@data.setter
def data(self, value):
if isinstance(value, list):
self.count = len(value)
self.items = value


class VoiceMessage(Base):
def __init__(self):
self.id = None
Expand Down
32 changes: 30 additions & 2 deletions tests/test_voicemessage.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,40 @@ def test_voicemessage(self):
http_client = Mock()
http_client.request.return_value = '{"body": "Hello World","createdDatetime": "2015-01-05T16:11:24+00:00","href": "https://rest.messagebird.com/voicemessages/voicemessage-id","id": "voicemessage-id","ifMachine": "continue","language": "en-gb","originator": "MessageBird","recipients": {"items": [{"recipient": 31612345678,"status": "calling","statusDatetime": "2015-01-05T16:11:24+00:00"}],"totalCount": 1,"totalDeliveredCount": 0,"totalDeliveryFailedCount": 0,"totalSentCount": 1},"reference": null,"repeat": 1,"scheduledDatetime": null,"voice": "female"}'

voice_message = Client('', http_client).voice_message('voicemessage-id')
voice_message = Client(
'', http_client).voice_message('voicemessage-id')

http_client.request.assert_called_once_with('voicemessages/voicemessage-id', 'GET', None)
http_client.request.assert_called_once_with(
'voicemessages/voicemessage-id', 'GET', None)

self.assertEqual('voicemessage-id', voice_message.id)

def test_voicemessages_list(self):
http_client = Mock()
http_client.request.return_value = '{ "offset": 0, "limit": 10, "count": 2, "totalCount": 2, "links": { "first": "https://rest.messagebird.com/voicemessages/?offset=0&limit=30", "previous": null, "next": null, "last": "https://rest.messagebird.com/voicemessages/?offset=0&limit=30" }, "items": [ { "id": "12345678-9012-3456-7890-123456789012", "href": "https://rest.messagebird.com/voicemessages/12345678-9012-3456-7890-123456789012", "originator": null, "body": "This is a test message.", "reference": null, "language": "en-gb", "voice": "male", "repeat": 1, "ifMachine": "continue", "machineTimeout": 7000, "scheduledDatetime": null, "createdDatetime": "2020-02-04T15:15:30+00:00", "recipients": { "totalCount": 1, "totalSentCount": 1, "totalDeliveredCount": 1, "totalDeliveryFailedCount": 0, "items": [ { "recipient": 31612345678, "originator": null, "status": "answered", "statusDatetime": "2020-02-04T15:15:57+00:00" } ] } }, { "id": "12345678-9012-3456-7890-123456789013", "href": "https://rest.messagebird.com/voicemessages/12345678-9012-3456-7890-123456789013", "originator": null, "body": "The voice message to be sent", "reference": null, "language": "en-gb", "voice": "female", "repeat": 1, "ifMachine": "delay", "machineTimeout": 7000, "scheduledDatetime": null, "createdDatetime": "2020-02-04T12:26:44+00:00", "recipients": { "totalCount": 1, "totalSentCount": 1, "totalDeliveredCount": 1, "totalDeliveryFailedCount": 0, "items": [ { "recipient": 31612345678, "originator": null, "status": "answered", "statusDatetime": "2020-02-04T12:27:32+00:00" } ] } } ] }'

voice_messages = Client('', http_client).voice_message_list()

http_client.request.assert_called_once_with(
'voicemessages?limit=10&offset=0', 'GET', None)

voice_messages_check = {
'12345678-9012-3456-7890-123456789012': {
"id": '12345678-9012-3456-7890-123456789012',
"href": "https://rest.messagebird.com/voicemessages/12345678-9012-3456-7890-123456789012"
},
'12345678-9012-3456-7890-123456789013': {
"id": '12345678-9012-3456-7890-123456789013',
"href": "https://rest.messagebird.com/voicemessages/12345678-9012-3456-7890-123456789013"
}
}

for item in voice_messages.items:
message_specific = voice_messages_check.get(item.id)
self.assertEqual(message_specific['id'], item.id)
self.assertEqual(message_specific['href'], item.href)
self.assertIsInstance(str(voice_messages), str)

def test_voicemessage_create(self):
http_client = Mock()
http_client.request.return_value = '{}'
Expand Down