import requests # from logger import logger import time from firebase_admin import db, initialize_app, credentials, get_app def send_webhook_data(uid, field_id, imagedate): """ Handles webhook call. Will retry 4 times. :param uid: :param field_id: :param imagedate: :return: None """ # init firebase with DB4 cred = credentials.Certificate("servicekey.json") try: fireApp4 = initialize_app( cred, {"databaseURL": "https://farmbase-b2f7e-741df.firebaseio.com/"}, "fireApp4", ) except: fireApp4 = get_app("fireApp4") try: # get webhook url sat_webhook_ref = ( db.reference("SecureUserData", fireApp4) .child(uid) .child("webhooks/satellite") ) webhook_obj = sat_webhook_ref.get() print("webhook_obj", webhook_obj) if webhook_obj is None: return webhook_url = webhook_obj.get("url") webhook_version = webhook_obj.get("version", 1) if webhook_url is None or webhook_version != 1: return # try link 4 times with timeouts if fails obj = { "FieldID": field_id, "SensedDay": imagedate, } attempt = 1 while attempt <= 4: response = requests.post( webhook_url, json=obj, ) if response.status_code == 200: return # failed print(f"Error sending webhook: {response.status_code} - {response.text}") attempt += 1 time.sleep(2 ** (attempt - 1)) except Exception as e: print(f"Error sending webhook: {e}")