Bruhletme commited on
Commit
d43bb8e
·
1 Parent(s): f27b094

fix: add limit param to prevent timeout

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -37,7 +37,7 @@ def home():
37
  "status": "online",
38
  "message": "User Search API",
39
  "endpoints": {
40
- "search": "/search?type=mobile&q=9999999891",
41
  "types": ["mobile", "alt", "id", "email"]
42
  }
43
  }
@@ -45,31 +45,33 @@ def home():
45
  @app.get("/search")
46
  def search(
47
  q: str = Query(..., description="Search value"),
48
- type: str = Query("mobile", description=f"Search type: {VALID_TYPES}")
 
49
  ):
50
  q_clean = str(q).strip()
51
  type_clean = str(type).strip().lower()
 
52
 
53
  if type_clean not in VALID_TYPES:
54
- return {"status": "error", "message": f"Invalid type. Valid: {VALID_TYPES}"}
55
 
56
  if type_clean == "mobile" and not re.match(r'^\d{10}$', q_clean):
57
- return {"status": "error", "message": "Mobile number must be 10 digits"}
58
  if type_clean == "alt" and q_clean and not re.match(r'^\d{10}$', q_clean):
59
- return {"status": "error", "message": "Alternate number must be 10 digits"}
60
  if type_clean == "id" and not re.match(r'^\d{12}$', q_clean):
61
- return {"status": "error", "message": "Aadhar ID must be 12 digits"}
62
  if type_clean == "email" and not re.match(r'^[^@]+@[^@]+\.[^@]+$', q_clean):
63
- return {"status": "error", "message": "Invalid email format"}
64
 
65
- future = executor.submit(_search_db, q_clean, type_clean)
66
  return future.result()
67
 
68
- def _search_db(q: str, type: str):
69
  try:
70
  col = SEARCH_COLUMNS[type]
71
- query = f"SELECT mobile, name, fname AS father_name, address, circle, alt AS alternate, id AS aadhar, email FROM users WHERE {col} = ?"
72
- results = con.execute(query, [q]).fetchall()
73
 
74
  if not results:
75
  return {"success": False, "message": f"{type} '{q}' not found"}
 
37
  "status": "online",
38
  "message": "User Search API",
39
  "endpoints": {
40
+ "search": "/search?type=mobile&q=9876543210&limit=100",
41
  "types": ["mobile", "alt", "id", "email"]
42
  }
43
  }
 
45
  @app.get("/search")
46
  def search(
47
  q: str = Query(..., description="Search value"),
48
+ type: str = Query("mobile", description=f"Search type: {VALID_TYPES}"),
49
+ limit: int = Query(100, description="Max results to return")
50
  ):
51
  q_clean = str(q).strip()
52
  type_clean = str(type).strip().lower()
53
+ limit_clean = min(max(limit, 1), 500)
54
 
55
  if type_clean not in VALID_TYPES:
56
+ return {"success": False, "message": f"Invalid type. Valid: {VALID_TYPES}"}
57
 
58
  if type_clean == "mobile" and not re.match(r'^\d{10}$', q_clean):
59
+ return {"success": False, "message": "Mobile number must be 10 digits"}
60
  if type_clean == "alt" and q_clean and not re.match(r'^\d{10}$', q_clean):
61
+ return {"success": False, "message": "Alternate number must be 10 digits"}
62
  if type_clean == "id" and not re.match(r'^\d{12}$', q_clean):
63
+ return {"success": False, "message": "Aadhar ID must be 12 digits"}
64
  if type_clean == "email" and not re.match(r'^[^@]+@[^@]+\.[^@]+$', q_clean):
65
+ return {"success": False, "message": "Invalid email format"}
66
 
67
+ future = executor.submit(_search_db, q_clean, type_clean, limit_clean)
68
  return future.result()
69
 
70
+ def _search_db(q: str, type: str, limit: int = 100):
71
  try:
72
  col = SEARCH_COLUMNS[type]
73
+ query = f"SELECT mobile, name, fname AS father_name, address, circle, alt AS alternate, id AS aadhar, email FROM users WHERE {col} = ? LIMIT ?"
74
+ results = con.execute(query, [q, limit]).fetchall()
75
 
76
  if not results:
77
  return {"success": False, "message": f"{type} '{q}' not found"}