-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
206 additions
and
5 deletions.
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
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
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,74 @@ | ||
""" | ||
Generic Webhook Notifications | ||
""" | ||
|
||
import logging | ||
from typing import List | ||
|
||
import requests | ||
|
||
from camply.config.notification_config import WebhookConfig | ||
from camply.containers import AvailableCampsite | ||
from camply.containers.data_containers import WebhookBody | ||
from camply.notifications.base_notifications import BaseNotifications | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class WebhookNotifications(BaseNotifications): | ||
""" | ||
Push Notifications via Webhooks | ||
""" | ||
|
||
last_gasp: bool = False | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.webhook_url = WebhookConfig.WEBHOOK_URL | ||
self.webhook_headers = WebhookConfig.DEFAULT_HEADERS | ||
self.webhook_headers.update(WebhookConfig.WEBHOOK_HEADERS) | ||
self.session.headers = self.webhook_headers | ||
if self.webhook_url is None: | ||
warning_message = ( | ||
"Webhook notifications are not configured properly. " | ||
"To send webhook messages " | ||
"make sure to run `camply configure` or set the " | ||
"proper environment variables: " | ||
"`WEBHOOK_URL` / `WEBHOOK_HEADERS`." | ||
) | ||
logger.error(warning_message) | ||
raise EnvironmentError(warning_message) | ||
|
||
def send_message(self, message: str, **kwargs) -> requests.Response: | ||
""" | ||
Send a message via Webhook | ||
Parameters | ||
---------- | ||
message: str | ||
Returns | ||
------- | ||
requests.Response | ||
""" | ||
response = self.session.post(url=self.webhook_url, data=message) | ||
try: | ||
response.raise_for_status() | ||
except requests.HTTPError as he: | ||
logger.warning( | ||
f"Notifications weren't able to be sent to {self.webhook_url}. " | ||
"Your configuration might be incorrect." | ||
) | ||
raise ConnectionError(response.text) from he | ||
return response | ||
|
||
def send_campsites(self, campsites: List[AvailableCampsite], **kwargs): | ||
""" | ||
Send a message with a campsite object | ||
Parameters | ||
---------- | ||
campsites: List[AvailableCampsite] | ||
""" | ||
webhook_body = WebhookBody(campsites=campsites).json() | ||
self.send_message(message=webhook_body) |
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