Skip to content

Commit 8696d1b

Browse files
committed
refactor: extract Context in its own file
1 parent 81d40e3 commit 8696d1b

File tree

6 files changed

+173
-171
lines changed

6 files changed

+173
-171
lines changed

scim2_models/base.py

Lines changed: 1 addition & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from collections import UserString
22
from enum import Enum
3-
from enum import auto
43
from inspect import isclass
54
from typing import Annotated
65
from typing import Any
@@ -29,6 +28,7 @@
2928
from typing_extensions import NewType
3029
from typing_extensions import Self
3130

31+
from scim2_models.context import Context
3232
from scim2_models.utils import normalize_attribute_name
3333
from scim2_models.utils import to_camel
3434

@@ -162,172 +162,6 @@ def _validate(cls, input_value: str, /) -> str:
162162
return input_value
163163

164164

165-
class Context(Enum):
166-
"""Represent the different HTTP contexts detailed in :rfc:`RFC7644 §3.2 <7644#section-3.2>`.
167-
168-
Contexts are intended to be used during model validation and serialization.
169-
For instance a client preparing a resource creation POST request can use
170-
:code:`resource.model_dump(Context.RESOURCE_CREATION_REQUEST)` and
171-
the server can then validate it with
172-
:code:`resource.model_validate(Context.RESOURCE_CREATION_REQUEST)`.
173-
"""
174-
175-
DEFAULT = auto()
176-
"""The default context.
177-
178-
All fields are accepted during validation, and all fields are
179-
serialized during a dump.
180-
"""
181-
182-
RESOURCE_CREATION_REQUEST = auto()
183-
"""The resource creation request context.
184-
185-
Should be used for clients building a payload for a resource creation request,
186-
and servers validating resource creation request payloads.
187-
188-
- When used for serialization, it will not dump attributes annotated with :attr:`~scim2_models.Mutability.read_only`.
189-
- When used for validation, it will raise a :class:`~pydantic.ValidationError`:
190-
- when finding attributes annotated with :attr:`~scim2_models.Mutability.read_only`,
191-
- when attributes annotated with :attr:`Required.true <scim2_models.Required.true>` are missing on null.
192-
"""
193-
194-
RESOURCE_CREATION_RESPONSE = auto()
195-
"""The resource creation response context.
196-
197-
Should be used for servers building a payload for a resource
198-
creation response, and clients validating resource creation response
199-
payloads.
200-
201-
- When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Returned.never` or when attributes annotated with :attr:`~scim2_models.Returned.always` are missing or :data:`None`;
202-
- When used for serialization, it will:
203-
- always dump attributes annotated with :attr:`~scim2_models.Returned.always`;
204-
- never dump attributes annotated with :attr:`~scim2_models.Returned.never`;
205-
- dump attributes annotated with :attr:`~scim2_models.Returned.default` unless they are explicitly excluded;
206-
- not dump attributes annotated with :attr:`~scim2_models.Returned.request` unless they are explicitly included.
207-
"""
208-
209-
RESOURCE_QUERY_REQUEST = auto()
210-
"""The resource query request context.
211-
212-
Should be used for clients building a payload for a resource query request,
213-
and servers validating resource query request payloads.
214-
215-
- When used for serialization, it will not dump attributes annotated with :attr:`~scim2_models.Mutability.write_only`.
216-
- When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Mutability.write_only`.
217-
"""
218-
219-
RESOURCE_QUERY_RESPONSE = auto()
220-
"""The resource query response context.
221-
222-
Should be used for servers building a payload for a resource query
223-
response, and clients validating resource query response payloads.
224-
225-
- When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Returned.never` or when attributes annotated with :attr:`~scim2_models.Returned.always` are missing or :data:`None`;
226-
- When used for serialization, it will:
227-
- always dump attributes annotated with :attr:`~scim2_models.Returned.always`;
228-
- never dump attributes annotated with :attr:`~scim2_models.Returned.never`;
229-
- dump attributes annotated with :attr:`~scim2_models.Returned.default` unless they are explicitly excluded;
230-
- not dump attributes annotated with :attr:`~scim2_models.Returned.request` unless they are explicitly included.
231-
"""
232-
233-
RESOURCE_REPLACEMENT_REQUEST = auto()
234-
"""The resource replacement request context.
235-
236-
Should be used for clients building a payload for a resource replacement request,
237-
and servers validating resource replacement request payloads.
238-
239-
- When used for serialization, it will not dump attributes annotated with :attr:`~scim2_models.Mutability.read_only`.
240-
- When used for validation, it will ignore attributes annotated with :attr:`scim2_models.Mutability.read_only` and raise a :class:`~pydantic.ValidationError`:
241-
- when finding attributes annotated with :attr:`~scim2_models.Mutability.immutable` different than :paramref:`~scim2_models.BaseModel.model_validate.original`:
242-
- when attributes annotated with :attr:`Required.true <scim2_models.Required.true>` are missing on null.
243-
"""
244-
245-
RESOURCE_REPLACEMENT_RESPONSE = auto()
246-
"""The resource replacement response context.
247-
248-
Should be used for servers building a payload for a resource
249-
replacement response, and clients validating resource query
250-
replacement payloads.
251-
252-
- When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Returned.never` or when attributes annotated with :attr:`~scim2_models.Returned.always` are missing or :data:`None`;
253-
- When used for serialization, it will:
254-
- always dump attributes annotated with :attr:`~scim2_models.Returned.always`;
255-
- never dump attributes annotated with :attr:`~scim2_models.Returned.never`;
256-
- dump attributes annotated with :attr:`~scim2_models.Returned.default` unless they are explicitly excluded;
257-
- not dump attributes annotated with :attr:`~scim2_models.Returned.request` unless they are explicitly included.
258-
"""
259-
260-
SEARCH_REQUEST = auto()
261-
"""The search request context.
262-
263-
Should be used for clients building a payload for a search request,
264-
and servers validating search request payloads.
265-
266-
- When used for serialization, it will not dump attributes annotated with :attr:`~scim2_models.Mutability.write_only`.
267-
- When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Mutability.write_only`.
268-
"""
269-
270-
SEARCH_RESPONSE = auto()
271-
"""The resource query response context.
272-
273-
Should be used for servers building a payload for a search response,
274-
and clients validating resource search payloads.
275-
276-
- When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Returned.never` or when attributes annotated with :attr:`~scim2_models.Returned.always` are missing or :data:`None`;
277-
- When used for serialization, it will:
278-
- always dump attributes annotated with :attr:`~scim2_models.Returned.always`;
279-
- never dump attributes annotated with :attr:`~scim2_models.Returned.never`;
280-
- dump attributes annotated with :attr:`~scim2_models.Returned.default` unless they are explicitly excluded;
281-
- not dump attributes annotated with :attr:`~scim2_models.Returned.request` unless they are explicitly included.
282-
"""
283-
284-
RESOURCE_PATCH_REQUEST = auto()
285-
"""The resource patch request context.
286-
287-
Should be used for clients building a payload for a PATCH request,
288-
and servers validating PATCH request payloads.
289-
290-
- When used for serialization, it will not dump attributes annotated with :attr:`~scim2_models.Mutability.read_only`.
291-
- When used for validation, it will raise a :class:`~pydantic.ValidationError`:
292-
- when finding attributes annotated with :attr:`~scim2_models.Mutability.read_only`,
293-
- when attributes annotated with :attr:`Required.true <scim2_models.Required.true>` are missing or null.
294-
"""
295-
296-
RESOURCE_PATCH_RESPONSE = auto()
297-
"""The resource patch response context.
298-
299-
Should be used for servers building a payload for a PATCH response,
300-
and clients validating patch response payloads.
301-
302-
- When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Returned.never` or when attributes annotated with :attr:`~scim2_models.Returned.always` are missing or :data:`None`;
303-
- When used for serialization, it will:
304-
- always dump attributes annotated with :attr:`~scim2_models.Returned.always`;
305-
- never dump attributes annotated with :attr:`~scim2_models.Returned.never`;
306-
- dump attributes annotated with :attr:`~scim2_models.Returned.default` unless they are explicitly excluded;
307-
- not dump attributes annotated with :attr:`~scim2_models.Returned.request` unless they are explicitly included.
308-
"""
309-
310-
@classmethod
311-
def is_request(cls, ctx: "Context") -> bool:
312-
return ctx in (
313-
cls.RESOURCE_CREATION_REQUEST,
314-
cls.RESOURCE_QUERY_REQUEST,
315-
cls.RESOURCE_REPLACEMENT_REQUEST,
316-
cls.SEARCH_REQUEST,
317-
cls.RESOURCE_PATCH_REQUEST,
318-
)
319-
320-
@classmethod
321-
def is_response(cls, ctx: "Context") -> bool:
322-
return ctx in (
323-
cls.RESOURCE_CREATION_RESPONSE,
324-
cls.RESOURCE_QUERY_RESPONSE,
325-
cls.RESOURCE_REPLACEMENT_RESPONSE,
326-
cls.SEARCH_RESPONSE,
327-
cls.RESOURCE_PATCH_RESPONSE,
328-
)
329-
330-
331165
class Mutability(str, Enum):
332166
"""A single keyword indicating the circumstances under which the value of the attribute can be (re)defined."""
333167

scim2_models/context.py

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
from enum import Enum
2+
from enum import auto
3+
4+
5+
class Context(Enum):
6+
"""Represent the different HTTP contexts detailed in :rfc:`RFC7644 §3.2 <7644#section-3.2>`.
7+
8+
Contexts are intended to be used during model validation and serialization.
9+
For instance a client preparing a resource creation POST request can use
10+
:code:`resource.model_dump(Context.RESOURCE_CREATION_REQUEST)` and
11+
the server can then validate it with
12+
:code:`resource.model_validate(Context.RESOURCE_CREATION_REQUEST)`.
13+
"""
14+
15+
DEFAULT = auto()
16+
"""The default context.
17+
18+
All fields are accepted during validation, and all fields are
19+
serialized during a dump.
20+
"""
21+
22+
RESOURCE_CREATION_REQUEST = auto()
23+
"""The resource creation request context.
24+
25+
Should be used for clients building a payload for a resource creation request,
26+
and servers validating resource creation request payloads.
27+
28+
- When used for serialization, it will not dump attributes annotated with :attr:`~scim2_models.Mutability.read_only`.
29+
- When used for validation, it will raise a :class:`~pydantic.ValidationError`:
30+
- when finding attributes annotated with :attr:`~scim2_models.Mutability.read_only`,
31+
- when attributes annotated with :attr:`Required.true <scim2_models.Required.true>` are missing on null.
32+
"""
33+
34+
RESOURCE_CREATION_RESPONSE = auto()
35+
"""The resource creation response context.
36+
37+
Should be used for servers building a payload for a resource
38+
creation response, and clients validating resource creation response
39+
payloads.
40+
41+
- When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Returned.never` or when attributes annotated with :attr:`~scim2_models.Returned.always` are missing or :data:`None`;
42+
- When used for serialization, it will:
43+
- always dump attributes annotated with :attr:`~scim2_models.Returned.always`;
44+
- never dump attributes annotated with :attr:`~scim2_models.Returned.never`;
45+
- dump attributes annotated with :attr:`~scim2_models.Returned.default` unless they are explicitly excluded;
46+
- not dump attributes annotated with :attr:`~scim2_models.Returned.request` unless they are explicitly included.
47+
"""
48+
49+
RESOURCE_QUERY_REQUEST = auto()
50+
"""The resource query request context.
51+
52+
Should be used for clients building a payload for a resource query request,
53+
and servers validating resource query request payloads.
54+
55+
- When used for serialization, it will not dump attributes annotated with :attr:`~scim2_models.Mutability.write_only`.
56+
- When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Mutability.write_only`.
57+
"""
58+
59+
RESOURCE_QUERY_RESPONSE = auto()
60+
"""The resource query response context.
61+
62+
Should be used for servers building a payload for a resource query
63+
response, and clients validating resource query response payloads.
64+
65+
- When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Returned.never` or when attributes annotated with :attr:`~scim2_models.Returned.always` are missing or :data:`None`;
66+
- When used for serialization, it will:
67+
- always dump attributes annotated with :attr:`~scim2_models.Returned.always`;
68+
- never dump attributes annotated with :attr:`~scim2_models.Returned.never`;
69+
- dump attributes annotated with :attr:`~scim2_models.Returned.default` unless they are explicitly excluded;
70+
- not dump attributes annotated with :attr:`~scim2_models.Returned.request` unless they are explicitly included.
71+
"""
72+
73+
RESOURCE_REPLACEMENT_REQUEST = auto()
74+
"""The resource replacement request context.
75+
76+
Should be used for clients building a payload for a resource replacement request,
77+
and servers validating resource replacement request payloads.
78+
79+
- When used for serialization, it will not dump attributes annotated with :attr:`~scim2_models.Mutability.read_only`.
80+
- When used for validation, it will ignore attributes annotated with :attr:`scim2_models.Mutability.read_only` and raise a :class:`~pydantic.ValidationError`:
81+
- when finding attributes annotated with :attr:`~scim2_models.Mutability.immutable` different than :paramref:`~scim2_models.BaseModel.model_validate.original`:
82+
- when attributes annotated with :attr:`Required.true <scim2_models.Required.true>` are missing on null.
83+
"""
84+
85+
RESOURCE_REPLACEMENT_RESPONSE = auto()
86+
"""The resource replacement response context.
87+
88+
Should be used for servers building a payload for a resource
89+
replacement response, and clients validating resource query
90+
replacement payloads.
91+
92+
- When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Returned.never` or when attributes annotated with :attr:`~scim2_models.Returned.always` are missing or :data:`None`;
93+
- When used for serialization, it will:
94+
- always dump attributes annotated with :attr:`~scim2_models.Returned.always`;
95+
- never dump attributes annotated with :attr:`~scim2_models.Returned.never`;
96+
- dump attributes annotated with :attr:`~scim2_models.Returned.default` unless they are explicitly excluded;
97+
- not dump attributes annotated with :attr:`~scim2_models.Returned.request` unless they are explicitly included.
98+
"""
99+
100+
SEARCH_REQUEST = auto()
101+
"""The search request context.
102+
103+
Should be used for clients building a payload for a search request,
104+
and servers validating search request payloads.
105+
106+
- When used for serialization, it will not dump attributes annotated with :attr:`~scim2_models.Mutability.write_only`.
107+
- When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Mutability.write_only`.
108+
"""
109+
110+
SEARCH_RESPONSE = auto()
111+
"""The resource query response context.
112+
113+
Should be used for servers building a payload for a search response,
114+
and clients validating resource search payloads.
115+
116+
- When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Returned.never` or when attributes annotated with :attr:`~scim2_models.Returned.always` are missing or :data:`None`;
117+
- When used for serialization, it will:
118+
- always dump attributes annotated with :attr:`~scim2_models.Returned.always`;
119+
- never dump attributes annotated with :attr:`~scim2_models.Returned.never`;
120+
- dump attributes annotated with :attr:`~scim2_models.Returned.default` unless they are explicitly excluded;
121+
- not dump attributes annotated with :attr:`~scim2_models.Returned.request` unless they are explicitly included.
122+
"""
123+
124+
RESOURCE_PATCH_REQUEST = auto()
125+
"""The resource patch request context.
126+
127+
Should be used for clients building a payload for a PATCH request,
128+
and servers validating PATCH request payloads.
129+
130+
- When used for serialization, it will not dump attributes annotated with :attr:`~scim2_models.Mutability.read_only`.
131+
- When used for validation, it will raise a :class:`~pydantic.ValidationError`:
132+
- when finding attributes annotated with :attr:`~scim2_models.Mutability.read_only`,
133+
- when attributes annotated with :attr:`Required.true <scim2_models.Required.true>` are missing or null.
134+
"""
135+
136+
RESOURCE_PATCH_RESPONSE = auto()
137+
"""The resource patch response context.
138+
139+
Should be used for servers building a payload for a PATCH response,
140+
and clients validating patch response payloads.
141+
142+
- When used for validation, it will raise a :class:`~pydantic.ValidationError` when finding attributes annotated with :attr:`~scim2_models.Returned.never` or when attributes annotated with :attr:`~scim2_models.Returned.always` are missing or :data:`None`;
143+
- When used for serialization, it will:
144+
- always dump attributes annotated with :attr:`~scim2_models.Returned.always`;
145+
- never dump attributes annotated with :attr:`~scim2_models.Returned.never`;
146+
- dump attributes annotated with :attr:`~scim2_models.Returned.default` unless they are explicitly excluded;
147+
- not dump attributes annotated with :attr:`~scim2_models.Returned.request` unless they are explicitly included.
148+
"""
149+
150+
@classmethod
151+
def is_request(cls, ctx: "Context") -> bool:
152+
return ctx in (
153+
cls.RESOURCE_CREATION_REQUEST,
154+
cls.RESOURCE_QUERY_REQUEST,
155+
cls.RESOURCE_REPLACEMENT_REQUEST,
156+
cls.SEARCH_REQUEST,
157+
cls.RESOURCE_PATCH_REQUEST,
158+
)
159+
160+
@classmethod
161+
def is_response(cls, ctx: "Context") -> bool:
162+
return ctx in (
163+
cls.RESOURCE_CREATION_RESPONSE,
164+
cls.RESOURCE_QUERY_RESPONSE,
165+
cls.RESOURCE_REPLACEMENT_RESPONSE,
166+
cls.SEARCH_RESPONSE,
167+
cls.RESOURCE_PATCH_RESPONSE,
168+
)

tests/test_dynamic_schemas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
from typing import Annotated
33
from typing import Optional
44

5-
from scim2_models.base import Context
65
from scim2_models.base import Required
6+
from scim2_models.context import Context
77
from scim2_models.rfc7643.enterprise_user import EnterpriseUser
88
from scim2_models.rfc7643.group import Group
99
from scim2_models.rfc7643.resource import Resource

tests/test_model_attributes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
from scim2_models.base import BaseModel
88
from scim2_models.base import ComplexAttribute
9-
from scim2_models.base import Context
109
from scim2_models.base import Required
1110
from scim2_models.base import Returned
1211
from scim2_models.base import validate_attribute_urn
12+
from scim2_models.context import Context
1313
from scim2_models.rfc7643.enterprise_user import EnterpriseUser
1414
from scim2_models.rfc7643.resource import Extension
1515
from scim2_models.rfc7643.resource import Meta

tests/test_model_serialization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import pytest
55

66
from scim2_models.base import ComplexAttribute
7-
from scim2_models.base import Context
87
from scim2_models.base import Mutability
98
from scim2_models.base import Required
109
from scim2_models.base import Returned
10+
from scim2_models.context import Context
1111
from scim2_models.rfc7643.resource import Resource
1212

1313

0 commit comments

Comments
 (0)