Skip to content

Commit

Permalink
add *args to the middleware interface
Browse files Browse the repository at this point in the history
  • Loading branch information
danylott committed Mar 9, 2021
1 parent f4a9372 commit fa72c40
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[flake8]
ignore = E203, E266, E501, W503, F403, F401, F811
max-line-length = 89
max-complexity = 18
max-complexity = 28
select = B,C,E,F,W,T4,B9
2 changes: 1 addition & 1 deletion examples/simple_examples/async_request_diff_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async def request():

@app.listen("some.request.subject.123", data_type=str)
async def subject_for_requests_listener(msg):
if not type(msg) == str:
if not type(msg.data) == str:
raise Exception("Wrong datatype!")
return {"success": True, "data": "request has been processed"}

Expand Down
20 changes: 11 additions & 9 deletions panini/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def listen(
data_type=dict,
):
def wrapper(function):
function = _MiddlewareManager._wrap_function_by_middleware(function, "listen")
function = _MiddlewareManager._wrap_function_by_middleware(
function, "listen"
)
function = _EventManager.wrap_function_by_validator(function, validator)
if type(subject) is list:
for t in subject:
Expand Down Expand Up @@ -201,23 +203,23 @@ def add_middleware(middleware_cls: Type[Middleware], *args, **kwargs):

if "send_any" in middleware_cls.__dict__:
if "send_publish" not in middleware_cls.__dict__:
_MiddlewareManager.MIDDLEWARE[f"send_publish_middleware"].append(
_MiddlewareManager.MIDDLEWARE["send_publish_middleware"].append(
middleware_obj.send_any
)

if "send_request" not in middleware_cls.__dict__:
_MiddlewareManager.MIDDLEWARE[f"send_request_middleware"].append(
_MiddlewareManager.MIDDLEWARE["send_request_middleware"].append(
middleware_obj.send_any
)

if "listen_any" in middleware_cls.__dict__:
if "listen_publish" not in middleware_cls.__dict__:
_MiddlewareManager.MIDDLEWARE[f"listen_publish_middleware"].append(
_MiddlewareManager.MIDDLEWARE["listen_publish_middleware"].append(
middleware_obj.listen_any
)

if "listen_request" not in middleware_cls.__dict__:
_MiddlewareManager.MIDDLEWARE[f"listen_request_middleware"].append(
_MiddlewareManager.MIDDLEWARE["listen_request_middleware"].append(
middleware_obj.listen_any
)

Expand All @@ -238,11 +240,11 @@ def _wrap_function_by_middleware(
def wrap_function_by_send_middleware(
func: Callable, single_middleware
) -> Callable:
def next_wrapper(subject: str, message, **kwargs):
return single_middleware(subject, message, func, **kwargs)
def next_wrapper(subject: str, message, *args, **kwargs):
return single_middleware(subject, message, func, *args, **kwargs)

async def aio_next_wrapper(subject: str, message, **kwargs):
return await single_middleware(subject, message, func, **kwargs)
async def aio_next_wrapper(subject: str, message, *args, **kwargs):
return await single_middleware(subject, message, *args, func, **kwargs)

if asyncio.iscoroutinefunction(single_middleware):
return aio_next_wrapper
Expand Down
6 changes: 3 additions & 3 deletions panini/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Middleware:
def __init__(self, *args, **kwargs):
pass

async def send_publish(self, subject: str, message, publish_func, **kwargs):
async def send_publish(self, subject: str, message, publish_func, *args, **kwargs):
"""
:param subject: str
:param message: any of supported types
Expand All @@ -19,7 +19,7 @@ async def listen_publish(self, msg, callback):
"""
pass

async def send_request(self, subject: str, message, request_func, **kwargs):
async def send_request(self, subject: str, message, request_func, *args, **kwargs):
"""
:param subject: str
:param message: any of supported types
Expand All @@ -36,7 +36,7 @@ async def listen_request(self, msg, callback):
"""
pass

async def send_any(self, subject: str, message, send_func, **kwargs):
async def send_any(self, subject: str, message, send_func, *args, **kwargs):
"""
:param subject: str
:param message: any of supported types
Expand Down

0 comments on commit fa72c40

Please sign in to comment.