Skip to content

Commit e0637c5

Browse files
committed
doc: improve FastAPI SCIMResponse example
1 parent 441c2e2 commit e0637c5

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

doc/guides/_examples/fastapi_example.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
from http import HTTPStatus
32
from typing import Annotated
43
from typing import Any
@@ -10,6 +9,7 @@
109
from fastapi import Query
1110
from fastapi import Request
1211
from fastapi import Response
12+
from fastapi.responses import JSONResponse
1313
from pydantic import ValidationError
1414

1515
from scim2_models import Context
@@ -45,19 +45,16 @@
4545
app = FastAPI()
4646

4747

48-
class SCIMResponse(Response):
48+
class SCIMResponse(JSONResponse):
4949
"""SCIM JSON response that auto-extracts the ``ETag`` from ``meta.version``."""
5050

5151
media_type = "application/scim+json"
5252

53-
def render(self, content: Any) -> bytes:
54-
self._etag = (content or {}).get("meta", {}).get("version")
55-
return json.dumps(content, ensure_ascii=False).encode("utf-8")
56-
5753
def __init__(self, content: Any = None, **kwargs: Any) -> None:
5854
super().__init__(content, **kwargs)
59-
if self._etag:
60-
self.headers["ETag"] = self._etag
55+
if meta := (content or {}).get("meta", {}):
56+
if version := meta.get("version"):
57+
self.headers["ETag"] = version
6158

6259

6360
router = APIRouter(prefix="/scim/v2", default_response_class=SCIMResponse)

0 commit comments

Comments
 (0)