-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from DalgoT4D/webhook-and-file-upload-session-c…
…hange Webhook and file upload session change
- Loading branch information
Showing
5 changed files
with
197 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import requests | ||
from requests.exceptions import HTTPError | ||
from pydantic import BaseModel | ||
import logging | ||
|
||
|
||
logger = logging.getLogger() | ||
|
||
|
||
class WebhookConfig(BaseModel): | ||
""" | ||
Details of webhook required to post results | ||
""" | ||
|
||
endpoint: str | ||
headers: dict | ||
|
||
|
||
class CustomWebhook: | ||
timeout = 20 | ||
|
||
def __init__(self, config: WebhookConfig): | ||
# TODO: maybe some validations on the endpoint etc. | ||
self.config: WebhookConfig = config | ||
|
||
def post_result(self, results: dict): | ||
""" | ||
Posts data to the configured webhook endpoint. | ||
""" | ||
try: | ||
response = requests.post( | ||
self.config.endpoint, | ||
json=results, | ||
headers=self.config.headers, | ||
timeout=self.timeout, | ||
) | ||
|
||
response.raise_for_status() | ||
|
||
logging.info(f"Successfully posted results to {self.config.endpoint}") | ||
return response.json() | ||
except HTTPError as err: | ||
logging.error(f"Failed to post webhook results {err.response.text}") | ||
return {"error": str(err.response.text)} | ||
except Exception as err: | ||
logging.error( | ||
f"Failed to post webhook results {str(err)}. Something went wrong" | ||
) | ||
return {"error": str(err)} |
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
Oops, something went wrong.