rn-forge-fastapi 0.5.0__tar.gz → 0.5.1__tar.gz

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.
Files changed (32) hide show
  1. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/PKG-INFO +1 -1
  2. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/pyproject.toml +1 -1
  3. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/pyproject.toml.orig +1 -1
  4. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/__init__.py +3 -0
  5. rn_forge_fastapi-0.5.1/src/rn_forge/fastapi/state.py +90 -0
  6. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/LICENSE +0 -0
  7. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/README.md +0 -0
  8. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/_batch.py +0 -0
  9. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/_tabular.py +0 -0
  10. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/app.py +0 -0
  11. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/auth/__init__.py +0 -0
  12. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/auth/authorization.py +0 -0
  13. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/auth/csrf.py +0 -0
  14. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/auth/dependencies.py +0 -0
  15. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/auth/proxies.py +0 -0
  16. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/auth/rate_limit.py +0 -0
  17. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/auth/redis.py +0 -0
  18. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/auth/routes.py +0 -0
  19. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/auth/sessions.py +0 -0
  20. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/cors.py +0 -0
  21. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/dependencies.py +0 -0
  22. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/deprecation.py +0 -0
  23. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/health.py +0 -0
  24. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/idempotency.py +0 -0
  25. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/merge_patch.py +0 -0
  26. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/openapi.py +0 -0
  27. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/problem.py +0 -0
  28. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/py.typed +0 -0
  29. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/read_mask.py +0 -0
  30. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/soft_delete.py +0 -0
  31. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/sse.py +0 -0
  32. {rn_forge_fastapi-0.5.0 → rn_forge_fastapi-0.5.1}/src/rn_forge/fastapi/transfer.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rn-forge-fastapi
3
- Version: 0.5.0
3
+ Version: 0.5.1
4
4
  Summary: FastAPI adapters over rn-forge-web
5
5
  Author: Rohit Narayanan
6
6
  Author-email: Rohit Narayanan <rohit.nn@gmail.com>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "rn-forge-fastapi"
3
- version = "0.5.0"
3
+ version = "0.5.1"
4
4
  description = "FastAPI adapters over rn-forge-web"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "rn-forge-fastapi"
3
- version = "0.5.0"
3
+ version = "0.5.1"
4
4
  description = "FastAPI adapters over rn-forge-web"
5
5
  readme = "README.md"
6
6
  authors = [{ name = "Rohit Narayanan", email = "rohit.nn@gmail.com" }]
@@ -54,6 +54,7 @@ from rn_forge.fastapi.soft_delete import (
54
54
  show_deleted_param,
55
55
  soft_delete_router,
56
56
  )
57
+ from rn_forge.fastapi.state import app_state, state_lifespan
57
58
 
58
59
  __all__ = [
59
60
  "ANONYMOUS_PREFIX",
@@ -80,6 +81,7 @@ __all__ = [
80
81
  "SoftDeleteState",
81
82
  "SoftDeleteStore",
82
83
  "active_session",
84
+ "app_state",
83
85
  "apply_cors",
84
86
  "auth_router",
85
87
  "basic_auth",
@@ -107,4 +109,5 @@ __all__ = [
107
109
  "sessions_for",
108
110
  "show_deleted_param",
109
111
  "soft_delete_router",
112
+ "state_lifespan",
110
113
  ]
@@ -0,0 +1,90 @@
1
+ """Typed application state: declared once, injected through ``Depends``."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import AsyncGenerator, Callable
6
+ from contextlib import AbstractAsyncContextManager, asynccontextmanager
7
+ from functools import cache
8
+ from typing import cast
9
+
10
+ from fastapi import FastAPI, Request
11
+ from starlette.types import Lifespan
12
+
13
+ __all__ = ["app_state", "state_lifespan"]
14
+
15
+ _KEY = "_rn_forge_states"
16
+
17
+
18
+ def _registry(app: FastAPI) -> dict[type, object]:
19
+ if not hasattr(app.state, _KEY):
20
+ setattr(app.state, _KEY, {})
21
+ return cast("dict[type, object]", getattr(app.state, _KEY))
22
+
23
+
24
+ @cache
25
+ def app_state[T](cls: type[T]) -> Callable[[Request], T]:
26
+ """Return the dependency that provides the *cls* state to a route.
27
+
28
+ The same dependency object is returned for the same class, so
29
+ ``app.dependency_overrides[app_state(cls)]`` replaces it.
30
+
31
+ Args:
32
+ cls: The state class registered with :func:`state_lifespan`.
33
+
34
+ Returns:
35
+ A dependency that returns the registered instance of *cls*; it raises
36
+ ``RuntimeError`` when the application has none.
37
+ """
38
+
39
+ def dependency(request: Request) -> T:
40
+ found = cast("dict[type, object]", getattr(request.app.state, _KEY, {})).get(
41
+ cls
42
+ )
43
+ if found is None:
44
+ raise RuntimeError(f"no {cls.__name__} registered on this application")
45
+ return cast("T", found)
46
+
47
+ return dependency
48
+
49
+
50
+ def state_lifespan[T](
51
+ cls: type[T],
52
+ opener: Callable[[FastAPI], AbstractAsyncContextManager[T]],
53
+ inner: Lifespan[FastAPI] | None = None,
54
+ ) -> Lifespan[FastAPI]:
55
+ """Return a lifespan that registers the state *opener* builds.
56
+
57
+ Startup enters *opener*, registers its value under *cls*, then enters
58
+ *inner*. Shutdown exits *inner*, removes the registration, then exits
59
+ *opener*. Nest calls through *inner* to register several state classes.
60
+
61
+ Args:
62
+ cls: The state class; routes receive it through :func:`app_state`.
63
+ opener: Builds the state and releases it on shutdown.
64
+ inner: A lifespan to run while the state is registered.
65
+
66
+ Returns:
67
+ A lifespan to pass as ``lifespan=`` to the application.
68
+
69
+ Raises:
70
+ TypeError: At startup, when the opener yields something that is not an
71
+ instance of *cls*; nothing is registered.
72
+ """
73
+
74
+ @asynccontextmanager
75
+ async def lifespan(app: FastAPI) -> AsyncGenerator[None]:
76
+ async with opener(app) as value:
77
+ if not isinstance(value, cls):
78
+ raise TypeError(f"{cls.__name__} expected, got {type(value).__name__}")
79
+ states = _registry(app)
80
+ states[cls] = value
81
+ try:
82
+ if inner is None:
83
+ yield
84
+ else:
85
+ async with cast("AbstractAsyncContextManager[None]", inner(app)):
86
+ yield
87
+ finally:
88
+ del states[cls]
89
+
90
+ return lifespan