-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use proxy on asgi callbacks on request
- Loading branch information
1 parent
139daec
commit 6be2f0c
Showing
12 changed files
with
109 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from collections.abc import Callable | ||
from functools import partial | ||
from typing import Any, Concatenate, ParamSpec | ||
|
||
__all__ = ("CallableProxy",) | ||
|
||
P = ParamSpec("P") | ||
|
||
|
||
class CallableProxy: | ||
__slots__ = ("func",) | ||
|
||
def __init__(self, func: Callable[P, Any]): | ||
self.func = func | ||
|
||
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> Any: | ||
return self.func(*args, **kwargs) | ||
|
||
def wrap(self, wrapper: Callable[Concatenate[Callable[P, Any], P], Any]): | ||
self.func = partial(wrapper, self.func) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from asgikit.util.callable_proxy import CallableProxy | ||
|
||
|
||
def test_call(): | ||
def func() -> int: | ||
return 1 | ||
|
||
proxy = CallableProxy(func) | ||
assert proxy() == 1 | ||
|
||
|
||
async def test_call_async(): | ||
async def func() -> int: | ||
return 1 | ||
|
||
proxy = CallableProxy(func) | ||
assert await proxy() == 1 | ||
|
||
|
||
def test_call_params(): | ||
def func(a: int, b: int) -> int: | ||
return a + b | ||
|
||
proxy = CallableProxy(func) | ||
assert proxy(1, 2) == 3 | ||
|
||
|
||
def test_wrap(): | ||
def func() -> int: | ||
return 1 | ||
|
||
def wrapper(f) -> int: | ||
return f() + 1 | ||
|
||
proxy = CallableProxy(func) | ||
proxy.wrap(wrapper) | ||
assert proxy() == 2 | ||
|
||
|
||
async def test_wrap_async(): | ||
async def func() -> int: | ||
return 1 | ||
|
||
async def wrapper(f) -> int: | ||
return await f() + 1 | ||
|
||
proxy = CallableProxy(func) | ||
proxy.wrap(wrapper) | ||
assert await proxy() == 2 | ||
|
||
|
||
async def test_wrap_async_non_async_wrapper(): | ||
async def func() -> int: | ||
return 1 | ||
|
||
def wrapper(f) -> int: | ||
return f() | ||
|
||
proxy = CallableProxy(func) | ||
proxy.wrap(wrapper) | ||
assert await proxy() == 1 | ||
|
||
|
||
def test_wrap_params(): | ||
def func(a: int, b: int) -> int: | ||
return a + b | ||
|
||
def wrapper(f, *args, **kwargs) -> int: | ||
return f(*args, **kwargs) + 1 | ||
|
||
proxy = CallableProxy(func) | ||
proxy.wrap(wrapper) | ||
assert proxy(2, 3) == 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters