-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
6f014b3
commit 750115b
Showing
3 changed files
with
82 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import logging | ||
|
||
import requests | ||
from django.conf import settings | ||
from huey.contrib.djhuey import task | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
BASE_URL = "https://hooks.slack.com/services/" | ||
|
||
|
||
def get_default_params(): | ||
return {} | ||
|
||
|
||
def get_default_client(params={}): | ||
params |= get_default_params() | ||
headers = { | ||
"user-agent": "betagouv-lemarche/0.0.1", | ||
} | ||
client = requests.Session() | ||
client.params = params | ||
client.headers = headers | ||
return client | ||
|
||
|
||
@task() | ||
def send_data_to_webhook( | ||
data: dict, webhook_url: str = settings.N8N_C4_TENDER_WEBHOOK, client: requests.Session = None | ||
): | ||
"""Huey task to send message to specific payload for specific slack service | ||
Args: | ||
payload (dict): payload for serivce | ||
service_id (str): service id of the service (ex of service: Webhook) | ||
client (requests.Session, optional): client to send requests. Defaults to None. | ||
Raises: | ||
e: requests.HTTPStatusError | ||
""" | ||
# if settings.SLACK_NOTIF_IS_ACTIVE: | ||
if not client: | ||
client = get_default_client() | ||
|
||
try: | ||
response = client.post(f"{webhook_url}", data=data) | ||
response.raise_for_status() | ||
print("N8N: send data to webhook") | ||
logger.info("N8N: send data to webhook") | ||
# logger.info(response.json()) // you'll receive a "HTTP 200" response with a plain text ok indicating that your message posted successfully # noqa | ||
return True | ||
except requests.exceptions.HTTPError as e: | ||
logger.error("Error while fetching `%s`: %s", e.request.url, e) | ||
raise e |
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