Port sendgrid to fetch_url

reviewable/pr18780/r1
Toshio Kuratomi 9 years ago
parent 79173ac18d
commit b8df0d32a2

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

Loading…
Cancel
Save