Port twilio to fetch_url

This commit is contained in:
Toshio Kuratomi 2015-07-20 23:39:42 -07:00
parent 79173ac18d
commit 4ef6f4ea42

View file

@ -104,10 +104,8 @@ EXAMPLES = '''
# ======================================= # =======================================
# twilio module support methods # twilio module support methods
# #
import urllib
import urllib2
import base64 import base64
import urllib
def post_twilio_api(module, account_sid, auth_token, msg, from_number, def post_twilio_api(module, account_sid, auth_token, msg, from_number,
@ -120,14 +118,16 @@ def post_twilio_api(module, account_sid, auth_token, msg, from_number,
if media_url: if media_url:
data['MediaUrl'] = media_url data['MediaUrl'] = media_url
encoded_data = urllib.urlencode(data) encoded_data = urllib.urlencode(data)
request = urllib2.Request(URI)
base64string = base64.encodestring('%s:%s' % \ base64string = base64.encodestring('%s:%s' % \
(account_sid, auth_token)).replace('\n', '') (account_sid, auth_token)).replace('\n', '')
request.add_header('User-Agent', AGENT)
request.add_header('Content-type', 'application/x-www-form-urlencoded') headers = {'User-Agent': AGENT,
request.add_header('Accept', 'application/json') 'Content-type': 'application/x-www-form-urlencoded',
request.add_header('Authorization', 'Basic %s' % base64string) 'Accept': 'application/json',
return urllib2.urlopen(request, encoded_data) 'Authorization': 'Basic %s' % base64string,
}
return fetch_url(module, URI, data=encoded_data, headers=headers)
# ======================================= # =======================================
@ -159,14 +159,15 @@ def main():
to_number = [to_number] to_number = [to_number]
for number in to_number: for number in to_number:
try: r, info = post_twilio_api(module, account_sid, auth_token, msg,
post_twilio_api(module, account_sid, auth_token, msg,
from_number, number, media_url) from_number, number, media_url)
except Exception: if info['status'] != 200:
module.fail_json(msg="unable to send message to %s" % number) module.fail_json(msg="unable to send message to %s" % number)
module.exit_json(msg=msg, changed=False) module.exit_json(msg=msg, changed=False)
# import module snippets # import module snippets
from ansible.module_utils.basic import * from ansible.module_utils.basic import *
main() from ansible.module_utils.urls import *
if __name__ == '__main__':
main()