##// END OF EJS Templates
webhook: add possibility to specify username and password during a call.
marcink -
r2137:a25dc0a4 default
parent child Browse files
Show More
@@ -23,8 +23,10 b' import string'
23 from collections import OrderedDict
23 from collections import OrderedDict
24
24
25 import deform
25 import deform
26 import deform.widget
26 import logging
27 import logging
27 import requests
28 import requests
29 import requests.adapters
28 import colander
30 import colander
29 from celery.task import task
31 from celery.task import task
30 from requests.packages.urllib3.util.retry import Retry
32 from requests.packages.urllib3.util.retry import Retry
@@ -61,6 +63,15 b' WEBHOOK_URL_VARS = ['
61 URL_VARS = ', '.join('${' + x + '}' for x in WEBHOOK_URL_VARS)
63 URL_VARS = ', '.join('${' + x + '}' for x in WEBHOOK_URL_VARS)
62
64
63
65
66 def get_auth(settings):
67 from requests.auth import HTTPBasicAuth
68 username = settings.get('username')
69 password = settings.get('password')
70 if username and password:
71 return HTTPBasicAuth(username, password)
72 return None
73
74
64 class WebhookHandler(object):
75 class WebhookHandler(object):
65 def __init__(self, template_url, secret_token, headers):
76 def __init__(self, template_url, secret_token, headers):
66 self.template_url = template_url
77 self.template_url = template_url
@@ -188,6 +199,27 b' class WebhookSettingsSchema(colander.Sch'
188 placeholder='e.g. secret_token'
199 placeholder='e.g. secret_token'
189 ),
200 ),
190 )
201 )
202 username = colander.SchemaNode(
203 colander.String(),
204 title=_('Username'),
205 description=_('Optional username to authenticate the call.'),
206 default='',
207 missing='',
208 widget=deform.widget.TextInputWidget(
209 placeholder='e.g. admin'
210 ),
211 )
212 password = colander.SchemaNode(
213 colander.String(),
214 title=_('Password'),
215 description=_('Optional password to authenticate the call.'),
216 default='',
217 missing='',
218 widget=deform.widget.PasswordWidget(
219 placeholder='e.g. secret.',
220 redisplay=True,
221 ),
222 )
191 custom_header_key = colander.SchemaNode(
223 custom_header_key = colander.SchemaNode(
192 colander.String(),
224 colander.String(),
193 title=_('Custom Header Key'),
225 title=_('Custom Header Key'),
@@ -269,8 +301,8 b' class WebhookIntegrationType(Integration'
269 template_url = self.settings['url']
301 template_url = self.settings['url']
270
302
271 headers = {}
303 headers = {}
272 head_key = self.settings['custom_header_key']
304 head_key = self.settings.get('custom_header_key')
273 head_val = self.settings['custom_header_val']
305 head_val = self.settings.get('custom_header_val')
274 if head_key and head_val:
306 if head_key and head_val:
275 headers = {head_key: head_val}
307 headers = {head_key: head_val}
276
308
@@ -305,12 +337,14 b' def post_to_webhook(url_calls, settings)'
305
337
306 headers = headers or {}
338 headers = headers or {}
307 call_headers.update(headers)
339 call_headers.update(headers)
340 auth = get_auth(settings)
308
341
309 log.debug('calling Webhook with method: %s', call_method)
342 log.debug('calling Webhook with method: %s, and auth:%s',
343 call_method, auth)
310 resp = call_method(url, json={
344 resp = call_method(url, json={
311 'token': token,
345 'token': token,
312 'event': data
346 'event': data
313 }, headers=call_headers)
347 }, headers=call_headers, auth=auth)
314 log.debug('Got Webhook response: %s', resp)
348 log.debug('Got Webhook response: %s', resp)
315
349
316 resp.raise_for_status() # raise exception on a failed request
350 resp.raise_for_status() # raise exception on a failed request
General Comments 0
You need to be logged in to leave comments. Login now