Port sendgrid to fetch_url

This commit is contained in:
Toshio Kuratomi 2015-07-20 23:33:05 -07:00 committed by Matt Clay
parent 8be4ffd07c
commit 91ed01d73e

View file

@ -85,9 +85,6 @@ EXAMPLES = '''
# sendgrid module support methods
#
import urllib
import urllib2
import base64
def post_sendgrid_api(module, username, password, from_address, to_addresses,
subject, body):
@ -102,11 +99,11 @@ def post_sendgrid_api(module, username, password, from_address, to_addresses,
recipient = recipient.encode('utf-8')
to_addresses_api += '&to[]=%s' % recipient
encoded_data += to_addresses_api
request = urllib2.Request(SENDGRID_URI)
request.add_header('User-Agent', AGENT)
request.add_header('Content-type', 'application/x-www-form-urlencoded')
request.add_header('Accept', 'application/json')
return urllib2.urlopen(request, encoded_data)
headers = { 'User-Agent': AGENT,
'Content-type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'}
return fetch_url(module, SENDGRID_URI, data=encoded_data, headers=headers, method='POST')
# =======================================
@ -133,14 +130,16 @@ def main():
subject = module.params['subject']
body = module.params['body']
try:
response = post_sendgrid_api(module, username, password,
response, info = post_sendgrid_api(module, username, password,
from_address, to_addresses, subject, body)
except Exception:
module.fail_json(msg="unable to send email through SendGrid API")
if info['status'] != 200:
module.fail_json(msg="unable to send email through SendGrid API: %s" % info['msg'])
module.exit_json(msg=subject, changed=False)
# import module snippets
from ansible.module_utils.basic import *
main()
from ansible.module_utils.urls import *
if __name__ == '__main__':
main()