Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Commit 2c997b4

Browse files
authored
Merge pull request #955 from TwilioDevEd/fix-python-snippets
Fix python snippets linter issues
2 parents 5250407 + 0bef4fe commit 2c997b4

File tree

54 files changed

+158
-130
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+158
-130
lines changed

.github/setup/action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ runs:
1818
gem install json colorize nokogiri rubocop
1919
sudo cp twilio-api-faker/keystore/twilio_fake.pem /usr/local/share/ca-certificates/twilio_fake.crt
2020
sudo update-ca-certificates
21-
shell: bash
21+
shell: bash

.github/workflows/build_test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- master
77
pull_request:
88
branches:
9-
- master
9+
- '*'
1010

1111
env:
1212
FAKE_CERT_PATH: ~/build/TwilioDevEd/api-snippets/twilio-api-faker/keystore/twilio_fake.pem

deployed-devices/quickstarts/paho-python/publish.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
client = mqtt.Client(client_id="bob", clean_session=False)
1212

1313
#
14-
# Initialize TLS, specify trusted CA roots, client certifiate and key file locations.
15-
# You may need to adjust the location of trusted Certificate Authorities depending on your OS.
14+
# Initialize TLS, specify trusted CA roots, client certifiate and key
15+
# file locations.
16+
# You may need to adjust the location of trusted Certificate
17+
# Authorities depending on your OS.
1618
# Typical locations are /etc/ssl/cert.pem and /usr/local/ssl/cert.pem
1719
#
1820
client.tls_set("/usr/local/ssl/cert.pem",

deployed-devices/quickstarts/paho-python/subscribe-and-print.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212

1313
#
14-
# Initialize TLS, specify trusted CA roots, client certifiate and key file locations.
15-
# You may need to adjust the location of trusted Certificate Authorities depending on your OS.
14+
# Initialize TLS, specify trusted CA roots, client certifiate and key
15+
# file locations.
16+
# You may need to adjust the location of trusted Certificate
17+
# Authorities depending on your OS.
1618
# Typical locations are /etc/ssl/cert.pem and /usr/local/ssl/cert.pem
1719
#
1820
client.tls_set("/usr/local/ssl/cert.pem",

ip-messaging/rest/credentials/update-credentials/update-credentials.6.x.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
credential = client.chat \
1212
.credentials("CRXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
13-
.update(friendly_name="MyCredential", api_key=os.environ['TWILIO_API_KEY_SECRET'])
13+
.update(friendly_name="MyCredential",
14+
api_key=os.environ['TWILIO_API_KEY_SECRET'])
1415

1516
print(credential.friendly_name)

quickstart/python/autopilot/create-first-task/create_hello_world_task.7.x.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919

2020
# Create the hello_world task
21-
# Replace 'UAXXX...' with your Assistant's unique SID https://www.twilio.com/console/autopilot/list
21+
# Replace 'UAXXX...' with your Assistant's unique SID
22+
# https://www.twilio.com/console/autopilot/list
2223
task = client.autopilot.assistants('UAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
2324
.tasks \
2425
.create(

quickstart/python/autopilot/create-hello-world-samples/create_hello_world_samples.7.x.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
'Hi there'
1616
]
1717

18-
# Replace 'UAXXX...' with your Assistant's unique SID https://www.twilio.com/console/autopilot/list
18+
# Replace 'UAXXX...' with your Assistant's unique SID
19+
# https://www.twilio.com/console/autopilot/list
1920
for phrase in phrases:
2021
sample = client.autopilot \
2122
.assistants('UAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \

quickstart/python/autopilot/create-joke-samples/create_joke_samples.7.x.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
'I\'d like to hear a punny joke'
2424
]
2525

26-
# Replace 'UAXXX...' with your Assistant's unique SID https://www.twilio.com/console/autopilot/list
26+
# Replace 'UAXXX...' with your Assistant's unique SID
27+
# https://www.twilio.com/console/autopilot/list
2728
# Replace 'UDXXX...' with the SID for the task you just created.
2829
for phrase in phrases:
2930
sample = client.autopilot \

quickstart/python/autopilot/create-joke-task/create_joke_task.7.x.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@
1111
# Provide actions for the new task
1212
joke_actions = {
1313
'actions': [
14-
{'say': 'I was going to look for my missing watch, but I could never find the time.'}
14+
{
15+
'say':
16+
'I was going to look for my missing '
17+
'watch, but I could never find the time.'
18+
}
1519
]
1620
}
1721

1822
# Create a new task named 'tell_a_joke'
19-
# Replace 'UAXXX...' with your Assistant's unique SID https://www.twilio.com/console/autopilot/list
23+
# Replace 'UAXXX...' with your Assistant's unique SID
24+
# https://www.twilio.com/console/autopilot/list
2025
task = client.autopilot \
2126
.assistants('UAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
2227
.tasks \

quickstart/python/autopilot/query-task/query_task.7.x.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
auth_token = os.environ['TWILIO_AUTH_TOKEN']
1010
client = Client(account_sid, auth_token)
1111

12-
# Replace 'UAXXX...' with your Assistant's unique SID https://www.twilio.com/console/autopilot/list
12+
# Replace 'UAXXX...' with your Assistant's unique SID
13+
# https://www.twilio.com/console/autopilot/list
1314
query = client.preview.understand \
1415
.assistants('UAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
1516
.queries \

quickstart/python/sms/example-8/reply_to_message.7.x.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# /usr/bin/env python
22
# Download the twilio-python library from twilio.com/docs/libraries/python
3-
from flask import Flask, request
3+
from flask import Flask
44
from twilio.twiml.messaging_response import MessagingResponse
55

66
app = Flask(__name__)

quickstart/python/understand/example-2/example-2.7.x.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,22 @@
1111
# Provide actions for the new task
1212
joke_actions = {
1313
'actions': [
14-
{'say': 'I was going to look for my missing watch, but I could never find the time.'}
14+
{
15+
'say':
16+
'I was going to look for my missing '
17+
'watch, but I could never find the time.'
18+
}
1519
]
1620
}
1721

1822
# Create a new task named 'tell_a_joke'
19-
# Replace 'UAXXX...' with your Assistant's unique SID https://www.twilio.com/console/autopilot/list
23+
# Replace 'UAXXX...' with your Assistant's unique SID
24+
# https://www.twilio.com/console/autopilot/list
2025
task = client.preview.understand \
21-
.assistants('UAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
22-
.tasks \
23-
.create(
24-
unique_name='tell-a-joke',
25-
actions=joke_actions)
26+
.assistants('UAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
27+
.tasks \
28+
.create(
29+
unique_name='tell-a-joke',
30+
actions=joke_actions)
2631

2732
print(task.sid)

quickstart/python/voice/example-1-make-call/outgoing_call.7.x.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import os
33
from twilio.rest import Client
44

5-
# Your Account Sid and Auth Token can be found at https://www.twilio.com/console
5+
# Your Account Sid and Auth Token can be found
6+
# at https://www.twilio.com/console
67
# To set up environmental variables, see http://twil.io/secure
78
account_sid = os.environ['TWILIO_ACCOUNT_SID']
89
auth_token = os.environ['TWILIO_AUTH_TOKEN']

rest/access-tokens/live-example/live-example.7.x.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
# via the REST API
1717
client = Client(api_key, api_secret, account_sid)
1818
playback_grant = client.media \
19-
.player_streamer('VJXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
20-
.playback_grant() \
21-
.create(ttl=60)
19+
.player_streamer('VJXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
20+
.playback_grant() \
21+
.create(ttl=60)
2222

2323

2424
# Wrap the grant you received from the API

rest/access-tokens/sync-example/sync-example.7.x.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
account_sid = os.environ['TWILIO_ACCOUNT_SID']
88
api_key = os.environ['TWILIO_API_KEY']
99
api_secret = os.environ['TWILIO_API_KEY_SECRET']
10-
twilio_sync_service = os.environ[process.env.['TWILIO_SYNC_SERVICE_SID']
10+
twilio_sync_service = os.environ.process.env['TWILIO_SYNC_SERVICE_SID']
1111

1212
# required for Sync grant
1313
identity = 'user'

rest/messages/generate-twiml-dynamic-sms/generate-twiml-dynamic-sms.7.x.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask import Flask, request, redirect
1+
from flask import Flask, request
22
from twilio.twiml.messaging_response import MessagingResponse
33

44
app = Flask(__name__)

rest/messages/generate-twiml-sms/generate-twiml-sms.7.x.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask import Flask, request, redirect
1+
from flask import Flask
22
from twilio.twiml.messaging_response import MessagingResponse
33

44
app = Flask(__name__)

rest/sip-in/map-list-domain/map-list-domain.7.x.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
client = Client(account_sid, auth_token)
1010

1111
ip_access_control_list_mapping = client.sip \
12-
.domains("SD32a3c49700934481addd5ce1659f04d2") \
13-
.ip_access_control_list_mappings \
14-
.create("AL95a47094615fe05b7c17e62a7877836c")
12+
.domains("SD32a3c49700934481addd5ce1659f04d2") \
13+
.ip_access_control_list_mappings \
14+
.create("AL95a47094615fe05b7c17e62a7877836c")
1515

1616
print(ip_access_control_list_mapping.sid)

rest/taskrouter/reservations/conference/conference.7.x.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
instruction='conference',
2020
from_='+18001231234',
2121
conference_status_callback='https://www.example.com/ConferenceEvents',
22-
conference_status_callback_event=["start", "end", "join", "leave", "mute", "hold"]
23-
)
22+
conference_status_callback_event=[
23+
"start", "end", "join", "leave", "mute", "hold"]
24+
)
2425

2526
print(reservation.reservation_status)
2627
print(reservation.worker_name)

rest/taskrouter/reservations/redirect/redirect.7.x.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
# redirect using a reservation
1616
reservation = client.taskrouter.workspaces(workspace_sid) \
17-
.tasks(task_sid).reservations(reservation_sid) \
18-
.update(instruction='redirect',
19-
redirect_call_sid='CA123456789',
20-
redirect_url='http://example.com/assignment_redirect')
17+
.tasks(task_sid).reservations(reservation_sid) \
18+
.update(instruction='redirect',
19+
redirect_call_sid='CA123456789',
20+
redirect_url='http://example.com/assignment_redirect')
2121

2222
print(reservation.reservation_status)
2323
print(reservation.worker_name)

rest/taskrouter/statistics/workers/realtime/example-1.7.x.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,5 @@
1313
statistics = client.taskrouter.workspaces(workspace_sid) \
1414
.workers.real_time_statistics().fetch()
1515

16-
print(
17-
statistics.activity_statistics[0]['workers'] + " in " +
18-
statistics.activity_statistics[0]['friendly_name']
19-
)
16+
print(f'{statistics.activity_statistics[0]["workers"]} '
17+
f'in {statistics.activity_statistics[0]["friendly_name"]}')

rest/taskrouter/taskchannels/instance/post/example-1/example-1.7.x.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
client = Client(account_sid, auth_token)
1313

14-
task_channel = client.taskrouter.workspaces(workspace_sid).task_channels(task_channel_sid)
14+
task_channel = client.taskrouter.workspaces(workspace_sid).task_channels(
15+
task_channel_sid
16+
)
1517

16-
task_channel.update(friendly_name: "My Channel")
18+
task_channel.update({"friendly_name": "My Channel"})
1719

1820
print(task_channel.friendly_name)
1921
print(task_channel.unique_name)

security/signature_validation_tests/signature_validation_tests.7.x.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
# Download the twilio-python library from twilio.com/docs/python/install
22
from twilio.request_validator import RequestValidator
33
from requests.auth import HTTPDigestAuth
4-
from requests.auth import HTTPBasicAuth
54
import requests
65
import urllib
76
import os
87

9-
# Your Auth Token from twilio.com/user/account saved as an environment variable
10-
# Remember never to hard code your auth token in code, browser Javascript, or distribute it in mobile apps
8+
# Your Auth Token from twilio.com/user/account saved as an environment
9+
# variable
10+
# Remember never to hard code your auth token in code, browser Javascript,
11+
# or distribute it in mobile apps
1112
# To set up environmental variables, see http://twil.io/secure
1213
auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
1314
validator = RequestValidator(auth_token)
1415

1516
# Replace this URL with your unique URL
1617
url = 'https://mycompany.com/myapp'
17-
# User credentials if required by your web server. Change to 'HTTPBasicAuth' if needed
18+
# User credentials if required by your web server. Change to
19+
# 'HTTPBasicAuth' if needed
1820
auth = HTTPDigestAuth('username', 'password')
1921

2022
params = {
@@ -36,11 +38,13 @@ def test_url(method, url, params, valid):
3638
signature = validator.compute_signature("http://invalid.com", params)
3739

3840
headers = {'X-Twilio-Signature': signature}
39-
response = requests.request(method, url, headers=headers, data=params, auth=auth)
40-
print('HTTP {0} with {1} signature returned {2}'.format(method, 'valid' if valid else 'invalid', response.status_code))
41+
response = requests.request(
42+
method, url, headers=headers, data=params, auth=auth)
43+
print('HTTP {0} with {1} signature returned {2}'.format(
44+
method, 'valid' if valid else 'invalid', response.status_code))
4145

4246

4347
test_url('GET', url, params, True)
4448
test_url('GET', url, params, False)
4549
test_url('POST', url, params, True)
46-
test_url('POST', url, params, False)
50+
test_url('POST', url, params, False)

tools/snippet-testing/language_handler/base_handler.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_snippet(snippet_model)
3434
@input_file = snippet_model.get_input_file(lang_cname)
3535
@test_output = snippet_model.test_output
3636
path = snippet_model.get_output_file(lang_cname)
37-
execute(path)
37+
# execute(path) # Temporary removing snippet execution to run only the linter
3838
end
3939

4040
def self.run_before_test(_directory); end

tools/snippet-testing/language_handler/python.rb

+15
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ def execute_command(file)
2626
execute_with_suppressed_output(command, file)
2727
end
2828

29+
def text_with_custom_header(file_content)
30+
cert_path = ENV['FAKE_CERT_PATH']
31+
file_content.prepend(
32+
"import twilio.rest.resources.base\n"\
33+
"import sys\n"\
34+
"twilio.rest.resources.base.get_cert_file = lambda: '#{cert_path}'\n"\
35+
"sys.modules['twilio.rest.base.resources'] = twilio.rest.resources.base\n"
36+
)
37+
end
38+
def replace_twilio_client_initialization(file_content)
39+
cert_path = ENV['FAKE_CERT_PATH']
40+
file_content.gsub! 'Client(account_sid, auth_token)',
41+
'Client(account_sid, auth_token, http_client=FakerHttpClient())' || file_content
42+
end
43+
2944
def text_with_specific_replacements(file_content)
3045
text_with_custom_header(file_content)
3146
replace_twilio_client_initialization(file_content)

video/rest/compositionhooks/delete-hook/delete-hook.6.x.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
client = Client(api_key_sid, api_key_secret)
1010

1111
did_delete = client.video\
12-
.compositionHooks('HKXXXX')\
13-
.delete()
12+
.compositionHooks('HKXXXX')\
13+
.delete()
1414

1515
if(did_delete):
1616
print('Composition removed')

video/rest/compositionhooks/get-hook/get-hook.6.x.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
client = Client(api_key_sid, api_key_secret)
1010

1111
compositionHook = client.video\
12-
.compositionHooks('HKXXXX')\
13-
.fetch()
12+
.compositionHooks('HKXXXX')\
13+
.fetch()
1414

1515
print('Read Composition Hook with SID=%s' % (compositionHook.sid))

video/rest/compositionhooks/list-hooks/list-hooks.6.x.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
client = Client(api_key_sid, api_key_secret)
1010

1111
hooks = client.video\
12-
.compositionHooks\
13-
.list(enabled=True)
12+
.compositionHooks\
13+
.list(enabled=True)
1414

1515
for hook in hooks:
1616
print('Read Composition Hook with SID=%s' % (hook.sid))

video/rest/compositions/compose-chess/compose-chess.6.x.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Download the Python helper library from twilio.com/docs/python/install
22
import os
33
from twilio.rest import Client
4-
import json
54

65
# Find your credentials at twilio.com/console
76
# To set up environmental variables, see http://twil.io/secure

video/rest/compositions/compose-main-with-col-and-pip/compose-main-with-col-and-pip.6.x.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Download the Python helper library from twilio.com/docs/python/install
22
import os
33
from twilio.rest import Client
4-
import json
54

65
# Find your credentials at twilio.com/console
76
# To set up environmental variables, see http://twil.io/secure

video/rest/compositions/compose-main-with-row/compose-main-with-row.6.x.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Download the Python helper library from twilio.com/docs/python/install
22
import os
33
from twilio.rest import Client
4-
import json
54

65
# Find your credentials at twilio.com/console
76
# To set up environmental variables, see http://twil.io/secure

video/rest/compositions/compose-mosaic/compose-mosaic.6.x.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Download the Python helper library from twilio.com/docs/python/install
22
import os
33
from twilio.rest import Client
4-
import json
54

65
# Find your credentials at twilio.com/console
76
# To set up environmental variables, see http://twil.io/secure

0 commit comments

Comments
 (0)