Skip to content

Commit 6e9e2a6

Browse files
authored
Merge pull request #47 from tutorcruncher/fix-webhook-saving
Save webhook logs to correct endpoint
2 parents b48327e + 48ea2d6 commit 6e9e2a6

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

chronos/pydantic_schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class RequestData(BaseModel):
7272
Pydantic model for the RequestData object
7373
"""
7474

75+
endpoint_id: int
7576
request_headers: str
7677
request_body: str
7778
response_headers: str = '{"Message": "No response from endpoint"}'

chronos/worker.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
celery_app.conf.broker_connection_retry_on_startup = True
2525

2626

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

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

109111
loaded_payload = json.loads(payload)
110-
task = asyncio.ensure_future(webhook_request(client, url, webhook_sig=sig_hex, data=loaded_payload))
112+
task = asyncio.ensure_future(
113+
webhook_request(client, url, endpoint.id, webhook_sig=sig_hex, data=loaded_payload)
114+
)
111115
tasks.append(task)
112116
webhook_responses = await asyncio.gather(*tasks, return_exceptions=True)
113117
for response in webhook_responses:
@@ -127,7 +131,7 @@ async def _async_post_webhooks(endpoints, url_extension, payload):
127131
# Log the response
128132
webhook_logs.append(
129133
WebhookLog(
130-
webhook_endpoint_id=endpoint.id,
134+
webhook_endpoint_id=response.endpoint_id,
131135
request_headers=response.request_headers,
132136
request_body=response.request_body,
133137
response_headers=response.response_headers,

0 commit comments

Comments
 (0)