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

Fix python snippets linter issues #955

Merged
merged 11 commits into from
Jan 6, 2022
2 changes: 1 addition & 1 deletion .github/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ runs:
gem install json colorize nokogiri rubocop
sudo cp twilio-api-faker/keystore/twilio_fake.pem /usr/local/share/ca-certificates/twilio_fake.crt
sudo update-ca-certificates
shell: bash
shell: bash
2 changes: 1 addition & 1 deletion .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- master
pull_request:
branches:
- master
- '*'

env:
FAKE_CERT_PATH: ~/build/TwilioDevEd/api-snippets/twilio-api-faker/keystore/twilio_fake.pem
Expand Down
6 changes: 4 additions & 2 deletions deployed-devices/quickstarts/paho-python/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
client = mqtt.Client(client_id="bob", clean_session=False)

#
# Initialize TLS, specify trusted CA roots, client certifiate and key file locations.
# You may need to adjust the location of trusted Certificate Authorities depending on your OS.
# Initialize TLS, specify trusted CA roots, client certifiate and key
# file locations.
# You may need to adjust the location of trusted Certificate
# Authorities depending on your OS.
# Typical locations are /etc/ssl/cert.pem and /usr/local/ssl/cert.pem
#
client.tls_set("/usr/local/ssl/cert.pem",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@


#
# Initialize TLS, specify trusted CA roots, client certifiate and key file locations.
# You may need to adjust the location of trusted Certificate Authorities depending on your OS.
# Initialize TLS, specify trusted CA roots, client certifiate and key
# file locations.
# You may need to adjust the location of trusted Certificate
# Authorities depending on your OS.
# Typical locations are /etc/ssl/cert.pem and /usr/local/ssl/cert.pem
#
client.tls_set("/usr/local/ssl/cert.pem",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

credential = client.chat \
.credentials("CRXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") \
.update(friendly_name="MyCredential", api_key=os.environ['TWILIO_API_KEY_SECRET'])
.update(friendly_name="MyCredential",
api_key=os.environ['TWILIO_API_KEY_SECRET'])

print(credential.friendly_name)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@


# Create the hello_world task
# Replace 'UAXXX...' with your Assistant's unique SID https://www.twilio.com/console/autopilot/list
# Replace 'UAXXX...' with your Assistant's unique SID
# https://www.twilio.com/console/autopilot/list
task = client.autopilot.assistants('UAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
.tasks \
.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
'Hi there'
]

# Replace 'UAXXX...' with your Assistant's unique SID https://www.twilio.com/console/autopilot/list
# Replace 'UAXXX...' with your Assistant's unique SID
# https://www.twilio.com/console/autopilot/list
for phrase in phrases:
sample = client.autopilot \
.assistants('UAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
'I\'d like to hear a punny joke'
]

# Replace 'UAXXX...' with your Assistant's unique SID https://www.twilio.com/console/autopilot/list
# Replace 'UAXXX...' with your Assistant's unique SID
# https://www.twilio.com/console/autopilot/list
# Replace 'UDXXX...' with the SID for the task you just created.
for phrase in phrases:
sample = client.autopilot \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@
# Provide actions for the new task
joke_actions = {
'actions': [
{'say': 'I was going to look for my missing watch, but I could never find the time.'}
{
'say':
'I was going to look for my missing '
'watch, but I could never find the time.'
}
]
}

# Create a new task named 'tell_a_joke'
# Replace 'UAXXX...' with your Assistant's unique SID https://www.twilio.com/console/autopilot/list
# Replace 'UAXXX...' with your Assistant's unique SID
# https://www.twilio.com/console/autopilot/list
task = client.autopilot \
.assistants('UAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
.tasks \
Expand Down
3 changes: 2 additions & 1 deletion quickstart/python/autopilot/query-task/query_task.7.x.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
auth_token = os.environ['TWILIO_AUTH_TOKEN']
client = Client(account_sid, auth_token)

# Replace 'UAXXX...' with your Assistant's unique SID https://www.twilio.com/console/autopilot/list
# Replace 'UAXXX...' with your Assistant's unique SID
# https://www.twilio.com/console/autopilot/list
query = client.preview.understand \
.assistants('UAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
.queries \
Expand Down
2 changes: 1 addition & 1 deletion quickstart/python/sms/example-8/reply_to_message.7.x.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# /usr/bin/env python
# Download the twilio-python library from twilio.com/docs/libraries/python
from flask import Flask, request
from flask import Flask
from twilio.twiml.messaging_response import MessagingResponse

app = Flask(__name__)
Expand Down
19 changes: 12 additions & 7 deletions quickstart/python/understand/example-2/example-2.7.x.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@
# Provide actions for the new task
joke_actions = {
'actions': [
{'say': 'I was going to look for my missing watch, but I could never find the time.'}
{
'say':
'I was going to look for my missing '
'watch, but I could never find the time.'
}
]
}

# Create a new task named 'tell_a_joke'
# Replace 'UAXXX...' with your Assistant's unique SID https://www.twilio.com/console/autopilot/list
# Replace 'UAXXX...' with your Assistant's unique SID
# https://www.twilio.com/console/autopilot/list
task = client.preview.understand \
.assistants('UAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
.tasks \
.create(
unique_name='tell-a-joke',
actions=joke_actions)
.assistants('UAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
.tasks \
.create(
unique_name='tell-a-joke',
actions=joke_actions)

print(task.sid)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import os
from twilio.rest import Client

# Your Account Sid and Auth Token can be found at https://www.twilio.com/console
# Your Account Sid and Auth Token can be found
# at https://www.twilio.com/console
# To set up environmental variables, see http://twil.io/secure
account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']
Expand Down
6 changes: 3 additions & 3 deletions rest/access-tokens/live-example/live-example.7.x.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
# via the REST API
client = Client(api_key, api_secret, account_sid)
playback_grant = client.media \
.player_streamer('VJXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
.playback_grant() \
.create(ttl=60)
.player_streamer('VJXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX') \
.playback_grant() \
.create(ttl=60)


# Wrap the grant you received from the API
Expand Down
2 changes: 1 addition & 1 deletion rest/access-tokens/sync-example/sync-example.7.x.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
account_sid = os.environ['TWILIO_ACCOUNT_SID']
api_key = os.environ['TWILIO_API_KEY']
api_secret = os.environ['TWILIO_API_KEY_SECRET']
twilio_sync_service = os.environ[process.env.['TWILIO_SYNC_SERVICE_SID']
twilio_sync_service = os.environ.process.env['TWILIO_SYNC_SERVICE_SID']

# required for Sync grant
identity = 'user'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Flask, request, redirect
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse

app = Flask(__name__)
Expand Down
2 changes: 1 addition & 1 deletion rest/messages/generate-twiml-sms/generate-twiml-sms.7.x.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Flask, request, redirect
from flask import Flask
from twilio.twiml.messaging_response import MessagingResponse

app = Flask(__name__)
Expand Down
6 changes: 3 additions & 3 deletions rest/sip-in/map-list-domain/map-list-domain.7.x.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
client = Client(account_sid, auth_token)

ip_access_control_list_mapping = client.sip \
.domains("SD32a3c49700934481addd5ce1659f04d2") \
.ip_access_control_list_mappings \
.create("AL95a47094615fe05b7c17e62a7877836c")
.domains("SD32a3c49700934481addd5ce1659f04d2") \
.ip_access_control_list_mappings \
.create("AL95a47094615fe05b7c17e62a7877836c")

print(ip_access_control_list_mapping.sid)
5 changes: 3 additions & 2 deletions rest/taskrouter/reservations/conference/conference.7.x.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
instruction='conference',
from_='+18001231234',
conference_status_callback='https://www.example.com/ConferenceEvents',
conference_status_callback_event=["start", "end", "join", "leave", "mute", "hold"]
)
conference_status_callback_event=[
"start", "end", "join", "leave", "mute", "hold"]
)

print(reservation.reservation_status)
print(reservation.worker_name)
8 changes: 4 additions & 4 deletions rest/taskrouter/reservations/redirect/redirect.7.x.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

# redirect using a reservation
reservation = client.taskrouter.workspaces(workspace_sid) \
.tasks(task_sid).reservations(reservation_sid) \
.update(instruction='redirect',
redirect_call_sid='CA123456789',
redirect_url='http://example.com/assignment_redirect')
.tasks(task_sid).reservations(reservation_sid) \
.update(instruction='redirect',
redirect_call_sid='CA123456789',
redirect_url='http://example.com/assignment_redirect')

print(reservation.reservation_status)
print(reservation.worker_name)
6 changes: 2 additions & 4 deletions rest/taskrouter/statistics/workers/realtime/example-1.7.x.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@
statistics = client.taskrouter.workspaces(workspace_sid) \
.workers.real_time_statistics().fetch()

print(
statistics.activity_statistics[0]['workers'] + " in " +
statistics.activity_statistics[0]['friendly_name']
)
print(f'{statistics.activity_statistics[0]["workers"]} '
f'in {statistics.activity_statistics[0]["friendly_name"]}')
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

client = Client(account_sid, auth_token)

task_channel = client.taskrouter.workspaces(workspace_sid).task_channels(task_channel_sid)
task_channel = client.taskrouter.workspaces(workspace_sid).task_channels(
task_channel_sid
)

task_channel.update(friendly_name: "My Channel")
task_channel.update({"friendly_name": "My Channel"})

print(task_channel.friendly_name)
print(task_channel.unique_name)
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
# Download the twilio-python library from twilio.com/docs/python/install
from twilio.request_validator import RequestValidator
from requests.auth import HTTPDigestAuth
from requests.auth import HTTPBasicAuth
import requests
import urllib
import os

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

# Replace this URL with your unique URL
url = 'https://mycompany.com/myapp'
# User credentials if required by your web server. Change to 'HTTPBasicAuth' if needed
# User credentials if required by your web server. Change to
# 'HTTPBasicAuth' if needed
auth = HTTPDigestAuth('username', 'password')

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

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


test_url('GET', url, params, True)
test_url('GET', url, params, False)
test_url('POST', url, params, True)
test_url('POST', url, params, False)
test_url('POST', url, params, False)
15 changes: 15 additions & 0 deletions tools/snippet-testing/language_handler/python.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ def execute_command(file)
execute_with_suppressed_output(command, file)
end

def text_with_custom_header(file_content)
cert_path = ENV['FAKE_CERT_PATH']
file_content.prepend(
"import twilio.rest.resources.base\n"\
"import sys\n"\
"twilio.rest.resources.base.get_cert_file = lambda: '#{cert_path}'\n"\
"sys.modules['twilio.rest.base.resources'] = twilio.rest.resources.base\n"
)
end
def replace_twilio_client_initialization(file_content)
cert_path = ENV['FAKE_CERT_PATH']
file_content.gsub! 'Client(account_sid, auth_token)',
'Client(account_sid, auth_token, http_client=FakerHttpClient())' || file_content
end

def text_with_specific_replacements(file_content)
text_with_custom_header(file_content)
replace_twilio_client_initialization(file_content)
Expand Down
4 changes: 2 additions & 2 deletions video/rest/compositionhooks/delete-hook/delete-hook.6.x.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
client = Client(api_key_sid, api_key_secret)

did_delete = client.video\
.compositionHooks('HKXXXX')\
.delete()
.compositionHooks('HKXXXX')\
.delete()

if(did_delete):
print('Composition removed')
4 changes: 2 additions & 2 deletions video/rest/compositionhooks/get-hook/get-hook.6.x.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
client = Client(api_key_sid, api_key_secret)

compositionHook = client.video\
.compositionHooks('HKXXXX')\
.fetch()
.compositionHooks('HKXXXX')\
.fetch()

print('Read Composition Hook with SID=%s' % (compositionHook.sid))
4 changes: 2 additions & 2 deletions video/rest/compositionhooks/list-hooks/list-hooks.6.x.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
client = Client(api_key_sid, api_key_secret)

hooks = client.video\
.compositionHooks\
.list(enabled=True)
.compositionHooks\
.list(enabled=True)

for hook in hooks:
print('Read Composition Hook with SID=%s' % (hook.sid))
1 change: 0 additions & 1 deletion video/rest/compositions/compose-chess/compose-chess.6.x.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Download the Python helper library from twilio.com/docs/python/install
import os
from twilio.rest import Client
import json

# Find your credentials at twilio.com/console
# To set up environmental variables, see http://twil.io/secure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Download the Python helper library from twilio.com/docs/python/install
import os
from twilio.rest import Client
import json

# Find your credentials at twilio.com/console
# To set up environmental variables, see http://twil.io/secure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Download the Python helper library from twilio.com/docs/python/install
import os
from twilio.rest import Client
import json

# Find your credentials at twilio.com/console
# To set up environmental variables, see http://twil.io/secure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Download the Python helper library from twilio.com/docs/python/install
import os
from twilio.rest import Client
import json

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