Skip to content

Commit 751cf2b

Browse files
Roddygithubroddy
andauthored
feat(api): autoclosed DB sessions + fights selectinload + webhooks pagination (#68)
* feat(api): auto-commit get_sessions on success, rollback on exception Signed-off-by: Roddy GitHub <roddy@users.noreply.github.com> * feat(api): preload agents + skills on GET /fights (closes list N+1) Signed-off-by: Roddy GitHub <roddy@users.noreply.github.com> * feat(api): add limit/offset pagination to GET /webhooks Signed-off-by: Roddy GitHub <roddy@users.noreply.github.com> * chore(web): regen OpenAPI schema.d.ts for webhooks pagination Signed-off-by: Roddy GitHub <roddy@users.noreply.github.com> --------- Signed-off-by: Roddy GitHub <roddy@users.noreply.github.com> Co-authored-by: Roddy GitHub <roddy@users.noreply.github.com>
1 parent 36e522d commit 751cf2b

4 files changed

Lines changed: 28 additions & 3 deletions

File tree

‎apps/api/src/gw2analytics_api/database.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,9 @@ def get_session() -> Iterator[Session]:
115115
session = get_sessionmaker()()
116116
try:
117117
yield session
118+
session.commit()
119+
except BaseException:
120+
session.rollback()
121+
raise
118122
finally:
119123
session.close()

‎apps/api/src/gw2analytics_api/routes/fights/__init__.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,11 @@ def list_fights(
200200
"""
201201
rows = (
202202
db.execute(
203-
select(OrmFight).order_by(OrmFight.started_at.desc()).limit(limit).offset(offset),
203+
select(OrmFight)
204+
.options(selectinload(OrmFight.agents), selectinload(OrmFight.skills))
205+
.order_by(OrmFight.started_at.desc())
206+
.limit(limit)
207+
.offset(offset),
204208
)
205209
.scalars()
206210
.all()

‎apps/api/src/gw2analytics_api/routes/webhooks.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,17 @@ def create_webhook(
284284
@limiter.limit("30/minute")
285285
def list_webhooks(
286286
request: Request, # noqa: ARG001
287+
limit: int = Query(100, ge=1, le=1000),
288+
offset: int = Query(0, ge=0),
287289
db: Session = Depends(get_session), # noqa: B008
288290
) -> list[WebhookSubscriptionOut]:
289291
"""List active (non-revoked) webhook subscriptions. Secrets never returned."""
290292
rows = (
291293
db.execute(
292-
select(OrmWebhookSubscription).where(OrmWebhookSubscription.revoked_at.is_(None))
294+
select(OrmWebhookSubscription)
295+
.where(OrmWebhookSubscription.revoked_at.is_(None))
296+
.limit(limit)
297+
.offset(offset),
293298
)
294299
.scalars()
295300
.all()

‎web/src/lib/api/schema.d.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2897,7 +2897,10 @@ export interface operations {
28972897
};
28982898
list_webhooks_api_v1_webhooks_get: {
28992899
parameters: {
2900-
query?: never;
2900+
query?: {
2901+
limit?: number;
2902+
offset?: number;
2903+
};
29012904
header?: never;
29022905
path?: never;
29032906
cookie?: never;
@@ -2913,6 +2916,15 @@ export interface operations {
29132916
"application/json": components["schemas"]["WebhookSubscriptionOut"][];
29142917
};
29152918
};
2919+
/** @description Validation Error */
2920+
422: {
2921+
headers: {
2922+
[name: string]: unknown;
2923+
};
2924+
content: {
2925+
"application/json": components["schemas"]["HTTPValidationError"];
2926+
};
2927+
};
29162928
};
29172929
};
29182930
create_webhook_api_v1_webhooks_post: {

0 commit comments

Comments
 (0)