Skip to content

Commit 4ba3355

Browse files
committed
doc: suppose that database perform pagination themlselves
1 parent 88775a8 commit 4ba3355

File tree

3 files changed

+99
-61
lines changed

3 files changed

+99
-61
lines changed

doc/guides/_examples/django_example.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
from .integrations import delete_record
2424
from .integrations import from_scim_user
2525
from .integrations import get_record
26+
from .integrations import get_resource_type
2627
from .integrations import get_resource_types
28+
from .integrations import get_schema
2729
from .integrations import get_schemas
2830
from .integrations import list_records
2931
from .integrations import save_record
@@ -170,11 +172,10 @@ def get(self, request):
170172
except ValidationError as error:
171173
return scim_validation_error(error)
172174

173-
all_records = list_records()
174-
page = all_records[req.start_index_0 : req.stop_index_0]
175+
total, page = list_records(req.start_index_0, req.stop_index_0)
175176
resources = [to_scim_user(record) for record in page]
176177
response = ListResponse[User](
177-
total_results=len(all_records),
178+
total_results=total,
178179
start_index=req.start_index or 1,
179180
items_per_page=len(resources),
180181
resources=resources,
@@ -227,10 +228,9 @@ def get(self, request):
227228
except ValidationError as error:
228229
return scim_validation_error(error)
229230

230-
all_schemas = get_schemas()
231-
page = all_schemas[req.start_index_0 : req.stop_index_0]
231+
total, page = get_schemas(req.start_index_0, req.stop_index_0)
232232
response = ListResponse[Schema](
233-
total_results=len(all_schemas),
233+
total_results=total,
234234
start_index=req.start_index or 1,
235235
items_per_page=len(page),
236236
resources=page,
@@ -244,13 +244,14 @@ class SchemaView(View):
244244
"""Handle GET on a single SCIM schema."""
245245

246246
def get(self, request, schema_id):
247-
for schema in get_schemas():
248-
if schema.id == schema_id:
249-
return scim_response(
250-
schema.model_dump_json(scim_ctx=Context.RESOURCE_QUERY_RESPONSE)
251-
)
252-
scim_error = Error(status=404, detail=f"Schema {schema_id!r} not found")
253-
return scim_response(scim_error.model_dump_json(), HTTPStatus.NOT_FOUND)
247+
try:
248+
schema = get_schema(schema_id)
249+
except KeyError:
250+
scim_error = Error(status=404, detail=f"Schema {schema_id!r} not found")
251+
return scim_response(scim_error.model_dump_json(), HTTPStatus.NOT_FOUND)
252+
return scim_response(
253+
schema.model_dump_json(scim_ctx=Context.RESOURCE_QUERY_RESPONSE)
254+
)
254255
# -- schemas-end --
255256

256257

@@ -264,10 +265,9 @@ def get(self, request):
264265
except ValidationError as error:
265266
return scim_validation_error(error)
266267

267-
all_resource_types = get_resource_types()
268-
page = all_resource_types[req.start_index_0 : req.stop_index_0]
268+
total, page = get_resource_types(req.start_index_0, req.stop_index_0)
269269
response = ListResponse[ResourceType](
270-
total_results=len(all_resource_types),
270+
total_results=total,
271271
start_index=req.start_index or 1,
272272
items_per_page=len(page),
273273
resources=page,
@@ -281,15 +281,16 @@ class ResourceTypeView(View):
281281
"""Handle GET on a single SCIM resource type."""
282282

283283
def get(self, request, resource_type_id):
284-
for rt in get_resource_types():
285-
if rt.id == resource_type_id:
286-
return scim_response(
287-
rt.model_dump_json(scim_ctx=Context.RESOURCE_QUERY_RESPONSE)
288-
)
289-
scim_error = Error(
290-
status=404, detail=f"ResourceType {resource_type_id!r} not found"
284+
try:
285+
rt = get_resource_type(resource_type_id)
286+
except KeyError:
287+
scim_error = Error(
288+
status=404, detail=f"ResourceType {resource_type_id!r} not found"
289+
)
290+
return scim_response(scim_error.model_dump_json(), HTTPStatus.NOT_FOUND)
291+
return scim_response(
292+
rt.model_dump_json(scim_ctx=Context.RESOURCE_QUERY_RESPONSE)
291293
)
292-
return scim_response(scim_error.model_dump_json(), HTTPStatus.NOT_FOUND)
293294
# -- resource-types-end --
294295

295296

doc/guides/_examples/flask_example.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
from .integrations import delete_record
2121
from .integrations import from_scim_user
2222
from .integrations import get_record
23+
from .integrations import get_resource_type
2324
from .integrations import get_resource_types
25+
from .integrations import get_schema
2426
from .integrations import get_schemas
2527
from .integrations import list_records
2628
from .integrations import save_record
@@ -165,11 +167,10 @@ def delete_user(app_record):
165167
def list_users():
166168
"""Return one page of users as a SCIM ListResponse."""
167169
req = SearchRequest.model_validate(request.args.to_dict())
168-
all_records = list_records()
169-
page = all_records[req.start_index_0 : req.stop_index_0]
170+
total, page = list_records(req.start_index_0, req.stop_index_0)
170171
resources = [to_scim_user(record) for record in page]
171172
response = ListResponse[User](
172-
total_results=len(all_records),
173+
total_results=total,
173174
start_index=req.start_index or 1,
174175
items_per_page=len(resources),
175176
resources=resources,
@@ -211,10 +212,9 @@ def create_user():
211212
def list_schemas():
212213
"""Return one page of SCIM schemas the server exposes."""
213214
req = SearchRequest.model_validate(request.args.to_dict())
214-
all_schemas = get_schemas()
215-
page = all_schemas[req.start_index_0 : req.stop_index_0]
215+
total, page = get_schemas(req.start_index_0, req.stop_index_0)
216216
response = ListResponse[Schema](
217-
total_results=len(all_schemas),
217+
total_results=total,
218218
start_index=req.start_index or 1,
219219
items_per_page=len(page),
220220
resources=page,
@@ -226,16 +226,17 @@ def list_schemas():
226226

227227

228228
@bp.get("/Schemas/<path:schema_id>")
229-
def get_schema(schema_id):
229+
def get_schema_by_id(schema_id):
230230
"""Return one SCIM schema by its URI identifier."""
231-
for schema in get_schemas():
232-
if schema.id == schema_id:
233-
return (
234-
schema.model_dump_json(scim_ctx=Context.RESOURCE_QUERY_RESPONSE),
235-
HTTPStatus.OK,
236-
)
237-
scim_error = Error(status=404, detail=f"Schema {schema_id!r} not found")
238-
return scim_error.model_dump_json(), HTTPStatus.NOT_FOUND
231+
try:
232+
schema = get_schema(schema_id)
233+
except KeyError:
234+
scim_error = Error(status=404, detail=f"Schema {schema_id!r} not found")
235+
return scim_error.model_dump_json(), HTTPStatus.NOT_FOUND
236+
return (
237+
schema.model_dump_json(scim_ctx=Context.RESOURCE_QUERY_RESPONSE),
238+
HTTPStatus.OK,
239+
)
239240
# -- schemas-end --
240241

241242

@@ -244,10 +245,9 @@ def get_schema(schema_id):
244245
def list_resource_types():
245246
"""Return one page of SCIM resource types the server exposes."""
246247
req = SearchRequest.model_validate(request.args.to_dict())
247-
all_resource_types = get_resource_types()
248-
page = all_resource_types[req.start_index_0 : req.stop_index_0]
248+
total, page = get_resource_types(req.start_index_0, req.stop_index_0)
249249
response = ListResponse[ResourceType](
250-
total_results=len(all_resource_types),
250+
total_results=total,
251251
start_index=req.start_index or 1,
252252
items_per_page=len(page),
253253
resources=page,
@@ -259,18 +259,19 @@ def list_resource_types():
259259

260260

261261
@bp.get("/ResourceTypes/<resource_type_id>")
262-
def get_resource_type(resource_type_id):
262+
def get_resource_type_by_id(resource_type_id):
263263
"""Return one SCIM resource type by its identifier."""
264-
for rt in get_resource_types():
265-
if rt.id == resource_type_id:
266-
return (
267-
rt.model_dump_json(scim_ctx=Context.RESOURCE_QUERY_RESPONSE),
268-
HTTPStatus.OK,
269-
)
270-
scim_error = Error(
271-
status=404, detail=f"ResourceType {resource_type_id!r} not found"
264+
try:
265+
rt = get_resource_type(resource_type_id)
266+
except KeyError:
267+
scim_error = Error(
268+
status=404, detail=f"ResourceType {resource_type_id!r} not found"
269+
)
270+
return scim_error.model_dump_json(), HTTPStatus.NOT_FOUND
271+
return (
272+
rt.model_dump_json(scim_ctx=Context.RESOURCE_QUERY_RESPONSE),
273+
HTTPStatus.OK,
272274
)
273-
return scim_error.model_dump_json(), HTTPStatus.NOT_FOUND
274275
# -- resource-types-end --
275276

276277

doc/guides/_examples/integrations.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,15 @@ def get_record(record_id):
2525
return records[record_id]
2626

2727

28-
def list_records():
29-
"""Return all stored records as a list."""
30-
return list(records.values())
28+
def list_records(start=None, stop=None):
29+
"""Return a page of stored records and the total count.
30+
31+
:param start: 0-based start index.
32+
:param stop: 0-based stop index (exclusive).
33+
:return: A ``(total, page)`` tuple.
34+
"""
35+
all_records = list(records.values())
36+
return len(all_records), all_records[start:stop]
3137

3238

3339
def save_record(record):
@@ -73,14 +79,44 @@ def from_scim_user(scim_user):
7379
RESOURCE_MODELS = [User]
7480

7581

76-
def get_schemas():
77-
"""Return a :class:`~scim2_models.Schema` for every resource the server exposes."""
78-
return [model.to_schema() for model in RESOURCE_MODELS]
82+
def get_schemas(start=None, stop=None):
83+
"""Return a page of :class:`~scim2_models.Schema` and the total count.
84+
85+
:param start: 0-based start index.
86+
:param stop: 0-based stop index (exclusive).
87+
:return: A ``(total, page)`` tuple.
88+
"""
89+
all_schemas = [model.to_schema() for model in RESOURCE_MODELS]
90+
return len(all_schemas), all_schemas[start:stop]
91+
92+
93+
def get_schema(schema_id):
94+
"""Return the :class:`~scim2_models.Schema` matching *schema_id*, or raise KeyError."""
95+
for model in RESOURCE_MODELS:
96+
schema = model.to_schema()
97+
if schema.id == schema_id:
98+
return schema
99+
raise KeyError(schema_id)
100+
101+
102+
def get_resource_types(start=None, stop=None):
103+
"""Return a page of :class:`~scim2_models.ResourceType` and the total count.
104+
105+
:param start: 0-based start index.
106+
:param stop: 0-based stop index (exclusive).
107+
:return: A ``(total, page)`` tuple.
108+
"""
109+
all_resource_types = [ResourceType.from_resource(model) for model in RESOURCE_MODELS]
110+
return len(all_resource_types), all_resource_types[start:stop]
79111

80112

81-
def get_resource_types():
82-
"""Return a :class:`~scim2_models.ResourceType` for every resource the server exposes."""
83-
return [ResourceType.from_resource(model) for model in RESOURCE_MODELS]
113+
def get_resource_type(resource_type_id):
114+
"""Return the :class:`~scim2_models.ResourceType` matching *resource_type_id*, or raise KeyError."""
115+
for model in RESOURCE_MODELS:
116+
rt = ResourceType.from_resource(model)
117+
if rt.id == resource_type_id:
118+
return rt
119+
raise KeyError(resource_type_id)
84120

85121

86122
service_provider_config = ServiceProviderConfig(

0 commit comments

Comments
 (0)