Merge pull request #5342 from dannon/integration.webhooks_improvements

[18.01] Webhooks improvements
This commit is contained in:
Martin Cech
2018-02-01 15:14:47 -05:00
committed by GitHub
38 changed files with 220 additions and 288 deletions
+9 -38
View File
@@ -3,7 +3,6 @@ API Controller providing Galaxy Webhooks
"""
import imp
import logging
import random
from galaxy.web import _future_expose_api_anonymous_and_sessionless as \
expose_api_anonymous_and_sessionless
@@ -17,7 +16,7 @@ class WebhooksController(BaseAPIController):
super(WebhooksController, self).__init__(app)
@expose_api_anonymous_and_sessionless
def get_all(self, trans, **kwd):
def all_webhooks(self, trans, **kwd):
"""
*GET /api/webhooks/
Returns all webhooks
@@ -28,35 +27,9 @@ class WebhooksController(BaseAPIController):
]
@expose_api_anonymous_and_sessionless
def get_random(self, trans, webhook_type, **kwd):
def webhook_data(self, trans, webhook_id, **kwd):
"""
*GET /api/webhooks/{webhook_type}
Returns a random webhook for a given type
"""
webhooks = [
webhook
for webhook in self.app.webhooks_registry.webhooks
if webhook_type in webhook.type and
webhook.activate is True
]
return random.choice(webhooks).to_dict() if webhooks else {}
@expose_api_anonymous_and_sessionless
def get_all_by_type(self, trans, webhook_type, **kwd):
"""
*GET /api/webhooks/{webhook_type}/all
Returns all webhooks for a given type
"""
return [
webhook.to_dict()
for webhook in self.app.webhooks_registry.webhooks
if webhook_type in webhook.type
]
@expose_api_anonymous_and_sessionless
def get_data(self, trans, webhook_name, **kwd):
"""
*GET /api/webhooks/{webhook_name}/get_data/{params}
*GET /api/webhooks/{webhook_id}/data/{params}
Returns the result of executing helper function
"""
params = {}
@@ -64,14 +37,12 @@ class WebhooksController(BaseAPIController):
for key, value in kwd.items():
params[key] = value
webhook = [
webhook = (
webhook
for webhook in self.app.webhooks_registry.webhooks
if webhook.name == webhook_name
]
if webhook.id == webhook_id
).next()
return imp.load_source('helper', webhook[0].helper).main(
trans,
webhook[0],
params,
) if webhook and webhook[0].helper != '' else {}
return imp.load_source(webhook.path, webhook.helper).main(
trans, webhook, params,
) if webhook and webhook.helper != '' else {}
+7 -19
View File
@@ -609,29 +609,17 @@ def populate_api_routes(webapp, app):
# ===== WEBHOOKS API =====
# ========================
webapp.mapper.connect('get_all',
webapp.mapper.connect('get_all_webhooks',
'/api/webhooks',
controller='webhooks',
action='get_all',
conditions=dict(method=["GET"]))
action='all_webhooks',
conditions=dict(method=['GET']))
webapp.mapper.connect('get_random',
'/api/webhooks/{webhook_type}',
webapp.mapper.connect('get_webhook_data',
'/api/webhooks/{webhook_id}/data',
controller='webhooks',
action='get_random',
conditions=dict(method=["GET"]))
webapp.mapper.connect('get_all_by_type',
'/api/webhooks/{webhook_type}/all',
controller='webhooks',
action='get_all_by_type',
conditions=dict(method=["GET"]))
webapp.mapper.connect('get_data',
'/api/webhooks/{webhook_name}/get_data',
controller='webhooks',
action='get_data',
conditions=dict(method=["GET"]))
action='webhook_data',
conditions=dict(method=['GET']))
# =======================
# ===== LIBRARY API =====
+56 -53
View File
@@ -12,11 +12,12 @@ log = logging.getLogger(__name__)
class Webhook(object):
def __init__(self, w_name, w_type, w_activate, w_path):
self.name = w_name
self.type = w_type
self.activate = w_activate
self.path = w_path
def __init__(self, id, type, activate, weight, path):
self.id = id
self.type = type
self.activate = activate
self.weight = weight
self.path = path
self.styles = ''
self.script = ''
self.helper = ''
@@ -24,12 +25,13 @@ class Webhook(object):
def to_dict(self):
return {
'name': self.name,
'id': self.id,
'type': self.type,
'activate': self.activate,
'weight': self.weight,
'styles': self.styles,
'script': self.script,
'config': self.config
'config': self.config,
}
@@ -48,56 +50,57 @@ class WebhooksRegistry(object):
def load_webhooks(self):
for directory in self.webhooks_directories:
config_dir = os.path.join(directory, 'config')
config_file_path = None
for config_file in ['config.yml', 'config.yaml']:
path = os.path.join(directory, config_file)
if os.path.isfile(path):
config_file_path = path
break
if not os.path.exists(config_dir):
log.warning('directory not found: %s', config_dir)
continue
if config_file_path:
try:
self.load_webhook_from_config(directory, config_file_path)
except Exception as e:
log.exception(e)
config_dir_contents = os.listdir(config_dir)
# We are assuming that all yml/yaml files in a webhooks'
# config directory are webhook config files.
for config_file in config_dir_contents:
if config_file.endswith('.yml') or config_file.endswith('.yaml'):
self.load_webhook_from_config(config_dir, config_file)
def load_webhook_from_config(self, webhook_dir, config_file_path):
with open(config_file_path) as file:
config = yaml.safe_load(file)
def load_webhook_from_config(self, config_dir, config_file):
weight = config.get('weight', 1)
if weight < 1:
raise ValueError('Webhook weight must be greater or equal 1.')
webhook = Webhook(
config.get('id'),
config.get('type'),
config.get('activate', False),
weight,
webhook_dir,
)
# Read styles into a string, assuming all styles are in a
# single file
try:
with open(os.path.join(config_dir, config_file)) as file:
config = yaml.safe_load(file)
path = os.path.normpath(os.path.join(config_dir, '..'))
webhook = Webhook(
config['name'],
config['type'],
config['activate'],
path,
)
styles_file = os.path.join(webhook_dir, 'styles.css')
with open(styles_file, 'r') as file:
webhook.styles = file.read().replace('\n', '')
except IOError:
pass
# Read styles into a string, assuming all styles are in a
# single file
try:
styles_file = os.path.join(path, 'static/styles.css')
with open(styles_file, 'r') as file:
webhook.styles = file.read().replace('\n', '')
except IOError:
pass
# Read script into a string, assuming everything is in a
# single file
try:
script_file = os.path.join(webhook_dir, 'script.js')
with open(script_file, 'r') as file:
webhook.script = file.read()
except IOError:
pass
# Read script into a string, assuming everything is in a
# single file
try:
script_file = os.path.join(path, 'static/script.js')
with open(script_file, 'r') as file:
webhook.script = file.read()
except IOError:
pass
# Save helper function path if it exists
helper_path = os.path.join(webhook_dir, '__init__.py')
if os.path.isfile(helper_path):
webhook.helper = helper_path
# Save helper function path if it exists
helper_path = os.path.join(path, 'helper/__init__.py')
if os.path.isfile(helper_path):
webhook.helper = helper_path
webhook.config = config
self.webhooks.append(webhook)
except Exception as e:
log.exception(e)
webhook.config = config
self.webhooks.append(webhook)