summaryrefslogtreecommitdiff
path: root/notification
diff options
context:
space:
mode:
authorToshio Kuratomi <a.badger@gmail.com>2015-07-28 10:58:59 -0700
committerToshio Kuratomi <a.badger@gmail.com>2015-07-28 10:58:59 -0700
commit079720a3413dc44c091279bd7b6eb9719bb1e29e (patch)
tree024c346cbe7c8e57eac17fb6fda7b46aae6c3ea1 /notification
parent5a2869bc10062075186f6cafba93771326e7f7e6 (diff)
parent905737c974c928482cda94397a7939cb45e2ae60 (diff)
downloadansible-modules-extras-079720a3413dc44c091279bd7b6eb9719bb1e29e.tar.gz
Merge pull request #738 from ansible/fetch_url-typetalk
Port typetalk to fetch_url
Diffstat (limited to 'notification')
-rw-r--r--notification/typetalk.py31
1 files changed, 20 insertions, 11 deletions
diff --git a/notification/typetalk.py b/notification/typetalk.py
index 002c8b5c..f6b84cb0 100644
--- a/notification/typetalk.py
+++ b/notification/typetalk.py
@@ -35,21 +35,28 @@ EXAMPLES = '''
import urllib
-import urllib2
-
try:
import json
except ImportError:
- json = None
+ try:
+ import simplejson as json
+ except ImportError:
+ json = None
-def do_request(url, params, headers={}):
+def do_request(module, url, params, headers=None):
data = urllib.urlencode(params)
+ if headers is None:
+ headers = dict()
headers = dict(headers, **{
'User-Agent': 'Ansible/typetalk module',
})
- return urllib2.urlopen(urllib2.Request(url, data, headers))
-
+ r, info = fetch_url(module, url, data=data, headers=headers)
+ if info['status'] != 200:
+ exc = ConnectionError(info['msg'])
+ exc.code = info['status']
+ raise exc
+ return r
def get_access_token(client_id, client_secret):
params = {
@@ -62,7 +69,7 @@ def get_access_token(client_id, client_secret):
return json.load(res)['access_token']
-def send_message(client_id, client_secret, topic, msg):
+def send_message(module, client_id, client_secret, topic, msg):
"""
send message to typetalk
"""
@@ -72,9 +79,9 @@ def send_message(client_id, client_secret, topic, msg):
headers = {
'Authorization': 'Bearer %s' % access_token,
}
- do_request(url, {'message': msg}, headers)
+ do_request(module, url, {'message': msg}, headers)
return True, {'access_token': access_token}
- except urllib2.HTTPError, e:
+ except ConnectionError, e:
return False, e
@@ -98,7 +105,7 @@ def main():
topic = module.params["topic"]
msg = module.params["msg"]
- res, error = send_message(client_id, client_secret, topic, msg)
+ res, error = send_message(module, client_id, client_secret, topic, msg)
if not res:
module.fail_json(msg='fail to send message with response code %s' % error.code)
@@ -107,4 +114,6 @@ def main():
# import module snippets
from ansible.module_utils.basic import *
-main()
+from ansible.module_utils.urls import *
+if __name__ == '__main__':
+ main()