8000 Add programmable voice grant · PNPtutorials/twilio-python@6bb14a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6bb14a7

Browse files
committed
Add programmable voice grant
1 parent 22b84cd commit 6bb14a7

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

tests/test_access_token.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from datetime import datetime
55
from nose.tools import assert_equal
66
from twilio.jwt import decode
7-
from twilio.access_token import AccessToken, ConversationsGrant, IpMessagingGrant
7+
from twilio.access_token import AccessToken, ConversationsGrant, IpMessagingGrant, ProgrammableVoiceGrant
88

99
ACCOUNT_SID = 'AC123'
1010
SIGNING_KEY_SID = 'SK123'
@@ -105,3 +105,28 @@ def test_grants(self):
105105
assert_equal(2, len(payload['grants']))
106106
assert_equal({}, payload['grants']['rtc'])
107107
assert_equal({}, payload['grants']['ip_messaging'])
108+
109+
def test_programmable_voice_grant(self):
110+
grant = ProgrammableVoiceGrant(
111+
outgoing_application_sid='AP123',
112+
outgoing_application_params={
113+
'foo': 'bar'
114+
}
115+
)
116+
117+
scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret')
118+
scat.add_grant(grant)
119+
120+
token = str(scat)
121+
assert_is_not_none(token)
122+
payload = decode(token, 'secret')
123+
self._validate_claims(payload)
124+
assert_equal(1, len(payload['grants']))
125+
assert_equal({
126+
'outgoing': {
127+
'application_sid': 'AP123',
128+
'params': {
129+
'foo': 'bar'
130+
}
131+
}
132+
}, payload['grants']['programmable_voice'])

twilio/access_token.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,45 @@ def to_payload(self):
4646
return grant
4747

4848

49+
class ProgrammableVoiceGrant(object):
50+
""" Grant to access Twilio Programmable Voice"""
51+
def __init__(self,
52+
outgoing_application_sid=None,
53+
outgoing_application_params=None,
54+
push_credential_sid=None,
55+
endpoint_id=None):
56+
self.outgoing_application_sid = outgoing_application_sid
57+
""" :type : str """
58+
self.outgoing_application_params = outgoing_application_params
59+
""" :type : dict """
60+
self.push_credential_sid = push_credential_sid
61+
""" :type : str """
62+
self.endpoint_id = endpoint_id
63+
""" :type : str """
64+
65+
@property
66+
def key(self):
67+
return "programmable_voice"
68+
69+
def to_payload(self):
70+
grant = {}
71+
if self.outgoing_application_sid:
72+
grant['outgoing'] = {}
73+
grant['outgoing']['application_sid'] = \
74+
self.outgoing_application_sid
75+
76+
if self.outgoing_application_params:
77+
grant['outgoing']['params'] = self.outgoing_application_params
78+
79+
if self.push_credential_sid:
80+
grant['push_credential_sid'] = self.push_credential_sid
81+
82+
if self.endpoint_id:
83+
grant['endpoint_id'] = self.endpoint_id
84+
85+
return grant
86+
87+
4988
class AccessToken(object):
5089
""" Access Token used to access Twilio Resources """
5190
def __init__(self, account_sid, signing_key_sid, secret,

0 commit comments

Comments
 (0)
0