Inayatgaming commited on
Commit
d9f8dd0
·
verified ·
1 Parent(s): 8438a3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -47
app.py CHANGED
@@ -4,26 +4,12 @@ import duckdb
4
  from fastapi import FastAPI, Query, HTTPException
5
 
6
  app = FastAPI(
7
- title="InayatGaming Dataset API",
8
- version="1.0.0"
9
  )
10
 
11
  API_KEY = os.getenv("API_KEY")
12
-
13
- PARQUET_FILES = [
14
- "hf://datasets/Inayatgaming/InayatGaming/part1.parquet",
15
- "hf://datasets/Inayatgaming/InayatGaming/part2a.parquet",
16
- "hf://datasets/Inayatgaming/InayatGaming/part2b_new.parquet",
17
- ]
18
-
19
- # Only fields explicitly approved for API output.
20
- PUBLIC_FIELDS = [
21
- "district",
22
- "pincode",
23
- "state",
24
- "town",
25
- "source",
26
- ]
27
 
28
 
29
  def authenticate(api_key: str):
@@ -44,7 +30,7 @@ def authenticate(api_key: str):
44
  def root():
45
  return {
46
  "success": True,
47
- "service": "InayatGaming Dataset API"
48
  }
49
 
50
 
@@ -58,52 +44,56 @@ def health():
58
  @app.get("/lookup")
59
  def lookup(
60
  api_key: str = Query(...),
61
- number: str = Query(..., min_length=1)
62
  ):
63
  authenticate(api_key)
64
 
65
- # Normalize the supplied identifier.
66
  number = number.strip()
67
 
68
- con = duckdb.connect()
 
 
 
 
 
 
 
 
 
69
 
70
  try:
71
- query = """
 
72
  SELECT
73
  district,
74
  pincode,
75
  state,
76
  town,
77
  source
78
- FROM read_parquet(
79
- ?,
80
- union_by_name=true
81
- )
82
- WHERE phoneNumber = ?
83
  LIMIT 1
84
- """
85
-
86
- # Query each source file rather than loading the dataset
87
- # into Python memory.
88
- for parquet_file in PARQUET_FILES:
89
- result = con.execute(
90
- query,
91
- [parquet_file, number]
92
- ).fetchone()
93
-
94
- if result:
95
- return {
96
- "success": True,
97
- "match": True,
98
- "data": dict(
99
- zip(PUBLIC_FIELDS, result)
100
- )
101
- }
102
 
103
  return {
104
  "success": True,
105
- "match": False,
106
- "data": None
 
 
 
 
 
 
107
  }
108
 
109
  finally:
 
4
  from fastapi import FastAPI, Query, HTTPException
5
 
6
  app = FastAPI(
7
+ title="InayatGaming Authorized Lookup API",
8
+ version="2.0.0"
9
  )
10
 
11
  API_KEY = os.getenv("API_KEY")
12
+ DB_PATH = os.getenv("DB_PATH", "/data/authorized_index.duckdb")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
 
15
  def authenticate(api_key: str):
 
30
  def root():
31
  return {
32
  "success": True,
33
+ "service": "InayatGaming Authorized Lookup API"
34
  }
35
 
36
 
 
44
  @app.get("/lookup")
45
  def lookup(
46
  api_key: str = Query(...),
47
+ number: str = Query(...)
48
  ):
49
  authenticate(api_key)
50
 
 
51
  number = number.strip()
52
 
53
+ if not number:
54
+ raise HTTPException(
55
+ status_code=400,
56
+ detail="number is required"
57
+ )
58
+
59
+ con = duckdb.connect(
60
+ DB_PATH,
61
+ read_only=True
62
+ )
63
 
64
  try:
65
+ row = con.execute(
66
+ """
67
  SELECT
68
  district,
69
  pincode,
70
  state,
71
  town,
72
  source
73
+ FROM authorized_lookup
74
+ WHERE lookup_key = ?
 
 
 
75
  LIMIT 1
76
+ """,
77
+ [number]
78
+ ).fetchone()
79
+
80
+ if row is None:
81
+ return {
82
+ "success": True,
83
+ "match": False,
84
+ "data": None
85
+ }
 
 
 
 
 
 
 
 
86
 
87
  return {
88
  "success": True,
89
+ "match": True,
90
+ "data": {
91
+ "district": row[0],
92
+ "pincode": row[1],
93
+ "state": row[2],
94
+ "town": row[3],
95
+ "source": row[4]
96
+ }
97
  }
98
 
99
  finally: