buildbot / lead_client.py
chandrashekhar2002dubey's picture
Added context of the client
9f48b57
Raw History Blame Contribute Delete
2.13 kB
import os
from dotenv import load_dotenv
import requests
from fastapi import FastAPI
from pydantic import BaseModel
load_dotenv() # Load environment variables from .env file
# πŸ”‘ CONFIG
BASEROW_TOKEN = "5WioOnzQce7V6ZS2smAjCOr3TEfXNBFM"
LEAD_CLIENT_TABLE_ID = 914565
# if not BASEROW_TOKEN or not TABLE_ID:
# raise Exception("❌ Missing BASEROW_TOKEN or TABLE_ID in environment variables")
BASE_URL = f"https://api.baserow.io/api/database/rows/table/{LEAD_CLIENT_TABLE_ID}/"
HEADERS = {
"Authorization": f"Token {BASEROW_TOKEN}",
"Content-Type": "application/json"
}
# πŸ“₯ Request Model
class User(BaseModel):
name: str
mobile: str
org: str
# πŸ” Check if user exists (SAFE METHOD)
def check_user_exists(mobile):
try:
response = requests.get(BASE_URL, headers=HEADERS)
# print("\n--- CHECK USER DEBUG ---")
# print("STATUS:", response.status_code)
# print("RESPONSE:", response.text)
if response.status_code == 200:
data = response.json()
for row in data.get("results", []):
# βœ… use field ID
if str(row.get("field_7932385")) == str(mobile):
return {
"name": row.get("field_7932386"),
"mobile": row.get("field_7932385"),
"org": row.get("field_7968316")
}
return False
except Exception as e:
print("CHECK ERROR:", str(e))
return False
# πŸ’Ύ Save user to Baserow
def save_user(name, mobile, orgId):
payload = {
"field_7932386": name, # βœ… name
"field_7932385": str(mobile), # βœ… mobile
"field_7968316": orgId # βœ… organization ID
}
try:
response = requests.post(BASE_URL, headers=HEADERS, json=payload)
# print("\n--- SAVE USER DEBUG ---")
# print("PAYLOAD:", payload)
# print("STATUS:", response.status_code)
# print("RESPONSE:", response.text)
return response.status_code in [200, 201]
except Exception as e:
print("SAVE ERROR:", str(e))
return False