Switch from httplib to fetch_url

This commit is contained in:
Toshio Kuratomi 2015-07-28 12:23:54 -07:00
parent 0297a7e7e6
commit fe69c0e67a

View file

@ -57,24 +57,36 @@ EXAMPLES = '''
'''
import urllib
import httplib
class pushover(object):
class Pushover(object):
''' Instantiates a pushover object, use it to send notifications '''
base_uri = 'https://api.pushover.net'
port = 443
def __init__(self):
self.host, self.port = 'api.pushover.net', 443
def __init__(self, module, user, token):
self.module = module
self.user = user
self.token = token
def run(self):
def run(self, priority, msg):
''' Do, whatever it is, we do. '''
url = '%s:%s/1/messages.json' % (self.base_uri, self.port)
# parse config
conn = httplib.HTTPSConnection(self.host, self.port)
conn.request("POST", "/1/messages.json",
urllib.urlencode(self.options),
{"Content-type": "application/x-www-form-urlencoded"})
conn.getresponse()
return
options = dict(user=self.user,
token=self.token,
priority=priority,
message=msg)
data = urllib.urlencode(options)
headers = { "Content-type": "application/x-www-form-urlencoded"}
r, info = fetch_url(self.module, url, method='POST', data=data, headers=headers)
if info['status'] != 200:
raise Exception(info)
return r.read()
def main():
@ -88,14 +100,9 @@ def main():
),
)
msg_object = pushover()
msg_object.options = {}
msg_object.options['user'] = module.params['user_key']
msg_object.options['token'] = module.params['app_token']
msg_object.options['priority'] = module.params['pri']
msg_object.options['message'] = module.params['msg']
msg_object = Pushover(module, module.params['user_key'], module.params['app_token'])
try:
msg_object.run()
msg_object.run(module.params['pri'], module.params['msg'])
except:
module.fail_json(msg='Unable to send msg via pushover')
@ -103,4 +110,6 @@ def main():
# import module snippets
from ansible.module_utils.basic import *
main()
from ansible.module_utils.urls import *
if __name__ == '__main__':
main()