Skip to content

Commit

Permalink
Merge pull request #47 from tutorcruncher/fix-webhook-saving
Browse files Browse the repository at this point in the history
Save webhook logs to correct endpoint
  • Loading branch information
HenryTraill authored Oct 29, 2024
2 parents b48327e + 48ea2d6 commit 6e9e2a6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions chronos/pydantic_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class RequestData(BaseModel):
Pydantic model for the RequestData object
"""

endpoint_id: int
request_headers: str
request_body: str
response_headers: str = '{"Message": "No response from endpoint"}'
Expand Down
12 changes: 8 additions & 4 deletions chronos/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
celery_app.conf.broker_connection_retry_on_startup = True


async def webhook_request(client: AsyncClient, url: str, *, webhook_sig: str, data: dict = None):
async def webhook_request(client: AsyncClient, url: str, endpoint_id: int, *, webhook_sig: str, data: dict = None):
"""
Send a request to TutorCruncher
:param client
Expand Down Expand Up @@ -57,7 +57,9 @@ async def webhook_request(client: AsyncClient, url: str, *, webhook_sig: str, da
else:
app_logger.info('Request method=%s url=%s status_code=%s', 'POST', url, r.status_code, extra={'data': data})

request_data = RequestData(request_headers=json.dumps(headers), request_body=json.dumps(data))
request_data = RequestData(
endpoint_id=endpoint_id, request_headers=json.dumps(headers), request_body=json.dumps(data)
)
if r is not None:
request_data.response_headers = json.dumps(dict(r.headers))
request_data.response_body = json.dumps(r.content.decode())
Expand Down Expand Up @@ -107,7 +109,9 @@ async def _async_post_webhooks(endpoints, url_extension, payload):
# Send the Webhook to the endpoint

loaded_payload = json.loads(payload)
task = asyncio.ensure_future(webhook_request(client, url, webhook_sig=sig_hex, data=loaded_payload))
task = asyncio.ensure_future(
webhook_request(client, url, endpoint.id, webhook_sig=sig_hex, data=loaded_payload)
)
tasks.append(task)
webhook_responses = await asyncio.gather(*tasks, return_exceptions=True)
for response in webhook_responses:
Expand All @@ -127,7 +131,7 @@ async def _async_post_webhooks(endpoints, url_extension, payload):
# Log the response
webhook_logs.append(
WebhookLog(
webhook_endpoint_id=endpoint.id,
webhook_endpoint_id=response.endpoint_id,
request_headers=response.request_headers,
request_body=response.request_body,
response_headers=response.response_headers,
Expand Down

0 comments on commit 6e9e2a6

Please sign in to comment.