Skip to content

Commit aff9659

Browse files
committed
Add some example usage of new library
1 parent 7b9bf77 commit aff9659

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

examples/basic_usage.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
3+
from twilio.twiml import Response
4+
5+
from twilio.rest import Client
6+
7+
ACCOUNT_SID = os.environ.get('TWILIO_ACCOUNT_SID')
8+
AUTH_TOKEN = os.environ.get('TWILIO_AUTH_TOKEN')
9+
10+
11+
def example():
12+
"""
13+
Some example usage of different twilio resources.
14+
"""
15+
client = Client(ACCOUNT_SID, AUTH_TOKEN)
16+
17+
print('Get all the messages...')
18+
all_messages = client.messages.list()
19+
print('There are {} messages in your account.'.format(len(all_messages)))
20+
21+
print('Get only last 10 messages...')
22+
some_messages = client.messages.list(limit=10)
23+
24+
print('Get messages in smaller pages...')
25+
some_messages = client.messages.list(page_size=10)
26+
27+
print('Sending a message...')
28+
new_message = client.messages.create(to='XXXX', from_='YYYY', body='Twilio rocks!')
29+
30+
print('Making a call...')
31+
new_call = client.calls.create(to='XXXX', from_='YYYY', method='GET')
32+
33+
print('Serving TwiML')
34+
twiml_response = Response()
35+
twiml_response.say('Hello!')
36+
twiml_response.hangup()
37+
twiml_xml = twiml_response.toxml()
38+
print('Generated twiml: {}'.format(twiml_xml))
39+
40+
41+
if __name__ == '__main__':
42+
example()

examples/client_validation.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import os
2+
3+
from cryptography.hazmat.backends import default_backend
4+
from cryptography.hazmat.primitives.asymmetric import rsa
5+
from cryptography.hazmat.primitives.serialization import (
6+
Encoding,
7+
PublicFormat,
8+
PrivateFormat,
9+
NoEncryption
10+
)
11+
12+
from twilio.http.validation_client import ValidationClient
13+
from twilio.rest import Client
14+
15+
16+
ACCOUNT_SID = os.environ.get('TWILIO_ACCOUNT_SID')
17+
AUTH_TOKEN = os.environ.get('TWILIO_AUTH_TOKEN')
18+
19+
def example():
20+
"""
21+
Example of using the ValidationClient for signed requests to Twilio.
22+
This is only available to enterprise customers.
23+
24+
This will walkthrough creating an API Key, generating an RSA keypair, setting up a
25+
ValidationClient with these values and making requests with the client.
26+
"""
27+
client = Client(ACCOUNT_SID, AUTH_TOKEN)
28+
29+
# Using Client Validation requires using API Keys for auth
30+
# First create an API key using the standard account sid, auth token client
31+
print('Creating new api key...')
32+
api_key = client.new_keys.create(friendly_name='ClientValidationApiKey')
33+
34+
# Generate a new RSA Keypair
35+
print('Generating RSA key pair...')
36+
key_pair = rsa.generate_private_key(
37+
public_exponent=65537,
38+
key_size=2048,
39+
backend=default_backend()
40+
)
41+
public_key = key_pair.public_key().public_bytes(Encoding.PEM, PublicFormat.PKCS1)
42+
private_key = key_pair.private_bytes(Encoding.PEM, PrivateFormat.PKCS8, NoEncryption())
43+
44+
# Register the public key with Twilio
45+
print('Registering public key with Twilio...')
46+
credential = client.accounts.credentials.public_key.create(
47+
public_key,
48+
friendly_name='ClientValidationPublicKey'
49+
)
50+
51+
# Create a new ValidationClient with the keys we created
52+
validation_client = ValidationClient(
53+
ACCOUNT_SID,
54+
api_key.sid,
55+
credential.sid,
56+
private_key
57+
)
58+
59+
# Create a REST Client using the validation_client
60+
client = Client(api_key.sid, api_key.secret, ACCOUNT_SID, http_client=validation_client)
61+
62+
# Use the library as usual
63+
print('Trying out client validation...')
64+
messages = client.messages.list(limit=10)
65+
for m in messages:
66+
print('Message {}'.format(m.sid))
67+
68+
print('Client validation works!')
69+
70+
71+
if __name__ == '__main__':
72+
example()

0 commit comments

Comments
 (0)