share one http session with a timeout for galtinn requests in the verification server
Unreachable or slow Galtinn requests now show an error page instead of a 500 or hanging the request. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
268f593e69
commit
30558eaa45
1 file changed
+25
-5
@@ -1,3 +1,4 @@
|
|||||||
|
import asyncio
|
||||||
import os
|
import os
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
@@ -27,9 +28,11 @@ async def lifespan(app: FastAPI):
|
|||||||
"password": os.environ["DATABASE_PASSWORD"],
|
"password": os.environ["DATABASE_PASSWORD"],
|
||||||
}
|
}
|
||||||
app.state.pool = await asyncpg.create_pool(**credentials)
|
app.state.pool = await asyncpg.create_pool(**credentials)
|
||||||
|
app.state.http = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10))
|
||||||
|
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
await app.state.http.close()
|
||||||
await app.state.pool.close()
|
await app.state.pool.close()
|
||||||
|
|
||||||
|
|
||||||
@@ -77,7 +80,8 @@ async def callback(request: Request, code: str, state: str):
|
|||||||
"redirect_uri": GALTINN_REDIRECT_URI,
|
"redirect_uri": GALTINN_REDIRECT_URI,
|
||||||
"code_verifier": code_verifier,
|
"code_verifier": code_verifier,
|
||||||
}
|
}
|
||||||
async with aiohttp.ClientSession() as session:
|
session: aiohttp.ClientSession = app.state.http
|
||||||
|
try:
|
||||||
async with session.post(f"{GALTINN_API_URL}/oauth/token/", data=payload) as r:
|
async with session.post(f"{GALTINN_API_URL}/oauth/token/", data=payload) as r:
|
||||||
if r.status != 200:
|
if r.status != 200:
|
||||||
return templates.TemplateResponse(
|
return templates.TemplateResponse(
|
||||||
@@ -90,8 +94,7 @@ async def callback(request: Request, code: str, state: str):
|
|||||||
)
|
)
|
||||||
token_data = await r.json()
|
token_data = await r.json()
|
||||||
|
|
||||||
# Get user info from galtinn
|
# Get user info from galtinn
|
||||||
async with aiohttp.ClientSession() as session:
|
|
||||||
async with session.get(
|
async with session.get(
|
||||||
f"{GALTINN_API_URL}/oauth/userinfo/",
|
f"{GALTINN_API_URL}/oauth/userinfo/",
|
||||||
headers={"Authorization": f"Bearer {token_data['access_token']}"},
|
headers={"Authorization": f"Bearer {token_data['access_token']}"},
|
||||||
@@ -107,8 +110,7 @@ async def callback(request: Request, code: str, state: str):
|
|||||||
)
|
)
|
||||||
user = await r.json()
|
user = await r.json()
|
||||||
|
|
||||||
# Enter discord user into galtinn
|
# Enter discord user into galtinn
|
||||||
async with aiohttp.ClientSession() as session:
|
|
||||||
async with session.post(
|
async with session.post(
|
||||||
f"{GALTINN_API_URL}/discordprofiles/",
|
f"{GALTINN_API_URL}/discordprofiles/",
|
||||||
json={"discord_id": discord_id, "user": user["sub"]},
|
json={"discord_id": discord_id, "user": user["sub"]},
|
||||||
@@ -123,6 +125,24 @@ async def callback(request: Request, code: str, state: str):
|
|||||||
},
|
},
|
||||||
status_code=502,
|
status_code=502,
|
||||||
)
|
)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
return templates.TemplateResponse(
|
||||||
|
"error.html",
|
||||||
|
{
|
||||||
|
"request": request,
|
||||||
|
"message": "Fikk ikke svar fra Galtinn i tide. Prøv igjen fra start!",
|
||||||
|
},
|
||||||
|
status_code=504,
|
||||||
|
)
|
||||||
|
except aiohttp.ClientError:
|
||||||
|
return templates.TemplateResponse(
|
||||||
|
"error.html",
|
||||||
|
{
|
||||||
|
"request": request,
|
||||||
|
"message": "Klarte ikke å koble til Galtinn. Prøv igjen fra start!",
|
||||||
|
},
|
||||||
|
status_code=502,
|
||||||
|
)
|
||||||
|
|
||||||
# Notify bot that user is verified
|
# Notify bot that user is verified
|
||||||
# NOTIFY does not accept query arguments, but pg_notify does
|
# NOTIFY does not accept query arguments, but pg_notify does
|
||||||
|
|||||||
Reference in new issue
Block a user