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
rn_forge/web/transfer.py
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
"""Wire shapes for tabular export, import and bulk operations."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Iterable, Mapping
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import Any, Final, Literal, cast
|
|
8
|
+
from urllib.parse import quote
|
|
9
|
+
|
|
10
|
+
from rn_forge.web.exceptions import InvalidBatchGet, RowsInvalid
|
|
11
|
+
from rn_forge.web.merge_patch import JsonValue
|
|
12
|
+
from rn_forge.web.problem import (
|
|
13
|
+
REQUIRED_FIELD_DETAIL,
|
|
14
|
+
VALIDATION_ERROR,
|
|
15
|
+
ProblemRegistry,
|
|
16
|
+
ProblemResponse,
|
|
17
|
+
default_registry,
|
|
18
|
+
field_error,
|
|
19
|
+
render_problem,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"DUPLICATE_ID_DETAIL",
|
|
24
|
+
"NON_EMPTY_IDS_DETAIL",
|
|
25
|
+
"NON_EMPTY_LIST_DETAIL",
|
|
26
|
+
"TABULAR_FORMATS",
|
|
27
|
+
"BatchUpdateItem",
|
|
28
|
+
"ImportCounts",
|
|
29
|
+
"RowError",
|
|
30
|
+
"TabularFormat",
|
|
31
|
+
"batch_get_ids",
|
|
32
|
+
"content_disposition",
|
|
33
|
+
"export_cap_problem",
|
|
34
|
+
"import_report_body",
|
|
35
|
+
"negotiate_tabular_format",
|
|
36
|
+
"parse_batch_update",
|
|
37
|
+
"parse_flag",
|
|
38
|
+
"row_cap_problem",
|
|
39
|
+
"row_errors_problem",
|
|
40
|
+
"unreadable_file_detail",
|
|
41
|
+
"unsupported_file_detail",
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
NON_EMPTY_LIST_DETAIL: Final = "A non-empty list is required."
|
|
45
|
+
"""The detail for a bulk member that is an empty list or not a list."""
|
|
46
|
+
|
|
47
|
+
NON_EMPTY_IDS_DETAIL: Final = "A non-empty ids parameter is required."
|
|
48
|
+
"""The detail for a ``:batchGet`` request with no usable ``ids`` value."""
|
|
49
|
+
|
|
50
|
+
DUPLICATE_ID_DETAIL: Final = "Duplicate id in batch."
|
|
51
|
+
"""The detail for a repeated id in a ``:batchUpdate`` request."""
|
|
52
|
+
|
|
53
|
+
_PATCH_DETAIL: Final = "A merge patch must be a JSON object."
|
|
54
|
+
_IF_MATCH_DETAIL: Final = "Not a valid string."
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@dataclass(frozen=True)
|
|
58
|
+
class TabularFormat:
|
|
59
|
+
"""One tabular file format: its media type, extension and codec name."""
|
|
60
|
+
|
|
61
|
+
media_type: str
|
|
62
|
+
extension: str
|
|
63
|
+
tablib_name: str
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
TABULAR_FORMATS: Final[Mapping[str, TabularFormat]] = {
|
|
67
|
+
fmt.extension: fmt
|
|
68
|
+
for fmt in (
|
|
69
|
+
TabularFormat("text/csv", "csv", "csv"),
|
|
70
|
+
TabularFormat("text/tab-separated-values", "tsv", "tsv"),
|
|
71
|
+
TabularFormat(
|
|
72
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
73
|
+
"xlsx",
|
|
74
|
+
"xlsx",
|
|
75
|
+
),
|
|
76
|
+
TabularFormat("application/vnd.oasis.opendocument.spreadsheet", "ods", "ods"),
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
"""The tabular formats, keyed by file extension (which is also the ``?format=`` value)."""
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def negotiate_tabular_format(
|
|
83
|
+
accept: str | None, format_param: str | None, allowed: Iterable[str]
|
|
84
|
+
) -> TabularFormat | None:
|
|
85
|
+
"""Choose the tabular format a GET should be answered in, if any.
|
|
86
|
+
|
|
87
|
+
*format_param* wins over *accept*, as a link cannot set headers. Otherwise
|
|
88
|
+
the allowed format with the highest ``q`` in *accept* is chosen, but only
|
|
89
|
+
when it beats every explicitly listed non-tabular range; wildcards never
|
|
90
|
+
select a tabular format, so a client that did not ask for one gets the
|
|
91
|
+
JSON representation.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
accept: The ``Accept`` header value.
|
|
95
|
+
format_param: The ``format`` query parameter value.
|
|
96
|
+
allowed: The extensions (keys of :data:`TABULAR_FORMATS`) the endpoint offers.
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
The format, or ``None`` when the response should not be tabular,
|
|
100
|
+
including when *format_param* names a format that is not allowed.
|
|
101
|
+
"""
|
|
102
|
+
offered = [TABULAR_FORMATS[ext] for ext in allowed if ext in TABULAR_FORMATS]
|
|
103
|
+
if format_param:
|
|
104
|
+
return next((f for f in offered if f.extension == format_param.lower()), None)
|
|
105
|
+
if not accept:
|
|
106
|
+
return None
|
|
107
|
+
by_media = {f.media_type: f for f in offered}
|
|
108
|
+
best: tuple[float, TabularFormat] | None = None
|
|
109
|
+
best_other = 0.0
|
|
110
|
+
for media, q in _parse_accept(accept):
|
|
111
|
+
fmt = by_media.get(media)
|
|
112
|
+
if fmt is not None:
|
|
113
|
+
if best is None or q > best[0]:
|
|
114
|
+
best = (q, fmt)
|
|
115
|
+
elif "*" not in media:
|
|
116
|
+
best_other = max(best_other, q)
|
|
117
|
+
if best is None or best[0] <= 0 or best[0] <= best_other:
|
|
118
|
+
return None
|
|
119
|
+
return best[1]
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _parse_accept(accept: str) -> list[tuple[str, float]]:
|
|
123
|
+
"""Return ``(lowercased media range, q)`` for each range in *accept*."""
|
|
124
|
+
ranges: list[tuple[str, float]] = []
|
|
125
|
+
for part in accept.split(","):
|
|
126
|
+
media, *params = (p.strip() for p in part.split(";"))
|
|
127
|
+
if not media:
|
|
128
|
+
continue
|
|
129
|
+
q = 1.0
|
|
130
|
+
for param in params:
|
|
131
|
+
name, _, value = param.partition("=")
|
|
132
|
+
if name.strip().lower() == "q":
|
|
133
|
+
try:
|
|
134
|
+
q = float(value)
|
|
135
|
+
except ValueError:
|
|
136
|
+
q = 0.0
|
|
137
|
+
ranges.append((media.lower(), q))
|
|
138
|
+
return ranges
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def content_disposition(filename: str) -> str:
|
|
142
|
+
"""Return an ``attachment`` ``Content-Disposition`` value for *filename*.
|
|
143
|
+
|
|
144
|
+
Carries both the RFC 6266 quoted ``filename`` (non-ASCII and control
|
|
145
|
+
characters replaced by ``_``, path separators dropped) and the RFC 8187
|
|
146
|
+
``filename*`` in UTF-8, which clients that understand it prefer.
|
|
147
|
+
|
|
148
|
+
Example::
|
|
149
|
+
|
|
150
|
+
content_disposition("Orders 2026.csv")
|
|
151
|
+
# 'attachment; filename="Orders 2026.csv"; filename*=UTF-8\\'\\'Orders%202026.csv'
|
|
152
|
+
"""
|
|
153
|
+
name = filename.replace("/", "").replace("\\", "")
|
|
154
|
+
fallback = "".join("_" if not (32 <= ord(c) < 127) else c for c in name).replace(
|
|
155
|
+
'"', "'"
|
|
156
|
+
)
|
|
157
|
+
encoded = quote(name, safe="!#$&+-.^_`|~")
|
|
158
|
+
return f"attachment; filename=\"{fallback}\"; filename*=UTF-8''{encoded}"
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def import_report_body(
|
|
162
|
+
*, created: int, updated: int, skipped: int, validate_only: bool
|
|
163
|
+
) -> dict[str, Any]:
|
|
164
|
+
"""Return the body of a successful import: counts, and whether nothing was persisted."""
|
|
165
|
+
return {
|
|
166
|
+
"created": created,
|
|
167
|
+
"updated": updated,
|
|
168
|
+
"skipped": skipped,
|
|
169
|
+
"validateOnly": validate_only,
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
@dataclass(frozen=True)
|
|
174
|
+
class RowError:
|
|
175
|
+
"""One failed cell: the row's index, the column (or field), and the message.
|
|
176
|
+
|
|
177
|
+
An empty *field* points at the whole row.
|
|
178
|
+
"""
|
|
179
|
+
|
|
180
|
+
row: int
|
|
181
|
+
field: str
|
|
182
|
+
message: str
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def row_errors_problem(
|
|
186
|
+
errors: Iterable[RowError],
|
|
187
|
+
*,
|
|
188
|
+
instance: str,
|
|
189
|
+
root: str = "rows",
|
|
190
|
+
registry: ProblemRegistry | None = None,
|
|
191
|
+
) -> ProblemResponse:
|
|
192
|
+
"""Render row errors as a 422 ``application/problem+json`` response.
|
|
193
|
+
|
|
194
|
+
Each error becomes an ``errors`` entry whose ``pointer`` is
|
|
195
|
+
``/<root>/<row>/<field>``, e.g. ``/rows/12/Quantity`` for an import and
|
|
196
|
+
``/requests/3/name`` for a batch create (``root="requests"``).
|
|
197
|
+
|
|
198
|
+
Args:
|
|
199
|
+
errors: The failed cells.
|
|
200
|
+
instance: The ``instance`` member, in practice the request path.
|
|
201
|
+
root: The first pointer segment.
|
|
202
|
+
registry: The registry to render with; the default when omitted.
|
|
203
|
+
"""
|
|
204
|
+
entries = [
|
|
205
|
+
field_error((root, e.row, *([e.field] if e.field else [])), e.message)
|
|
206
|
+
for e in errors
|
|
207
|
+
]
|
|
208
|
+
return render_problem(
|
|
209
|
+
registry or default_registry(),
|
|
210
|
+
ValueError("Validation Error"),
|
|
211
|
+
instance=instance,
|
|
212
|
+
problem=VALIDATION_ERROR,
|
|
213
|
+
detail="One or more rows are invalid.",
|
|
214
|
+
extensions={"errors": entries},
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def export_cap_problem(
|
|
219
|
+
cap: int, *, instance: str, registry: ProblemRegistry | None = None
|
|
220
|
+
) -> ProblemResponse:
|
|
221
|
+
"""Render "the export exceeds *cap* rows" as a 422 problem naming the cap."""
|
|
222
|
+
return render_problem(
|
|
223
|
+
registry or default_registry(),
|
|
224
|
+
ValueError("Validation Error"),
|
|
225
|
+
instance=instance,
|
|
226
|
+
problem=VALIDATION_ERROR,
|
|
227
|
+
detail=f"The export exceeds the limit of {cap} rows; narrow the filter.",
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def row_cap_problem(
|
|
232
|
+
kind: Literal["import", "batch"],
|
|
233
|
+
cap: int,
|
|
234
|
+
*,
|
|
235
|
+
instance: str,
|
|
236
|
+
registry: ProblemRegistry | None = None,
|
|
237
|
+
) -> ProblemResponse:
|
|
238
|
+
"""Render "the *kind* exceeds *cap* rows" as a 422 problem with no ``errors`` member.
|
|
239
|
+
|
|
240
|
+
Args:
|
|
241
|
+
kind: ``"import"`` or ``"batch"``.
|
|
242
|
+
cap: The row limit.
|
|
243
|
+
instance: The ``instance`` member, in practice the request path.
|
|
244
|
+
registry: The registry to render with; the default when omitted.
|
|
245
|
+
"""
|
|
246
|
+
return render_problem(
|
|
247
|
+
registry or default_registry(),
|
|
248
|
+
ValueError("Validation Error"),
|
|
249
|
+
instance=instance,
|
|
250
|
+
problem=VALIDATION_ERROR,
|
|
251
|
+
detail=_cap_detail(kind, cap),
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def _cap_detail(kind: str, cap: int) -> str:
|
|
256
|
+
return f"The {kind} exceeds the limit of {cap} rows."
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def batch_get_ids(values: Iterable[str], *, cap: int | None) -> list[str]:
|
|
260
|
+
"""Return the ids of a ``:batchGet`` request, from its repeated ``ids`` values.
|
|
261
|
+
|
|
262
|
+
Empty values are dropped; a value is never split, so a comma-joined value
|
|
263
|
+
is one id.
|
|
264
|
+
|
|
265
|
+
Args:
|
|
266
|
+
values: The raw values of the repeated ``ids`` query parameter.
|
|
267
|
+
cap: The most ids a request may name, or ``None`` for no limit.
|
|
268
|
+
|
|
269
|
+
Raises:
|
|
270
|
+
InvalidBatchGet: No value is non-empty, or there are more than *cap* ids.
|
|
271
|
+
"""
|
|
272
|
+
ids = [value for value in values if value]
|
|
273
|
+
if not ids:
|
|
274
|
+
raise InvalidBatchGet(NON_EMPTY_IDS_DETAIL)
|
|
275
|
+
if cap is not None and len(ids) > cap:
|
|
276
|
+
raise InvalidBatchGet(_cap_detail("batch", cap))
|
|
277
|
+
return ids
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
@dataclass(frozen=True)
|
|
281
|
+
class BatchUpdateItem:
|
|
282
|
+
"""One validated ``:batchUpdate`` item.
|
|
283
|
+
|
|
284
|
+
*index* is the item's position in ``requests``, *patch* its JSON Merge Patch
|
|
285
|
+
and *if_match* its ``ifMatch`` precondition, if any.
|
|
286
|
+
"""
|
|
287
|
+
|
|
288
|
+
index: int
|
|
289
|
+
id: str
|
|
290
|
+
patch: dict[str, JsonValue]
|
|
291
|
+
if_match: str | None
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def parse_batch_update(
|
|
295
|
+
body: object,
|
|
296
|
+
*,
|
|
297
|
+
cap: int | None,
|
|
298
|
+
instance: str,
|
|
299
|
+
registry: ProblemRegistry | None = None,
|
|
300
|
+
) -> list[BatchUpdateItem] | ProblemResponse:
|
|
301
|
+
"""Check a ``:batchUpdate`` body's list, its size, each item's shape and the ids.
|
|
302
|
+
|
|
303
|
+
Args:
|
|
304
|
+
body: The decoded JSON request body.
|
|
305
|
+
cap: The most items a request may carry, or ``None`` for no limit.
|
|
306
|
+
instance: The ``instance`` member, in practice the request path.
|
|
307
|
+
registry: The registry to render with; the default when omitted.
|
|
308
|
+
|
|
309
|
+
Returns:
|
|
310
|
+
The items in request order, or the problem to render when ``requests``
|
|
311
|
+
is missing, empty or not a list (422 at ``/requests``) or over *cap*
|
|
312
|
+
(:func:`row_cap_problem`).
|
|
313
|
+
|
|
314
|
+
Raises:
|
|
315
|
+
RowsInvalid: An item has no ``id``, a ``patch`` that is not a JSON
|
|
316
|
+
object or an ``ifMatch`` that is not a string, or repeats an
|
|
317
|
+
earlier item's id. Every failing item is reported, with root
|
|
318
|
+
``requests``.
|
|
319
|
+
"""
|
|
320
|
+
members = cast("dict[str, object]", body) if isinstance(body, dict) else {}
|
|
321
|
+
raw = members.get("requests")
|
|
322
|
+
if not isinstance(raw, list) or not raw:
|
|
323
|
+
detail = (
|
|
324
|
+
NON_EMPTY_LIST_DETAIL if "requests" in members else REQUIRED_FIELD_DETAIL
|
|
325
|
+
)
|
|
326
|
+
return render_problem(
|
|
327
|
+
registry or default_registry(),
|
|
328
|
+
ValueError("Validation Error"),
|
|
329
|
+
instance=instance,
|
|
330
|
+
problem=VALIDATION_ERROR,
|
|
331
|
+
detail="Validation Error",
|
|
332
|
+
extensions={"errors": [field_error(("requests",), detail)]},
|
|
333
|
+
)
|
|
334
|
+
entries = cast("list[object]", raw)
|
|
335
|
+
if cap is not None and len(entries) > cap:
|
|
336
|
+
return row_cap_problem("batch", cap, instance=instance, registry=registry)
|
|
337
|
+
|
|
338
|
+
items: list[BatchUpdateItem] = []
|
|
339
|
+
errors: list[RowError] = []
|
|
340
|
+
for index, entry in enumerate(entries):
|
|
341
|
+
fields = cast("dict[str, object]", entry) if isinstance(entry, dict) else {}
|
|
342
|
+
item_id = fields.get("id")
|
|
343
|
+
patch = fields.get("patch")
|
|
344
|
+
if_match = fields.get("ifMatch")
|
|
345
|
+
failed = False
|
|
346
|
+
if isinstance(item_id, bool) or not isinstance(item_id, (str, int)):
|
|
347
|
+
errors.append(RowError(index, "id", REQUIRED_FIELD_DETAIL))
|
|
348
|
+
failed = True
|
|
349
|
+
if not isinstance(patch, dict):
|
|
350
|
+
errors.append(RowError(index, "patch", _PATCH_DETAIL))
|
|
351
|
+
failed = True
|
|
352
|
+
if if_match is not None and not isinstance(if_match, str):
|
|
353
|
+
errors.append(RowError(index, "ifMatch", _IF_MATCH_DETAIL))
|
|
354
|
+
failed = True
|
|
355
|
+
if not failed:
|
|
356
|
+
items.append(
|
|
357
|
+
BatchUpdateItem(
|
|
358
|
+
index,
|
|
359
|
+
str(item_id),
|
|
360
|
+
cast("dict[str, JsonValue]", patch),
|
|
361
|
+
cast("str | None", if_match),
|
|
362
|
+
)
|
|
363
|
+
)
|
|
364
|
+
if errors:
|
|
365
|
+
raise RowsInvalid(errors, root="requests")
|
|
366
|
+
|
|
367
|
+
seen: set[str] = set()
|
|
368
|
+
for item in items:
|
|
369
|
+
if item.id in seen:
|
|
370
|
+
errors.append(RowError(item.index, "id", DUPLICATE_ID_DETAIL))
|
|
371
|
+
seen.add(item.id)
|
|
372
|
+
if errors:
|
|
373
|
+
raise RowsInvalid(errors, root="requests")
|
|
374
|
+
return items
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
@dataclass(frozen=True)
|
|
378
|
+
class ImportCounts:
|
|
379
|
+
"""What an import did: rows *created*, rows *updated* and rows *skipped*."""
|
|
380
|
+
|
|
381
|
+
created: int
|
|
382
|
+
updated: int
|
|
383
|
+
skipped: int
|
|
384
|
+
|
|
385
|
+
|
|
386
|
+
def parse_flag(value: str | None) -> bool:
|
|
387
|
+
"""Return whether *value* is ``"true"`` or ``"1"``, ignoring case.
|
|
388
|
+
|
|
389
|
+
Anything else, including ``None``, is ``False``. It parses the
|
|
390
|
+
``validateOnly`` and ``prefill`` query parameters.
|
|
391
|
+
"""
|
|
392
|
+
return value is not None and value.lower() in {"true", "1"}
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
def unsupported_file_detail(formats: Iterable[str]) -> str:
|
|
396
|
+
"""Return the detail for an upload whose type is not in *formats*.
|
|
397
|
+
|
|
398
|
+
Example::
|
|
399
|
+
|
|
400
|
+
unsupported_file_detail(("csv", "xlsx"))
|
|
401
|
+
# 'Unsupported file type; use one of: csv, xlsx.'
|
|
402
|
+
"""
|
|
403
|
+
return f"Unsupported file type; use one of: {', '.join(formats)}."
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
def unreadable_file_detail(extension: str) -> str:
|
|
407
|
+
"""Return the detail for an upload that could not be parsed as *extension*."""
|
|
408
|
+
return f"The file could not be read as {extension}."
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rn-forge-web
|
|
3
|
+
Version: 0.3.0.dev0
|
|
4
|
+
Summary: Framework-agnostic HTTP/API primitives built on rn-forge-commons
|
|
5
|
+
Author: Rohit Narayanan
|
|
6
|
+
Author-email: Rohit Narayanan <rohit.nn@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Typing :: Typed
|
|
16
|
+
Requires-Dist: rn-forge-commons[pydantic]>=0.8.0,<0.9
|
|
17
|
+
Requires-Dist: opentelemetry-api>=1.44
|
|
18
|
+
Requires-Dist: secure>=2.0.1 ; extra == 'security'
|
|
19
|
+
Requires-Dist: assertpy>=1.1 ; extra == 'testing'
|
|
20
|
+
Requires-Dist: pytest>=9.1.1 ; extra == 'testing'
|
|
21
|
+
Requires-Python: >=3.14
|
|
22
|
+
Project-URL: Documentation, https://rn-forge.github.io/pykit/packages/rn-forge-web/latest/
|
|
23
|
+
Project-URL: Source, https://github.com/rn-forge/pykit/tree/main/packages/rn-forge-web
|
|
24
|
+
Project-URL: Changelog, https://github.com/rn-forge/pykit/blob/main/packages/rn-forge-web/CHANGELOG.md
|
|
25
|
+
Project-URL: Issues, https://github.com/rn-forge/pykit/issues
|
|
26
|
+
Provides-Extra: security
|
|
27
|
+
Provides-Extra: testing
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# rn-forge-web
|
|
31
|
+
|
|
32
|
+
[Documentation](https://rn-forge.github.io/pykit/packages/rn-forge-web/latest/)
|
|
33
|
+
·
|
|
34
|
+
[Changelog](https://github.com/rn-forge/pykit/blob/main/packages/rn-forge-web/CHANGELOG.md)
|
|
35
|
+
· [Source](https://github.com/rn-forge/pykit/tree/main/packages/rn-forge-web) ·
|
|
36
|
+
[All pykit packages](https://rn-forge.github.io/pykit/)
|
|
37
|
+
|
|
38
|
+
Framework-agnostic HTTP/API primitives, built on `rn-forge-commons`.
|
|
39
|
+
|
|
40
|
+
These are the wire semantics an application *promises its callers* — W3C Trace
|
|
41
|
+
Context propagation, the error body, the precondition contract, the pagination
|
|
42
|
+
envelope, the idempotency contract, the readiness aggregate, the authentication
|
|
43
|
+
contract, the OpenAPI naming rules, and the conformance table the framework
|
|
44
|
+
packages are tested against.
|
|
45
|
+
|
|
46
|
+
Fourteen flat modules, all one kind of mechanism: **inbound** HTTP wire
|
|
47
|
+
semantics.
|
|
48
|
+
|
|
49
|
+
## Where it sits
|
|
50
|
+
|
|
51
|
+
```text
|
|
52
|
+
commons ──► web ──► auth ──► django
|
|
53
|
+
│ └───► fastapi
|
|
54
|
+
└───► sqlalchemy
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
`rn-forge-web` depends on **`rn-forge-commons` and nothing else in the
|
|
58
|
+
workspace**. It never imports `rn_forge.cli` or `rn_forge.tooling` — those are
|
|
59
|
+
the command-line and file-owning layers, and a package that ships into an ASGI
|
|
60
|
+
server has no business reaching either. It never imports a web framework: not
|
|
61
|
+
`django`, not `fastapi`, not `starlette`, not `rest_framework`.
|
|
62
|
+
|
|
63
|
+
`.importlinter` at the repo root carries the `web-is-framework-free` and
|
|
64
|
+
`web-layers` contracts, and `uv run lint-imports` proves them.
|
|
65
|
+
|
|
66
|
+
## Inbound, not outbound
|
|
67
|
+
|
|
68
|
+
The tempting rule is "everything HTTP lives in `rn-forge-web`", and it is wrong.
|
|
69
|
+
A circuit breaker, a retry policy and a token bucket are transport-agnostic: a
|
|
70
|
+
worker retrying a database call, a CLI retrying a blob upload and an LLM adapter
|
|
71
|
+
backing off all need them, and none of them speaks server-side HTTP. Those live
|
|
72
|
+
in `rn_forge.commons.integration.resilience`, so that an Azure Blob or Key Vault
|
|
73
|
+
adapter package can depend on commons alone rather than dragging ASGI middleware
|
|
74
|
+
and cursor codecs into a process that serves no requests.
|
|
75
|
+
|
|
76
|
+
The one concern that runs the other way is `problem_from_body`, which parses an
|
|
77
|
+
*upstream's* `application/problem+json` back into a `ProblemDetail`. It takes a
|
|
78
|
+
parsed mapping and a status — not a response object — precisely so no HTTP
|
|
79
|
+
client library is pulled in either direction.
|
|
80
|
+
|
|
81
|
+
## What stays in a framework package
|
|
82
|
+
|
|
83
|
+
Logic that both framework packages need lives here, as framework-free functions:
|
|
84
|
+
rendering a problem response (`render_problem`), the field-error entry
|
|
85
|
+
(`field_error`), reading the current trace id, parsing an `Authorization`
|
|
86
|
+
header, requiring an idempotency key, the liveness body and the OpenAPI
|
|
87
|
+
problem-response declarations. `rn-forge-django` and `rn-forge-fastapi` keep
|
|
88
|
+
only the code that reads their framework's native shapes (a DRF error tree, a
|
|
89
|
+
pydantic error list, a Starlette exception) and writes their framework's native
|
|
90
|
+
response.
|
|
91
|
+
|
|
92
|
+
## Installation
|
|
93
|
+
|
|
94
|
+
Require the package by version range:
|
|
95
|
+
|
|
96
|
+
```toml
|
|
97
|
+
dependencies = ["rn-forge-web>=0.3,<0.4"]
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Inside this workspace, `[tool.uv.sources]` overrides the requirement with the
|
|
101
|
+
local checkout; that override is for local development only and does not survive
|
|
102
|
+
into a built wheel.
|
|
103
|
+
|
|
104
|
+
## Dependencies and why
|
|
105
|
+
|
|
106
|
+
**`rn-forge-commons`** — `AppException` (every exception here subclasses it, so
|
|
107
|
+
a consumer already catching that catches these), through its `pydantic` extra.
|
|
108
|
+
|
|
109
|
+
**`pydantic`, through that extra** — `models.py` holds `WireModel`, the
|
|
110
|
+
camelCase base, and `ProblemDetail`, `Page[T]`, `CheckResult` and `HealthReport`
|
|
111
|
+
are `WireModel` subclasses. Both framework packages use them directly: FastAPI
|
|
112
|
+
as request and response models, Django for its OpenAPI components and health
|
|
113
|
+
bodies. `rn-forge-django` therefore installs pydantic transitively.
|
|
114
|
+
|
|
115
|
+
**`secure` (2.0.1), behind this package's own `security` extra** — and only for
|
|
116
|
+
`rn_forge.web.security`. It is framework-agnostic (no dependencies of its own)
|
|
117
|
+
and emits the OWASP REST Security Cheat Sheet header preset exactly, so it is
|
|
118
|
+
wrapped rather than hand-rolled. Behind an extra and excluded from the curated
|
|
119
|
+
`__init__.py` so the rest of the package stays dependency-free; import
|
|
120
|
+
`rn_forge.web.security` directly.
|
|
121
|
+
|
|
122
|
+
**`opentelemetry-api`, a base dependency** — and only for
|
|
123
|
+
`rn_forge.web.tracing`. W3C Trace Context is read through OpenTelemetry's
|
|
124
|
+
active-span API, following OpenTelemetry's own library guidance: a library
|
|
125
|
+
depends on the API only, which is a no-op until the application configures the
|
|
126
|
+
SDK. This package never configures a `TracerProvider` or an exporter.
|
|
127
|
+
|
|
128
|
+
**Nothing else beyond those.** The problem shape, ETag preconditions, cursor
|
|
129
|
+
pagination, idempotency and health aggregation are implemented here: the
|
|
130
|
+
maintained libraries for each either depend on a web framework or fix a
|
|
131
|
+
different wire shape.
|
|
132
|
+
|
|
133
|
+
## Documentation
|
|
134
|
+
|
|
135
|
+
`uv run --directory packages/rn-forge-web --group docs mkdocs build --strict`,
|
|
136
|
+
or the combined site from the repo root. `docs/guides/api-conventions.md` is the
|
|
137
|
+
normative wire contract for an application's specification.
|
|
138
|
+
`docs/guides/model-conventions.md` compares implemented ORM building blocks and
|
|
139
|
+
optional application design advice.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
rn_forge/web/__init__.py,sha256=wK-1_ALjAtXQ7seTOXj2gPY7D2XzGMqZf5mKRsuJMjc,7993
|
|
2
|
+
rn_forge/web/asgi.py,sha256=yoSBh71-vMePkd6uqQ_fOYm9AcVVqxvKh05qNON0VA8,4910
|
|
3
|
+
rn_forge/web/auth.py,sha256=eLB8X35MgKxnz9PLw8KBBEU1nGXkUxZY1Cz2lz26NzA,3750
|
|
4
|
+
rn_forge/web/concurrency.py,sha256=j7fXsuz4hA62y9hAuabNfVFIW494pPwjhKdTah6J8yU,8832
|
|
5
|
+
rn_forge/web/conformance/__init__.py,sha256=xObs5gDZ34CS_xyF-_0_jgc34i_zpU_I21TzquexLrc,642
|
|
6
|
+
rn_forge/web/conformance/cases.py,sha256=NJYaoNqWr6nLp8_bTl_op_sGNxIKnRPHgcH5roH99uA,71090
|
|
7
|
+
rn_forge/web/conformance/types.py,sha256=_ZjJVNjDq0pfSfD1xlqdlrwWQ5ejYDLuQVfSVoN_mA0,4550
|
|
8
|
+
rn_forge/web/deprecation.py,sha256=2g1Burwf5ZaYhB4its2BpIjQzD10LIhOsmGU_dydbq4,1240
|
|
9
|
+
rn_forge/web/exceptions.py,sha256=JKGjJw7wdEymy3YMXQhbdoJ3zQ5jARnpgZIBwoMYMgU,11771
|
|
10
|
+
rn_forge/web/health.py,sha256=27XWkpHTuusE8H83ONHq9jHKuzdO0G6rgUOZ8n6OstU,7941
|
|
11
|
+
rn_forge/web/idempotency.py,sha256=Md5Ppn8HigFv8w3NCVefuFL4MqksOsJGAlGnu1WSU-k,10073
|
|
12
|
+
rn_forge/web/merge_patch.py,sha256=aL9zNoS2wxWxa6dZGFOp7PH8mzpUmPQ4I5dd0PHAyx8,3007
|
|
13
|
+
rn_forge/web/models.py,sha256=vOP4z2WWe_0C5-8-UwHSR4CwPJPEpU0ZMfkCMGUeVJI,635
|
|
14
|
+
rn_forge/web/openapi.py,sha256=0fBYSmyWry_GYmunxctQjPaAaW04hlQGj6JoYRfkifU,6316
|
|
15
|
+
rn_forge/web/operations.py,sha256=NoujeyQquRGMeE_m_XKItbCOIt3xmaVxmA3wsGHg6JU,1890
|
|
16
|
+
rn_forge/web/pagination.py,sha256=CsrHE7I00XQ__NMwl3dKg-A8bkGVuEer9y5C7lENagY,8873
|
|
17
|
+
rn_forge/web/problem.py,sha256=MQ1YTrtmaSMf-3SNpu5lbJfeDdBg-0OrTOh4fsD3wGY,21914
|
|
18
|
+
rn_forge/web/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
19
|
+
rn_forge/web/read_mask.py,sha256=CcVePUcHf3CenduTMOeGLEpRh3ZuSCk4c5qZQEkEMY8,3912
|
|
20
|
+
rn_forge/web/security.py,sha256=Na4XxGOE4JT0pZxhwSBrmXaqEa9Jr8xFb-zJ-8aV25c,3686
|
|
21
|
+
rn_forge/web/soft_delete.py,sha256=0zEc2j6LmvSFVH0VYbRZWgrpwPV4ELf1dXRDshjL-K0,935
|
|
22
|
+
rn_forge/web/tracing.py,sha256=LKc5pAz5-ggUOpCTBiYYQ_eebZnKl0zpUFSxBdACy0g,1679
|
|
23
|
+
rn_forge/web/transfer.py,sha256=Zob6LZG9Dh5IlMJkJXXB7aG2k63u_ivD6aAS1Dhevuk,13560
|
|
24
|
+
rn_forge_web-0.3.0.dev0.dist-info/licenses/LICENSE,sha256=buwykOASkbZdpj0j_JmFbwNzbUiuLzOiaMQmVQt5lr8,1072
|
|
25
|
+
rn_forge_web-0.3.0.dev0.dist-info/WHEEL,sha256=si2p8vW03RnhyBnhWl4tj-ObJa78SpE-c9HsEtblGR0,80
|
|
26
|
+
rn_forge_web-0.3.0.dev0.dist-info/METADATA,sha256=aXrbEdCUgW2DCrkOBzAlc2jLgPJfZ92W0sTChdxAo0Y,6467
|
|
27
|
+
rn_forge_web-0.3.0.dev0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rohit Narayanan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|