async def test_instantiation(self): matcher: AsyncListenerMatcher = AsyncCustomListenerMatcher( app_name="foo", func=func, ) resp = BoltResponse(status=201) req = AsyncBoltRequest(body='payload={"result":true}') result = await matcher.async_matches(req, resp) assert result is True req = AsyncBoltRequest(body='payload={"result":false}') result = await matcher.async_matches(req, resp) assert result is False
def _register_listener( self, functions: Sequence[Callable[..., Awaitable[Optional[BoltResponse]]]], primary_matcher: AsyncListenerMatcher, matchers: Optional[Sequence[Callable[..., Awaitable[bool]]]], middleware: Optional[Sequence[Union[Callable, AsyncMiddleware]]], auto_acknowledgement: bool = False, ) -> Optional[Callable[..., Awaitable[Optional[BoltResponse]]]]: value_to_return = None if not isinstance(functions, list): functions = list(functions) if len(functions) == 1: # In the case where the function is registered using decorator, # the registration should return the original function. value_to_return = functions[0] for func in functions: if not inspect.iscoroutinefunction(func): name = get_name_for_callable(func) raise BoltError(error_listener_function_must_be_coro_func(name)) listener_matchers = [ AsyncCustomListenerMatcher(app_name=self.name, func=f) for f in (matchers or []) ] listener_matchers.insert(0, primary_matcher) listener_middleware = [] for m in middleware or []: if isinstance(m, AsyncMiddleware): listener_middleware.append(m) elif isinstance(m, Callable) and inspect.iscoroutinefunction(m): listener_middleware.append( AsyncCustomMiddleware(app_name=self.name, func=m) ) else: raise ValueError(error_unexpected_listener_middleware(type(m))) self._async_listeners.append( AsyncCustomListener( app_name=self.name, ack_function=functions.pop(0), lazy_functions=functions, matchers=listener_matchers, middleware=listener_middleware, auto_acknowledgement=auto_acknowledgement, ) ) return value_to_return
def _register_listener( self, func: Callable[..., Awaitable[BoltResponse]], primary_matcher: AsyncListenerMatcher, matchers: Optional[List[Callable[..., Awaitable[bool]]]], middleware: Optional[List[Union[Callable, AsyncMiddleware]]], auto_acknowledgement: bool = False, ) -> None: if not inspect.iscoroutinefunction(func): name = func.__name__ raise BoltError( f"The listener function ({name}) is not a coroutine function.") listener_matchers = [ AsyncCustomListenerMatcher(app_name=self.name, func=f) for f in (matchers or []) ] listener_matchers.insert(0, primary_matcher) listener_middleware = [] for m in middleware or []: if isinstance(m, AsyncMiddleware): listener_middleware.append(m) elif isinstance(m, Callable) and inspect.iscoroutinefunction(m): listener_middleware.append( AsyncCustomMiddleware(app_name=self.name, func=m)) else: raise ValueError( f"async function is required for AsyncApp's listener middleware: {type(m)}" ) self._async_listeners.append( AsyncCustomListener( app_name=self.name, func=func, matchers=listener_matchers, middleware=listener_middleware, auto_acknowledgement=auto_acknowledgement, ))