Fix aiohttp
This commit is contained in:
parent
7c19b3f324
commit
814e9184de
1 changed files with 7 additions and 7 deletions
14
ipserv.py
14
ipserv.py
|
@ -14,7 +14,8 @@ logging.basicConfig(level=logging.INFO)
|
||||||
async def lifespan(_app: FastAPI):
|
async def lifespan(_app: FastAPI):
|
||||||
async with aiohttp.ClientSession(
|
async with aiohttp.ClientSession(
|
||||||
"https://ip.shronk.tech",
|
"https://ip.shronk.tech",
|
||||||
connector=aiohttp.TCPConnector(limit=2048)
|
connector=aiohttp.TCPConnector(limit=2048),
|
||||||
|
raise_for_status=True
|
||||||
) as client:
|
) as client:
|
||||||
_app.state.session = client
|
_app.state.session = client
|
||||||
yield
|
yield
|
||||||
|
@ -50,19 +51,18 @@ async def ip(
|
||||||
logging.info("looking up IP info for %s", ip)
|
logging.info("looking up IP info for %s", ip)
|
||||||
try:
|
try:
|
||||||
async with app.state.session.get(f"/lookup?ip={ip}") as response:
|
async with app.state.session.get(f"/lookup?ip={ip}") as response:
|
||||||
response.raise_for_status()
|
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
except Exception as e:
|
|
||||||
logging.error("Failed to get data for %s: %s", ip, e, exc_info=True)
|
|
||||||
raise HTTPException(500, "Failed to get upstream data.")
|
|
||||||
except json.JSONDecodeError as e:
|
except json.JSONDecodeError as e:
|
||||||
logging.error("Failed to parse data for %s: %s", ip, e, exc_info=True)
|
logging.error("Failed to parse data for %s: %s", ip, e, exc_info=True)
|
||||||
raise HTTPException(500, "Failed to parse upstream response.")
|
raise HTTPException(500, "Failed to parse upstream response.")
|
||||||
|
except Exception as e:
|
||||||
|
logging.error("Failed to get data for %s: %s", ip, e, exc_info=True)
|
||||||
|
raise HTTPException(500, "Failed to get upstream data.")
|
||||||
logging.info("%s -> %r", ip, data)
|
logging.info("%s -> %r", ip, data)
|
||||||
data["ip"] = ip
|
data["ip"] = ip
|
||||||
data.pop("legalese", None)
|
data.pop("legalese", None)
|
||||||
data.pop("source", None)
|
data.pop("source", None)
|
||||||
data.pop("brexitRequired", None)
|
data.pop("brexitRequired", None)
|
||||||
if response.status_code == 200:
|
if response.status == 200:
|
||||||
app.state.cache[ip] = [data, time.time()]
|
app.state.cache[ip] = [data, time.time()]
|
||||||
return JSONResponse(data, response.status_code)
|
return JSONResponse(data, response.status)
|
||||||
|
|
Reference in a new issue