Skip to content

Commit cb6373f

Browse files
authored
Adds paginated cache collection (#227)
1 parent 57a5270 commit cb6373f

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

controller/auth/kratos.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,40 @@ def __refresh_identity_cache():
3333
request = requests.get(f"{KRATOS_ADMIN_URL}/identities")
3434
if request.ok:
3535
collected = datetime.now()
36+
identities = request.json()
37+
38+
# maybe more pages https://www.ory.sh/docs/ecosystem/api-design#pagination
39+
while next_link := __get_link_from_kratos_request(request):
40+
request = requests.get(next_link)
41+
if request.ok:
42+
identities.extend(request.json())
43+
3644
KRATOS_IDENTITY_CACHE = {
3745
identity["id"]: {
3846
"identity": identity,
3947
"simple": __parse_identity_to_simple(identity),
4048
}
41-
for identity in request.json()
49+
for identity in identities
4250
}
51+
4352
KRATOS_IDENTITY_CACHE["collected"] = collected
4453
else:
4554
KRATOS_IDENTITY_CACHE = {}
4655

4756

57+
def __get_link_from_kratos_request(request: requests.Response) -> str:
58+
# rel=next only if there is more than 1 page
59+
# </admin/identities?page_size=1&page_token=00000000-0000-0000-0000-000000000000>; rel="first",</admin/identities?page_size=1&page_token=08f30706-9919-4776-9018-6a56c4fa8bb9>; rel="next"
60+
link = request.headers.get("Link")
61+
if link:
62+
if 'rel="next"' in link:
63+
parts = link.split("<")
64+
for part in parts:
65+
if 'rel="next"' in part:
66+
return part.split(">")[0].replace("/admin", KRATOS_ADMIN_URL)
67+
return None
68+
69+
4870
def __get_identity(user_id: str, only_simple: bool = True) -> Dict[str, Any]:
4971
if not isinstance(user_id, str):
5072
user_id = str(user_id)

fast_api/routes/organization.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
from typing import Optional
32
from fastapi import APIRouter, Depends, Request, Body
43
from controller.misc import config_service
54
from fast_api.models import (
@@ -30,7 +29,6 @@
3029
from fast_api.routes.client_response import get_silent_success, pack_json_result
3130
from submodules.model import events
3231
from submodules.model.business_objects import organization
33-
from submodules.model.business_objects.user import get
3432
from submodules.model.util import sql_alchemy_to_dict
3533
from util import doc_ock, notification
3634

0 commit comments

Comments
 (0)