Use requests now that we need it
This commit is contained in:
parent
8dbdec6d32
commit
e4b4039ed3
1 changed files with 11 additions and 17 deletions
|
@ -1,10 +1,8 @@
|
|||
import urllib.request
|
||||
import urllib.error
|
||||
import hmac
|
||||
import hashlib
|
||||
import json
|
||||
from datetime import datetime
|
||||
from flask import current_app
|
||||
import requests
|
||||
from datetime import datetime, timezone
|
||||
from .models import Settings
|
||||
|
||||
class WebhookError(Exception):
|
||||
|
@ -31,40 +29,36 @@ def _send_webhook(event_type, data):
|
|||
|
||||
payload = {
|
||||
"event_type": event_type,
|
||||
"timestamp": datetime.utcnow().isoformat(),
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
"data": data
|
||||
}
|
||||
|
||||
# Convert payload to JSON
|
||||
payload_str = json.dumps(payload).encode('utf-8')
|
||||
payload_str = json.dumps(payload)
|
||||
|
||||
# Create request with headers
|
||||
headers = {'Content-Type': 'application/json'}
|
||||
if settings.webhook_secret:
|
||||
signature = hmac.new(
|
||||
settings.webhook_secret.encode(),
|
||||
payload_str,
|
||||
payload_str.encode('utf-8'),
|
||||
hashlib.sha256
|
||||
).hexdigest()
|
||||
headers['X-Webhook-Signature'] = signature
|
||||
|
||||
try:
|
||||
# Create request object
|
||||
req = urllib.request.Request(
|
||||
# Send request using requests library
|
||||
response = requests.post(
|
||||
settings.webhook_url,
|
||||
data=payload_str,
|
||||
headers=headers,
|
||||
method='POST'
|
||||
timeout=5
|
||||
)
|
||||
|
||||
# Set timeout
|
||||
with urllib.request.urlopen(req, timeout=5) as response:
|
||||
if response.status != 200:
|
||||
raise WebhookError(f"Webhook error: {response.status}", status_code=response.status)
|
||||
# Check response status
|
||||
response.raise_for_status()
|
||||
except Exception as e:
|
||||
status_code = None
|
||||
if hasattr(e, 'status_code'):
|
||||
status_code = e.status_code
|
||||
status_code = e.response.status_code if hasattr(e, 'response') else None
|
||||
raise WebhookError(f"Webhook error: {str(e)}", status_code=status_code, original_exception=e)
|
||||
|
||||
def inquiry_created(inquiry_id, message):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue