From 8cbf56dec23b6af35269a8937e85ff916b2d0e2f Mon Sep 17 00:00:00 2001 From: pehlicd <65170388+pehlicd@users.noreply.github.com> Date: Thu, 21 Nov 2024 11:44:34 +0100 Subject: [PATCH] feat(providers): added retry mechanism to google chat provider to overcome rate limiting --- .../google_chat_provider.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/keep/providers/google_chat_provider/google_chat_provider.py b/keep/providers/google_chat_provider/google_chat_provider.py index ba27d4624..a572147cc 100644 --- a/keep/providers/google_chat_provider/google_chat_provider.py +++ b/keep/providers/google_chat_provider/google_chat_provider.py @@ -1,4 +1,7 @@ +import http import os +import time + import pydantic import dataclasses import requests @@ -65,13 +68,23 @@ def _notify(self, message="", **kwargs: dict): if not message: raise ProviderException("Message is required") + def __send_message(url, body, headers, retries=3): + for _ in range(retries): + try: + resp = requests.post(url, json=body, headers=headers) + if resp.status_code == http.HTTPStatus.OK: + return resp + except requests.exceptions.RequestException as e: + self.logger.error(f"Failed to send message to Google Chat: {e}") + time.sleep(1) + payload = { "text": message, } - requestHeaders = {"Content-Type": "application/json; charset=UTF-8"} + request_headers = {"Content-Type": "application/json; charset=UTF-8"} - response = requests.post(webhook_url, json=payload, headers=requestHeaders) + response = __send_message(webhook_url, body=payload, headers=request_headers) if not response.ok: raise ProviderException(