diff options
author | Toshio Kuratomi <a.badger@gmail.com> | 2015-07-28 11:24:57 -0700 |
---|---|---|
committer | Toshio Kuratomi <a.badger@gmail.com> | 2015-07-28 11:24:57 -0700 |
commit | 4e5c5fa728735874bc86f7455c011104a26a2f6e (patch) | |
tree | 4be889d8b4ccefa4ca2d6f77c51a5898248ad451 | |
parent | aa1b4caa8a467f4db4046755aa2df609ed8dbc41 (diff) | |
parent | 4ef6f4ea4248d2b16771cfcf72cab0cb4009fbd9 (diff) | |
download | ansible-modules-extras-4e5c5fa728735874bc86f7455c011104a26a2f6e.tar.gz |
Merge pull request #737 from ansible/fetch_url-twilio
Port twilio to fetch_url
-rw-r--r-- | notification/twilio.py | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/notification/twilio.py b/notification/twilio.py index ee12d987..58898b0b 100644 --- a/notification/twilio.py +++ b/notification/twilio.py @@ -104,10 +104,8 @@ EXAMPLES = ''' # ======================================= # twilio module support methods # -import urllib -import urllib2 - import base64 +import urllib 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: data['MediaUrl'] = media_url encoded_data = urllib.urlencode(data) - request = urllib2.Request(URI) + base64string = base64.encodestring('%s:%s' % \ (account_sid, auth_token)).replace('\n', '') - request.add_header('User-Agent', AGENT) - request.add_header('Content-type', 'application/x-www-form-urlencoded') - request.add_header('Accept', 'application/json') - request.add_header('Authorization', 'Basic %s' % base64string) - return urllib2.urlopen(request, encoded_data) + + headers = {'User-Agent': AGENT, + 'Content-type': 'application/x-www-form-urlencoded', + 'Accept': 'application/json', + 'Authorization': 'Basic %s' % base64string, + } + return fetch_url(module, URI, data=encoded_data, headers=headers) # ======================================= @@ -159,14 +159,15 @@ def main(): to_number = [to_number] for number in to_number: - try: - post_twilio_api(module, account_sid, auth_token, msg, + r, info = post_twilio_api(module, account_sid, auth_token, msg, from_number, number, media_url) - except Exception: + if info['status'] != 200: module.fail_json(msg="unable to send message to %s" % number) module.exit_json(msg=msg, changed=False) # import module snippets from ansible.module_utils.basic import * -main() +from ansible.module_utils.urls import * +if __name__ == '__main__': + main() |