Skip to content

Commit

Permalink
added timeout to middleware requests
Browse files Browse the repository at this point in the history
  • Loading branch information
DraKen0009 committed Sep 7, 2024
1 parent 4f6296a commit 5a10159
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 11 additions & 0 deletions care/utils/assetintegration/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def __init__(self, meta):
self.host = self.meta["local_ip_address"]
self.middleware_hostname = self.meta["middleware_hostname"]
self.insecure_connection = self.meta.get("insecure_connection", False)
self.timeout = getattr(settings, "MIDDLEWARE_API_TIMEOUT", 20)

def handle_action(self, action):
pass
Expand All @@ -37,12 +38,17 @@ def api_post(self, url, data=None):
url,
json=data,
headers=self.get_headers(),
timeout=self.timeout,
)
try:
response = req.json()
if req.status_code >= 400:
raise APIException(response, req.status_code)
return response

except requests.Timeout:
raise APIException({"error": "Request Timeout"}, 504)

except json.decoder.JSONDecodeError:
raise APIException({"error": "Invalid Response"}, req.status_code)

Expand All @@ -51,11 +57,16 @@ def api_get(self, url, data=None):
url,
params=data,
headers=self.get_headers(),
timeout=self.timeout,
)
try:
if req.status_code >= 400:
raise APIException(req.text, req.status_code)
response = req.json()
return response

except requests.Timeout:
raise APIException({"error": "Request Timeout"}, 504)

except json.decoder.JSONDecodeError:
raise APIException({"error": "Invalid Response"}, req.status_code)
5 changes: 4 additions & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import json
from datetime import datetime, timedelta
from pathlib import Path
from django.utils.translation import gettext_lazy as _

import environ
from authlib.jose import JsonWebKey
from django.utils.translation import gettext_lazy as _
from healthy_django.healthcheck.celery_queue_length import (
DjangoCeleryQueueLengthHealthCheck,
)
Expand Down Expand Up @@ -663,3 +663,6 @@
TASK_SUMMARIZE_DISTRICT_PATIENT = env.bool(
"TASK_SUMMARIZE_DISTRICT_PATIENT", default=True
)

# Timeout for middleware request (in seconds)
MIDDLEWARE_API_TIMEOUT = 20

0 comments on commit 5a10159

Please sign in to comment.