import httpx import os from datetime import datetime from dotenv import load_dotenv load_dotenv() BASEROW_TOKEN = "5WioOnzQce7V6ZS2smAjCOr3TEfXNBFM" USER_CHAT_TABLE_ID = 916555 BASE_URL = f"https://api.baserow.io/api/database/rows/table/{USER_CHAT_TABLE_ID}/?user_field_names=true" class BaserowClient: def __init__(self): self.headers = { "Authorization": f"Token {BASEROW_TOKEN}", "Content-Type": "application/json" } # 🔍 FIND estimate BY MOBILE def find_estimate(self, mobile): url = f"{BASE_URL}&filter__mobile__equal={mobile}" with httpx.Client() as client: res = client.get(url, headers=self.headers) # print("\n--- FIND estimate ---") # print(res.status_code, res.text) data = res.json() return data["results"][0] if data.get("results") else None # ➕ INSERT def insert_estimate(self, data): with httpx.Client() as client: res = client.post(BASE_URL, headers=self.headers, json=data) # print("\n--- INSERT ---") # print(res.status_code, res.text) return res.json() # ✏️ UPDATE def update_estimate(self, row_id, data): url = f"https://api.baserow.io/api/database/rows/table/{USER_CHAT_TABLE_ID}/{row_id}/?user_field_names=true" with httpx.Client() as client: res = client.patch(url, headers=self.headers, json=data) # print("\n--- UPDATE ---") # print(res.status_code, res.text) return res.json() # 🔁 UPSERT (MAIN LOGIC) def upsert_estimate(self, mobile, floors, estimate, sqft): #existing = self.find_estimate(mobile) data = { "mobile": mobile, "floors": floors, "sqft": sqft, "estimate": estimate, "created_at": datetime.now().isoformat() } # if existing: # return self.update_estimate(existing["id"], data) return self.insert_estimate(data)