Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Query, HTTPException
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = FastAPI(
|
| 7 |
+
title="API",
|
| 8 |
+
description="API",
|
| 9 |
+
version="1.0.0"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
# Replace this with an API you own or are authorized to proxy.
|
| 13 |
+
UPSTREAM_URL = os.getenv( "API" )
|
| 14 |
+
|
| 15 |
+
REQUEST_TIMEOUT = 20
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
@app.get("/")
|
| 19 |
+
def home():
|
| 20 |
+
return {
|
| 21 |
+
"status": True,
|
| 22 |
+
"message": "API is running"
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@app.get("/api")
|
| 27 |
+
def lookup(
|
| 28 |
+
num: str = Query(..., min_length=1)
|
| 29 |
+
):
|
| 30 |
+
try:
|
| 31 |
+
response = requests.get(
|
| 32 |
+
UPSTREAM_URL,
|
| 33 |
+
params={"num": num},
|
| 34 |
+
timeout=REQUEST_TIMEOUT,
|
| 35 |
+
headers={
|
| 36 |
+
"Accept": "application/json",
|
| 37 |
+
"User-Agent": "Mozilla/5.0"
|
| 38 |
+
}
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
response.raise_for_status()
|
| 42 |
+
|
| 43 |
+
result = response.json()
|
| 44 |
+
|
| 45 |
+
except requests.RequestException:
|
| 46 |
+
raise HTTPException(
|
| 47 |
+
status_code=502,
|
| 48 |
+
detail="Request failed"
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
except ValueError:
|
| 52 |
+
raise HTTPException(
|
| 53 |
+
status_code=502,
|
| 54 |
+
detail="Request failed"
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# -----------------------------------------
|
| 58 |
+
# NO DATA RESPONSE
|
| 59 |
+
# -----------------------------------------
|
| 60 |
+
if result.get("success") is False:
|
| 61 |
+
return JSONResponse(
|
| 62 |
+
content={
|
| 63 |
+
"Main_Records": None,
|
| 64 |
+
"Alt_Records": None
|
| 65 |
+
}
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# -----------------------------------------
|
| 69 |
+
# SUCCESS RESPONSE
|
| 70 |
+
# -----------------------------------------
|
| 71 |
+
data = result.get("data", {})
|
| 72 |
+
inner_data = data.get("Data", {})
|
| 73 |
+
|
| 74 |
+
return JSONResponse(
|
| 75 |
+
content={
|
| 76 |
+
"Main_Records": inner_data.get("Main_Records"),
|
| 77 |
+
"Alt_Records": inner_data.get("Alt_Records")
|
| 78 |
+
}
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
import uvicorn
|
| 84 |
+
|
| 85 |
+
uvicorn.run(
|
| 86 |
+
app,
|
| 87 |
+
host="0.0.0.0",
|
| 88 |
+
port=7860
|
| 89 |
+
)
|