8000 Add programmable voice grant by jingming · Pull Request #279 · twilio/twilio-python · GitHub
[go: up one dir, main page]

Skip to content

Add programmable voice grant #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in< 8000 /a> to your account

Merged
merged 3 commits into from
Sep 1, 2016
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
2 changes: 1 addition & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ mock==0.8.0
nose
coverage
nosexcover
flake8
flake8==3.0.3
mccabe
wheel>=0.22.0
27 changes: 26 additions & 1 deletion tests/test_access_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datetime import datetime
from nose.tools import assert_equal
from twilio.jwt import decode
from twilio.access_token import AccessToken, ConversationsGrant, IpMessagingGrant
from twilio.access_token import AccessToken, ConversationsGrant, IpMessagingGrant, VoiceGrant

ACCOUNT_SID = 'AC123'
SIGNING_KEY_SID = 'SK123'
Expand Down Expand Up @@ -105,3 +105,28 @@ def test_grants(self):
assert_equal(2, len(payload['grants']))
assert_equal({}, payload['grants']['rtc'])
assert_equal({}, payload['grants']['ip_messaging'])

def test_programmable_voice_grant(self):
grant = VoiceGrant(
outgoing_application_sid='AP123',
outgoing_application_params={
'foo': 'bar'
}
)

scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret')
scat.add_grant(grant)

token = str(scat)
assert_is_not_none(token)
payload = decode(token, 'secret')
self._validate_claims(payload)
assert_equal(1, len(payload['grants']))
assert_equal({
'outgoing': {
'application_sid': 'AP123',
'params': {
'foo': 'bar'
}
}
}, payload['grants']['voice'])
39 changes: 39 additions & 0 deletions twilio/access_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,45 @@ def to_payload(self):
return grant


class VoiceGrant(object):
""" Grant to access Twilio Programmable Voice"""
def __init__(self,
outgoing_application_sid=None,
outgoing_application_params=None,
push_credential_sid=None,
endpoint_id=None):
self.outgoing_application_sid = outgoing_application_sid
""" :type : str """
self.outgoing_application_params = outgoing_application_params
""" :type : dict """
self.push_credential_sid = push_credential_sid
""" :type : str """
self.endpoint_id = endpoint_id
""" :type : str """

@property
def key(self):
return "voice"

def to_payload(self):
grant = {}
if self.outgoing_application_sid:
grant['outgoing'] = {}
grant['outgoing']['application_sid'] = \
self.outgoing_application_sid

if self.outgoing_application_params:
grant['outgoing']['params'] = self.outgoing_application_params

if self.push_credential_sid:
grant['push_credential_sid'] = self.push_credential_sid

if self.endpoint_id:
grant['endpoint_id'] = self.endpoint_id

return grant


class AccessToken(object):
""" Access Token used to access Twilio Resources """
def __init__(self, account_sid, signing_key_sid, secret,
Expand Down
0