fastapi-injected 0.4.0__tar.gz → 0.4.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.
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/PKG-INFO +1 -1
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/fastapi.py +35 -6
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/pyproject.toml +1 -1
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_body.py +22 -1
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_fastapi.py +4 -1
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/uv.lock +1 -1
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/.github/dependabot.yml +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/.github/workflows/automerge.yml +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/.github/workflows/lint.yml +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/.github/workflows/publish.yml +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/.github/workflows/test.yml +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/.gitignore +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/.pre-commit-config.yaml +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/LICENSE +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/README.md +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/__init__.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/_bind.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/_body.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/_cache.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/_calls.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/_dataclass.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/_deps_tp.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/_given.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/_injected.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/_overrides.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/_rlock.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/ctx.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/deps.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/ext.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/inject.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/overrides.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/reflect/__init__.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/reflect/_dependant.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/reflect/_openapi.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/reflect/_routes.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/resolve.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/signature.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/fastapi_injected/types.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/__init__.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/_typing/__init__.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/_typing/annotations.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/_typing/arg.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/_typing/bind.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/_typing/ctx.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/_typing/dataclass.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/_typing/deps.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/_typing/errors.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/_typing/given.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/_typing/inject.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/_typing/injected.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/_typing/integration.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/_typing/markers.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/_typing/overrides.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/_typing/resolve.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/deps.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/ext/__init__.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/ext/test_pydantic_ai.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_bind.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_body_reads.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_cache.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_calls.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_concurrency.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_ctx.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_dataclass.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_dependant_cache.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_errors.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_given.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_inject.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_injected.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_overrides.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_reflect.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_request_params.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_resolve.py +0 -0
- {fastapi_injected-0.4.0 → fastapi_injected-0.4.1}/tests/test_rlock.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: fastapi-injected
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.1
|
|
4
4
|
Summary: Yet another library to reuse fastapi dependency injection
|
|
5
5
|
Project-URL: Repository, https://github.com/uriyyo/fastapi-injected
|
|
6
6
|
Author-email: Yurii Karabas <1998uriyyo@gmail.com>
|
|
@@ -6,7 +6,7 @@ from typing import TYPE_CHECKING, Any
|
|
|
6
6
|
from fastapi import Depends, FastAPI, Request, WebSocket, routing
|
|
7
7
|
from fastapi.dependencies import utils
|
|
8
8
|
from starlette.requests import HTTPConnection
|
|
9
|
-
from starlette.types import ASGIApp, Receive, Scope, Send
|
|
9
|
+
from starlette.types import ASGIApp, Message, Receive, Scope, Send
|
|
10
10
|
|
|
11
11
|
from ._cache import ContextCache
|
|
12
12
|
from .ctx import injection_ctx
|
|
@@ -19,6 +19,19 @@ class MissingDependencyCacheError(Exception):
|
|
|
19
19
|
pass
|
|
20
20
|
|
|
21
21
|
|
|
22
|
+
class ConnectionNotAvailableError(Exception):
|
|
23
|
+
def __init__(self) -> None:
|
|
24
|
+
super().__init__("The connection can not be received from or sent to outside of a route")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
async def _unavailable_receive() -> Message:
|
|
28
|
+
raise ConnectionNotAvailableError
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
async def _unavailable_send(_: Message) -> None:
|
|
32
|
+
raise ConnectionNotAvailableError
|
|
33
|
+
|
|
34
|
+
|
|
22
35
|
if not TYPE_CHECKING:
|
|
23
36
|
_base_solve_dependencies = utils.solve_dependencies
|
|
24
37
|
|
|
@@ -91,9 +104,9 @@ class InjectionMiddleware:
|
|
|
91
104
|
|
|
92
105
|
match scope["type"]:
|
|
93
106
|
case "http":
|
|
94
|
-
connection = Request(scope,
|
|
107
|
+
connection = Request(scope, _unavailable_receive, _unavailable_send)
|
|
95
108
|
case "websocket":
|
|
96
|
-
connection = WebSocket(scope,
|
|
109
|
+
connection = WebSocket(scope, _unavailable_receive, _unavailable_send)
|
|
97
110
|
case _:
|
|
98
111
|
await self.app(scope, receive, send)
|
|
99
112
|
return
|
|
@@ -103,23 +116,39 @@ class InjectionMiddleware:
|
|
|
103
116
|
async with injection_ctx(
|
|
104
117
|
dependency_cache=cache,
|
|
105
118
|
request=connection,
|
|
106
|
-
provider=
|
|
119
|
+
provider=_get_overrides_provider(connection),
|
|
107
120
|
):
|
|
108
121
|
await self.app(scope, receive, send)
|
|
109
122
|
|
|
110
123
|
|
|
124
|
+
def _wrap_build_middleware_stack(app: FastAPI) -> None:
|
|
125
|
+
_build_middleware_stack = app.build_middleware_stack
|
|
126
|
+
|
|
127
|
+
@wraps(_build_middleware_stack)
|
|
128
|
+
def _build_middleware_stack_wrapper() -> ASGIApp:
|
|
129
|
+
return InjectionMiddleware(_build_middleware_stack())
|
|
130
|
+
|
|
131
|
+
app.build_middleware_stack = _build_middleware_stack_wrapper # type: ignore[ty:invalid-assignment]
|
|
132
|
+
app.build_middleware_stack.__patched__ = True # type: ignore[ty:unresolved-attribute]
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _is_build_middleware_stack_patched(app: FastAPI) -> bool:
|
|
136
|
+
return getattr(app.build_middleware_stack, "__patched__", False)
|
|
137
|
+
|
|
138
|
+
|
|
111
139
|
def add_injection(
|
|
112
140
|
app: FastAPI,
|
|
113
141
|
/,
|
|
114
142
|
) -> None:
|
|
115
|
-
if not
|
|
116
|
-
app
|
|
143
|
+
if not _is_build_middleware_stack_patched(app):
|
|
144
|
+
_wrap_build_middleware_stack(app)
|
|
117
145
|
|
|
118
146
|
if not any(dep.dependency is bind_injection_ctx for dep in app.router.dependencies):
|
|
119
147
|
app.router.dependencies.insert(0, Depends(bind_injection_ctx))
|
|
120
148
|
|
|
121
149
|
|
|
122
150
|
__all__ = [
|
|
151
|
+
"ConnectionNotAvailableError",
|
|
123
152
|
"InjectionMiddleware",
|
|
124
153
|
"MissingDependencyCacheError",
|
|
125
154
|
"add_injection",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from typing import Annotated
|
|
1
|
+
from typing import Annotated, Any
|
|
2
2
|
|
|
3
3
|
import pytest
|
|
4
4
|
from fastapi import (
|
|
@@ -30,6 +30,7 @@ from fastapi_injected import (
|
|
|
30
30
|
resolve,
|
|
31
31
|
synthetic_request,
|
|
32
32
|
)
|
|
33
|
+
from fastapi_injected.fastapi import ConnectionNotAvailableError
|
|
33
34
|
|
|
34
35
|
|
|
35
36
|
class Item(BaseModel):
|
|
@@ -522,3 +523,23 @@ async def test_header_with_an_alias_on_a_synthetic_request():
|
|
|
522
523
|
[error] = exc_info.value.errors
|
|
523
524
|
|
|
524
525
|
assert (error["type"], error["loc"]) == ("missing", ("header", "X-Api-Key"))
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
def test_body_can_not_be_read_in_a_middleware() -> None:
|
|
529
|
+
middleware_app = FastAPI()
|
|
530
|
+
|
|
531
|
+
@middleware_app.middleware("http")
|
|
532
|
+
async def _read_body(request: Request, call_next: Any) -> Any:
|
|
533
|
+
with pytest.raises(ConnectionNotAvailableError):
|
|
534
|
+
await resolve(ItemDep)
|
|
535
|
+
|
|
536
|
+
return await call_next(request)
|
|
537
|
+
|
|
538
|
+
@middleware_app.post("/")
|
|
539
|
+
async def _route(item: Item) -> str:
|
|
540
|
+
# the route still gets the body the middleware could not take
|
|
541
|
+
return item.name
|
|
542
|
+
|
|
543
|
+
add_injection(middleware_app)
|
|
544
|
+
|
|
545
|
+
assert TestClient(middleware_app).post("/", json={"name": "g"}).json() == "g"
|
|
@@ -136,7 +136,10 @@ def test_add_injection_is_idempotent() -> None:
|
|
|
136
136
|
add_injection(other_app)
|
|
137
137
|
add_injection(other_app)
|
|
138
138
|
|
|
139
|
-
|
|
139
|
+
stack = other_app.build_middleware_stack()
|
|
140
|
+
assert isinstance(stack, InjectionMiddleware)
|
|
141
|
+
assert not isinstance(stack.app, InjectionMiddleware)
|
|
142
|
+
assert [middleware.cls for middleware in other_app.user_middleware] == [GZipMiddleware]
|
|
140
143
|
assert [dep.dependency for dep in other_app.router.dependencies] == [bind_injection_ctx, _item_id_dep]
|
|
141
144
|
|
|
142
145
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|