rn-forge-web 0.3.0.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of rn-forge-web might be problematic. Click here for more details.
- rn_forge/web/__init__.py +367 -0
- rn_forge/web/asgi.py +147 -0
- rn_forge/web/auth.py +115 -0
- rn_forge/web/concurrency.py +251 -0
- rn_forge/web/conformance/__init__.py +33 -0
- rn_forge/web/conformance/cases.py +1890 -0
- rn_forge/web/conformance/types.py +134 -0
- rn_forge/web/deprecation.py +37 -0
- rn_forge/web/exceptions.py +386 -0
- rn_forge/web/health.py +239 -0
- rn_forge/web/idempotency.py +296 -0
- rn_forge/web/merge_patch.py +98 -0
- rn_forge/web/models.py +24 -0
- rn_forge/web/openapi.py +176 -0
- rn_forge/web/operations.py +54 -0
- rn_forge/web/pagination.py +253 -0
- rn_forge/web/problem.py +593 -0
- rn_forge/web/py.typed +0 -0
- rn_forge/web/read_mask.py +121 -0
- rn_forge/web/security.py +97 -0
- rn_forge/web/soft_delete.py +31 -0
- rn_forge/web/tracing.py +53 -0
- rn_forge/web/transfer.py +408 -0
- rn_forge_web-0.3.0.dev0.dist-info/METADATA +139 -0
- rn_forge_web-0.3.0.dev0.dist-info/RECORD +27 -0
- rn_forge_web-0.3.0.dev0.dist-info/WHEEL +4 -0
- rn_forge_web-0.3.0.dev0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
"""ETag codecs and ``If-Match`` checks for optimistic concurrency.
|
|
2
|
+
|
|
3
|
+
Version mismatches produce 412, required missing preconditions produce 428,
|
|
4
|
+
and malformed validators produce 400. Version codecs accept weak validators
|
|
5
|
+
and ``If-Match: *`` only.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import re
|
|
11
|
+
from typing import TYPE_CHECKING, Final, Protocol, runtime_checkable
|
|
12
|
+
|
|
13
|
+
from rn_forge.web.exceptions import (
|
|
14
|
+
MalformedPrecondition,
|
|
15
|
+
PreconditionRequired,
|
|
16
|
+
VersionConflict,
|
|
17
|
+
)
|
|
18
|
+
from rn_forge.web.problem import field_error
|
|
19
|
+
|
|
20
|
+
if TYPE_CHECKING:
|
|
21
|
+
from rn_forge.web.transfer import BatchUpdateItem
|
|
22
|
+
|
|
23
|
+
__all__ = [
|
|
24
|
+
"ANY_ETAG",
|
|
25
|
+
"ETagCodec",
|
|
26
|
+
"EntityVersionETagCodec",
|
|
27
|
+
"VersionETagCodec",
|
|
28
|
+
"check_item_precondition",
|
|
29
|
+
"check_precondition",
|
|
30
|
+
"is_not_modified",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
ANY_ETAG: Final = "*"
|
|
34
|
+
"""``If-Match: *`` — matches any current representation (RFC 9110 §13.1.1)."""
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@runtime_checkable
|
|
38
|
+
class ETagCodec(Protocol):
|
|
39
|
+
"""Formats and parses the ETag validator carried in ``If-Match``."""
|
|
40
|
+
|
|
41
|
+
def format(self, *, entity_id: object, version: int) -> str:
|
|
42
|
+
"""Return the ETag validator for an entity at a version."""
|
|
43
|
+
...
|
|
44
|
+
|
|
45
|
+
def parse(self, raw: str) -> tuple[str | None, int]:
|
|
46
|
+
"""Parse a validator into ``(entity_id or None, version)``.
|
|
47
|
+
|
|
48
|
+
Raises:
|
|
49
|
+
MalformedPrecondition: *raw* is not a validator this codec emits.
|
|
50
|
+
"""
|
|
51
|
+
...
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
_VERSION_RE: Final = re.compile(r'^W/"(?P<version>\d+)"$')
|
|
55
|
+
_ENTITY_VERSION_RE: Final = re.compile(r'^W/"(?P<entity>[^:"]+):(?P<version>\d+)"$')
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class VersionETagCodec:
|
|
59
|
+
"""``W/"<version>"`` — carries no entity identity.
|
|
60
|
+
|
|
61
|
+
For models without a stable public id to put in the validator. It cannot
|
|
62
|
+
catch a precondition replayed from a different entity; prefer
|
|
63
|
+
:class:`EntityVersionETagCodec` where an id is available.
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
def format(self, *, entity_id: object, version: int) -> str:
|
|
67
|
+
"""Return ``W/"<version>"``. *entity_id* is accepted and ignored."""
|
|
68
|
+
del entity_id
|
|
69
|
+
return f'W/"{version}"'
|
|
70
|
+
|
|
71
|
+
def parse(self, raw: str) -> tuple[str | None, int]:
|
|
72
|
+
"""Parse ``W/"<version>"`` into ``(None, version)``."""
|
|
73
|
+
match = _VERSION_RE.match(raw.strip())
|
|
74
|
+
if match is None:
|
|
75
|
+
raise MalformedPrecondition(
|
|
76
|
+
"Malformed If-Match validator: {}", raw, error_code=400
|
|
77
|
+
)
|
|
78
|
+
return None, int(match["version"])
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class EntityVersionETagCodec:
|
|
82
|
+
"""``W/"<entity_id>:<version>"`` — the default.
|
|
83
|
+
|
|
84
|
+
Carrying the entity id catches a client replaying a precondition from a
|
|
85
|
+
*different* entity, which the version alone cannot.
|
|
86
|
+
|
|
87
|
+
The entity segment is deliberately **any run of characters that is not a
|
|
88
|
+
colon or a quote**, not a UUID pattern: integer and slug primary keys are
|
|
89
|
+
both real, and ``rn_forge.django.BaseModel`` does not have a UUID pk. That
|
|
90
|
+
relaxation is intentional.
|
|
91
|
+
"""
|
|
92
|
+
|
|
93
|
+
def format(self, *, entity_id: object, version: int) -> str:
|
|
94
|
+
"""Return ``W/"<entity_id>:<version>"``.
|
|
95
|
+
|
|
96
|
+
Raises:
|
|
97
|
+
MalformedPrecondition: *entity_id* renders with a ``:`` or ``"``,
|
|
98
|
+
which would produce a validator this codec cannot parse back.
|
|
99
|
+
"""
|
|
100
|
+
rendered = str(entity_id)
|
|
101
|
+
if ":" in rendered or '"' in rendered:
|
|
102
|
+
raise MalformedPrecondition(
|
|
103
|
+
"Entity id {!r} cannot appear in an ETag validator", rendered
|
|
104
|
+
)
|
|
105
|
+
return f'W/"{rendered}:{version}"'
|
|
106
|
+
|
|
107
|
+
def parse(self, raw: str) -> tuple[str | None, int]:
|
|
108
|
+
"""Parse ``W/"<entity_id>:<version>"`` into ``(entity_id, version)``."""
|
|
109
|
+
match = _ENTITY_VERSION_RE.match(raw.strip())
|
|
110
|
+
if match is None:
|
|
111
|
+
raise MalformedPrecondition(
|
|
112
|
+
"Malformed If-Match validator: {}", raw, error_code=400
|
|
113
|
+
)
|
|
114
|
+
return match["entity"], int(match["version"])
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
DEFAULT_CODEC: Final[ETagCodec] = EntityVersionETagCodec()
|
|
118
|
+
"""The codec :func:`check_precondition` uses when the caller names none."""
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def check_precondition(
|
|
122
|
+
raw_if_match: str | None,
|
|
123
|
+
*,
|
|
124
|
+
current_version: int,
|
|
125
|
+
entity_id: object | None = None,
|
|
126
|
+
codec: ETagCodec = DEFAULT_CODEC,
|
|
127
|
+
required: bool = False,
|
|
128
|
+
) -> None:
|
|
129
|
+
"""Raise unless the client's precondition matches the current state.
|
|
130
|
+
|
|
131
|
+
Deliberately strict about the validator's syntax: anything it cannot parse
|
|
132
|
+
is a 400, never a crash.
|
|
133
|
+
|
|
134
|
+
Args:
|
|
135
|
+
raw_if_match: The raw ``If-Match`` header value, or ``None``.
|
|
136
|
+
current_version: The entity's current version.
|
|
137
|
+
entity_id: The entity's id, compared when the codec carries one.
|
|
138
|
+
codec: The validator format. Defaults to :class:`EntityVersionETagCodec`.
|
|
139
|
+
required: When ``True``, an absent header is an error. Making
|
|
140
|
+
preconditions mandatory is the caller's choice, per route.
|
|
141
|
+
|
|
142
|
+
Raises:
|
|
143
|
+
PreconditionRequired: *raw_if_match* is ``None`` and *required*.
|
|
144
|
+
MalformedPrecondition: The header is present but unparseable.
|
|
145
|
+
VersionConflict: The version, or the entity id, does not match.
|
|
146
|
+
"""
|
|
147
|
+
if raw_if_match is None:
|
|
148
|
+
if required:
|
|
149
|
+
raise PreconditionRequired(
|
|
150
|
+
"This endpoint requires an If-Match precondition", error_code=428
|
|
151
|
+
)
|
|
152
|
+
return
|
|
153
|
+
|
|
154
|
+
candidate = raw_if_match.strip()
|
|
155
|
+
if candidate == ANY_ETAG:
|
|
156
|
+
# RFC 9110: `*` matches any current representation. Both surveyed
|
|
157
|
+
# implementations got this wrong; it costs two lines.
|
|
158
|
+
return
|
|
159
|
+
|
|
160
|
+
parsed_entity, parsed_version = codec.parse(candidate)
|
|
161
|
+
if parsed_version != current_version:
|
|
162
|
+
raise VersionConflict(
|
|
163
|
+
"Precondition failed: expected version {}, got {}",
|
|
164
|
+
current_version,
|
|
165
|
+
parsed_version,
|
|
166
|
+
error_code=412,
|
|
167
|
+
)
|
|
168
|
+
if (
|
|
169
|
+
parsed_entity is not None
|
|
170
|
+
and entity_id is not None
|
|
171
|
+
and parsed_entity != str(entity_id)
|
|
172
|
+
):
|
|
173
|
+
raise VersionConflict(
|
|
174
|
+
"Precondition failed: validator is for entity {}, not {}",
|
|
175
|
+
parsed_entity,
|
|
176
|
+
entity_id,
|
|
177
|
+
error_code=412,
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def check_item_precondition(
|
|
182
|
+
item: BatchUpdateItem,
|
|
183
|
+
*,
|
|
184
|
+
current_version: int,
|
|
185
|
+
entity_id: object,
|
|
186
|
+
codec: ETagCodec,
|
|
187
|
+
required: bool,
|
|
188
|
+
) -> None:
|
|
189
|
+
"""Raise unless a ``:batchUpdate`` item's ``ifMatch`` matches the current state.
|
|
190
|
+
|
|
191
|
+
Checks ``item.if_match`` as :func:`check_precondition` checks an
|
|
192
|
+
``If-Match`` header, and raises the same exception for the same input,
|
|
193
|
+
with an ``errors`` entry pointing at ``/requests/<index>/ifMatch``.
|
|
194
|
+
|
|
195
|
+
Args:
|
|
196
|
+
item: The item to check.
|
|
197
|
+
current_version: The entity's current version.
|
|
198
|
+
entity_id: The entity's id, compared when the codec carries one.
|
|
199
|
+
codec: The validator format.
|
|
200
|
+
required: When ``True``, an item without ``ifMatch`` is an error.
|
|
201
|
+
|
|
202
|
+
Raises:
|
|
203
|
+
PreconditionRequired: The item has no ``ifMatch`` and *required*.
|
|
204
|
+
MalformedPrecondition: The ``ifMatch`` is unparseable.
|
|
205
|
+
VersionConflict: The version, or the entity id, does not match.
|
|
206
|
+
"""
|
|
207
|
+
try:
|
|
208
|
+
check_precondition(
|
|
209
|
+
item.if_match,
|
|
210
|
+
current_version=current_version,
|
|
211
|
+
entity_id=entity_id,
|
|
212
|
+
codec=codec,
|
|
213
|
+
required=required,
|
|
214
|
+
)
|
|
215
|
+
except (PreconditionRequired, MalformedPrecondition, VersionConflict) as exc:
|
|
216
|
+
pointed = [
|
|
217
|
+
field_error(("requests", item.index, "ifMatch"), exc.message),
|
|
218
|
+
]
|
|
219
|
+
raise type(exc)(
|
|
220
|
+
"{}", exc.message, error_code=exc.error_code, errors=pointed
|
|
221
|
+
) from exc
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def is_not_modified(if_none_match: str | None, etag: str) -> bool:
|
|
225
|
+
"""Return whether *etag* satisfies an inbound ``If-None-Match`` (RFC 9110 §13.1.2).
|
|
226
|
+
|
|
227
|
+
For a ``GET``/``HEAD`` whose caller already holds a cached representation:
|
|
228
|
+
a match means the caller sends 304 with no body and the ``ETag`` repeated
|
|
229
|
+
(RFC 9110 §15.4.5). Comparison is **weak** — a leading ``W/`` is ignored on
|
|
230
|
+
both sides — unlike :func:`check_precondition`'s ``If-Match``, which RFC
|
|
231
|
+
9110 requires to compare strongly by default; ETags in this kit are always
|
|
232
|
+
weak, so the distinction is moot here but the rule is still the caller's
|
|
233
|
+
to get right for a strong validator from elsewhere.
|
|
234
|
+
|
|
235
|
+
Args:
|
|
236
|
+
if_none_match: The raw ``If-None-Match`` header value, or ``None``.
|
|
237
|
+
etag: The resource's current ETag validator.
|
|
238
|
+
|
|
239
|
+
Returns:
|
|
240
|
+
``True`` when *if_none_match* is ``*``, or any validator in its
|
|
241
|
+
comma-separated list weakly matches *etag*.
|
|
242
|
+
"""
|
|
243
|
+
if if_none_match is None:
|
|
244
|
+
return False
|
|
245
|
+
candidate = if_none_match.strip()
|
|
246
|
+
if candidate == ANY_ETAG:
|
|
247
|
+
return True
|
|
248
|
+
target = etag.strip().removeprefix("W/")
|
|
249
|
+
return any(
|
|
250
|
+
part.strip().removeprefix("W/") == target for part in candidate.split(",")
|
|
251
|
+
)
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""Framework-independent HTTP conformance scenarios and redaction rules."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from rn_forge.web.conformance.cases import (
|
|
6
|
+
CASES,
|
|
7
|
+
case_by_id,
|
|
8
|
+
cases_for,
|
|
9
|
+
cases_provided_by,
|
|
10
|
+
)
|
|
11
|
+
from rn_forge.web.conformance.types import (
|
|
12
|
+
REDACTED,
|
|
13
|
+
VARIABLE_MEMBERS,
|
|
14
|
+
ConformanceArea,
|
|
15
|
+
ConformanceCapability,
|
|
16
|
+
ConformanceCase,
|
|
17
|
+
RequestSpec,
|
|
18
|
+
redact,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
__all__ = [
|
|
22
|
+
"CASES",
|
|
23
|
+
"REDACTED",
|
|
24
|
+
"VARIABLE_MEMBERS",
|
|
25
|
+
"ConformanceArea",
|
|
26
|
+
"ConformanceCapability",
|
|
27
|
+
"ConformanceCase",
|
|
28
|
+
"RequestSpec",
|
|
29
|
+
"case_by_id",
|
|
30
|
+
"cases_for",
|
|
31
|
+
"cases_provided_by",
|
|
32
|
+
"redact",
|
|
33
|
+
]
|