Skip to content
Snippets Groups Projects
Commit 3b25fff8 authored by David Trattnig's avatar David Trattnig
Browse files

refactor: add default timeout to requests

parent 4f0b1c67
No related branches found
No related tags found
1 merge request!35ORM-less scheduling
......@@ -204,7 +204,7 @@ class SimpleRestApi:
return json_data
@exception_handler
def get(self, url: str, headers: dict = None) -> requests.Response:
def get(self, url: str, headers: dict = None, timeout: int = 5) -> requests.Response:
"""
GET from an URL.
......@@ -222,13 +222,13 @@ class SimpleRestApi:
json_data = None
if not headers:
headers = SimpleRestApi.default_headers
response = requests.get(url, headers=headers)
response = requests.get(url, headers=headers, timeout=timeout)
if headers.get("content-type") == SimpleRestApi.CONTENT_JSON:
json_data = self.deserialize_json(response)
return DotDict({"response": response, "json": json_data})
@exception_handler
def post(self, url: str, data: dict, headers: dict = None):
def post(self, url: str, data: dict, headers: dict = None, timeout: int = 5):
"""
POST to an URL.
......@@ -248,10 +248,12 @@ class SimpleRestApi:
if not headers:
headers = SimpleRestApi.default_headers
body: str = self.serialize_json(data)
return requests.post(url, data=body, headers=headers)
return requests.post(url, data=body, headers=headers, timeout=timeout)
@exception_handler
def put(self, url: str, data: dict, headers: dict = None) -> requests.Response:
def put(
self, url: str, data: dict, headers: dict = None, timeout: int = 5
) -> requests.Response:
"""
PUT to an URL.
......@@ -271,7 +273,7 @@ class SimpleRestApi:
if not headers:
headers = SimpleRestApi.default_headers
body: str = self.serialize_json(data)
return requests.put(url, data=body, headers=headers)
return requests.put(url, data=body, headers=headers, timeout=timeout)
class SimpleCachedRestApi:
......
......@@ -197,6 +197,7 @@ class AuraMonitor:
body["isHealthy"] = is_healthy
body["details"] = json.dumps(data, default=str)
json_data = json.dumps(body, default=str)
timeout = 5
url = self.config.get("api_engine_store_health")
url = url.replace("${ENGINE_NUMBER}", str(self.config.get("api_engine_number")))
headers = {"content-type": "application/json"}
......@@ -204,7 +205,7 @@ class AuraMonitor:
response.status_code = 404
try:
response = requests.post(url, data=json_data, headers=headers)
response = requests.post(url, data=json_data, headers=headers, timeout=timeout)
if response.status_code == 204:
self.logger.info(
"Successfully posted healthy=%s state to Engine API!" % is_healthy
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment