Skip to content

Commit

Permalink
#patch: fix missing webhook fields when sanitifizing webhook data
Browse files Browse the repository at this point in the history
Signed-off-by: circa10a <caleblemoine@gmail.com>
  • Loading branch information
circa10a committed Jan 14, 2025
1 parent df4e5e9 commit 883d103
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python
FROM python:3.12
ENV WORKDIR=/opt/cv-notifier
WORKDIR $WORKDIR
COPY . .
Expand Down
14 changes: 12 additions & 2 deletions lib/webhooks/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ def sanitize_webhook(webhook: Dict, template_strs: Dict) -> Dict:
sanitized_webhook['url'] = Template(webhook['url']).substitute(template_strs)
sanitized_webhook['method'] = Template(webhook['method']).substitute(template_strs)
sanitized_webhook['headers'] = sanitized_headers
sanitized_webhook['data'] = Template(webhook['body']).substitute(template_strs)
sanitized_webhook['body'] = Template(webhook['body']).substitute(template_strs)
sanitized_webhook['timeout'] = webhook['timeout']
sanitized_webhook['notifyInterval'] = webhook['notifyInterval']
sanitized_webhook['objects'] = webhook['objects']

return sanitized_webhook

Expand All @@ -55,6 +57,14 @@ def process(webhooks: List[Dict], object_name: str, object_confidence: str) -> N
# Subsitute url, headers, body with object data, environment variables for secrets
webhook = sanitize_webhook(webhook, template_strs)
# Create opts to send request and hash to render an immutable ID to track last notification
# Opts for requests library
request_opts = {
'url': webhook['url'],
'method': webhook['method'],
'headers': webhook['headers'],
'data': webhook['body'],
'timeout': webhook['timeout']
}
# Get the webhook's ID and check when webhook was last notified
webhook_hash = dict_hash(webhook)
if webhook_hash in WEBHOOKS_LAST_NOTIFIED:
Expand All @@ -68,7 +78,7 @@ def process(webhooks: List[Dict], object_name: str, object_confidence: str) -> N
# Otherwise continue and notify
try:
log.info(f"Object {object_name} detected with confidence of {object_confidence}. Notifying {webhook['url']}")
response = requests.request(**webhook)
response = requests.request(**request_opts)
# Raise an error in the event of a non 2XX response code
response.raise_for_status()
# Request was successful, so we store last notified time
Expand Down
15 changes: 7 additions & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
ultralytics >= "8.0.106"
schema >= "0.7.5"
requests >= "2.31.0"
loguru >= "0.7.0"
-f https://download.pytorch.org/whl/cpu
torch >= "2.0.1"
torchvision >= "0.15.2"
torchaudio >= "2.0.2"
ultralytics>=8.0.106
schema>=0.7.5
requests>=2.31.0
loguru>=0.7.0 -f https://download.pytorch.org/whl/cpu
torch>=2.0.1
torchvision>=0.15.2
torchaudio>=2.0.2

0 comments on commit 883d103

Please # to comment.