-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added output to webhook as an option (#558)
* Added output to webhook as an option * added documentation for new webhook configuration --------- Co-authored-by: Sean Whalen <[email protected]>
- Loading branch information
1 parent
cf46558
commit 0a6cfb6
Showing
3 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import requests | ||
|
||
from parsedmarc import logger | ||
|
||
|
||
class WebhookClient(object): | ||
""" A client for webhooks""" | ||
|
||
def __init__(self, aggregate_url, forensic_url, smtp_tls_url, | ||
timeout=60): | ||
""" | ||
Initializes the WebhookClient | ||
Args: | ||
aggregate_url (str): The aggregate report webhook url | ||
forensic_url (str): The forensic report webhook url | ||
smtp_tls_url (str): The smtp_tls report webhook url | ||
timeout (int): The timeout to use when calling the webhooks | ||
""" | ||
self.aggregate_url = aggregate_url | ||
self.forensic_url = forensic_url | ||
self.smtp_tls_url = smtp_tls_url | ||
self.timeout = timeout | ||
self.session = requests.Session() | ||
self.session.headers = { | ||
'User-Agent': 'parsedmarc', | ||
'Content-Type': 'application/json', | ||
} | ||
|
||
def save_forensic_report_to_webhook(self, report): | ||
try: | ||
self._send_to_webhook(self.forensic_url, report) | ||
except Exception as error_: | ||
logger.error("Webhook Error: {0}".format(error_.__str__())) | ||
|
||
def save_smtp_tls_report_to_webhook(self, report): | ||
try: | ||
self._send_to_webhook(self.smtp_tls_url, report) | ||
except Exception as error_: | ||
logger.error("Webhook Error: {0}".format(error_.__str__())) | ||
|
||
def save_aggregate_report_to_webhook(self, report): | ||
try: | ||
self._send_to_webhook(self.aggregate_url, report) | ||
except Exception as error_: | ||
logger.error("Webhook Error: {0}".format(error_.__str__())) | ||
|
||
def _send_to_webhook(self, webhook_url, payload): | ||
try: | ||
self.session.post(webhook_url, data=payload, timeout=self.timeout) | ||
except Exception as error_: | ||
logger.error("Webhook Error: {0}".format(error_.__str__())) |