mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2024-08-30 18:23:09 +00:00
Instantiate Jinja environment and create renderer in base webhook
This commit is contained in:
parent
438b7cc6ab
commit
ae4f806bac
@ -1,6 +1,7 @@
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
|
from jinja2 import Environment, BaseLoader
|
||||||
|
|
||||||
from app.classes.shared.helpers import Helpers
|
from app.classes.shared.helpers import Helpers
|
||||||
|
|
||||||
@ -16,6 +17,9 @@ class WebhookProvider(ABC):
|
|||||||
ensuring that each provider will have a send method.
|
ensuring that each provider will have a send method.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.jinja_env = Environment(loader=BaseLoader())
|
||||||
|
|
||||||
WEBHOOK_USERNAME = "Crafty Webhooks"
|
WEBHOOK_USERNAME = "Crafty Webhooks"
|
||||||
WEBHOOK_PFP_URL = (
|
WEBHOOK_PFP_URL = (
|
||||||
"https://gitlab.com/crafty-controller/crafty-4/-"
|
"https://gitlab.com/crafty-controller/crafty-4/-"
|
||||||
@ -34,6 +38,25 @@ class WebhookProvider(ABC):
|
|||||||
logger.error(error)
|
logger.error(error)
|
||||||
raise RuntimeError(f"Failed to dispatch notification: {error}") from error
|
raise RuntimeError(f"Failed to dispatch notification: {error}") from error
|
||||||
|
|
||||||
|
def render_template(self, template_str, context):
|
||||||
|
"""
|
||||||
|
Renders a Jinja2 template with the provided context.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
template_str (str): The Jinja2 template string.
|
||||||
|
context (dict): A dictionary containing all the variables needed for
|
||||||
|
rendering the template.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The rendered message.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
template = self.jinja_env.from_string(template_str)
|
||||||
|
return template.render(context)
|
||||||
|
except Exception as error:
|
||||||
|
logger.error(f"Error rendering Jinja2 template: {error}")
|
||||||
|
raise
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def send(self, server_name, title, url, message, **kwargs):
|
def send(self, server_name, title, url, message_template, event_data, **kwargs):
|
||||||
"""Abstract method that derived classes will implement for sending webhooks."""
|
"""Abstract method that derived classes will implement for sending webhooks."""
|
||||||
|
Loading…
Reference in New Issue
Block a user