Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,26 +4,12 @@ import duckdb
|
|
| 4 |
from fastapi import FastAPI, Query, HTTPException
|
| 5 |
|
| 6 |
app = FastAPI(
|
| 7 |
-
title="InayatGaming
|
| 8 |
-
version="
|
| 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
|
| 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(...
|
| 62 |
):
|
| 63 |
authenticate(api_key)
|
| 64 |
|
| 65 |
-
# Normalize the supplied identifier.
|
| 66 |
number = number.strip()
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
try:
|
| 71 |
-
|
|
|
|
| 72 |
SELECT
|
| 73 |
district,
|
| 74 |
pincode,
|
| 75 |
state,
|
| 76 |
town,
|
| 77 |
source
|
| 78 |
-
FROM
|
| 79 |
-
|
| 80 |
-
union_by_name=true
|
| 81 |
-
)
|
| 82 |
-
WHERE phoneNumber = ?
|
| 83 |
LIMIT 1
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 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":
|
| 106 |
-
"data":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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:
|