Repository navigation
Fix #193 by enabling listener middleware to return a response #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
seratch
merged 2 commits into
slackapi:main
from
seratch:issue-193-middleware-listener-response
Jan 7, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,8 @@ | ||
| coverage: | ||
| status: | ||
| project: | ||
| default: | ||
| threshold: 0.3% | ||
| patch: | ||
| default: | ||
| target: 50% |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import inspect | ||
| import logging | ||
| import os | ||
| import time | ||
| from typing import Optional, List, Union, Callable, Pattern, Dict, Awaitable, Sequence | ||
|
|
||
| from aiohttp import web | ||
|
|
@@ -39,6 +40,7 @@ | |
| error_oauth_settings_invalid_type_async, | ||
| error_oauth_flow_invalid_type_async, | ||
| warning_bot_only_conflicts, | ||
| debug_return_listener_middleware_response, | ||
| ) | ||
| from slack_bolt.lazy_listener.asyncio_runner import AsyncioLazyListenerRunner | ||
| from slack_bolt.listener.async_listener import AsyncListener, AsyncCustomListener | ||
|
|
@@ -359,6 +361,7 @@ async def async_dispatch(self, req: AsyncBoltRequest) -> BoltResponse: | |
| :param req: An incoming request from Slack. | ||
| :return: The response generated by this Bolt app. | ||
| """ | ||
| starting_time = time.time() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Debug logs used to track only the time spent in a listener function. Starting here is more accurate. |
||
| self._init_context(req) | ||
|
|
||
| resp: BoltResponse = BoltResponse(status=200, body="") | ||
|
|
@@ -386,14 +389,28 @@ async def async_middleware_next(): | |
| self._framework_logger.debug(debug_checking_listener(listener_name)) | ||
| if await listener.async_matches(req=req, resp=resp): | ||
| # run all the middleware attached to this listener first | ||
| resp, next_was_not_called = await listener.run_async_middleware( | ||
| req=req, resp=resp | ||
| ) | ||
| ( | ||
| middleware_resp, | ||
| next_was_not_called, | ||
| ) = await listener.run_async_middleware(req=req, resp=resp) | ||
| if next_was_not_called: | ||
| if middleware_resp is not None: | ||
| if self._framework_logger.level <= logging.DEBUG: | ||
| debug_message = debug_return_listener_middleware_response( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An example log message: |
||
| listener_name, | ||
| middleware_resp.status, | ||
| middleware_resp.body, | ||
| starting_time, | ||
| ) | ||
| self._framework_logger.debug(debug_message) | ||
| return middleware_resp | ||
| # The last listener middleware didn't call next() method. | ||
| # This means the listener is not for this incoming request. | ||
| continue | ||
|
|
||
| if middleware_resp is not None: | ||
| resp = middleware_resp | ||
|
|
||
| self._framework_logger.debug(debug_running_listener(listener_name)) | ||
| listener_response: Optional[ | ||
| BoltResponse | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,102 @@ | ||
| import json | ||
| from time import time | ||
|
|
||
| from slack_sdk.signature import SignatureVerifier | ||
| from slack_sdk.web import WebClient | ||
|
|
||
| from slack_bolt import BoltResponse | ||
| from slack_bolt.app import App | ||
| from slack_bolt.request import BoltRequest | ||
| from tests.mock_web_api_server import ( | ||
| setup_mock_web_api_server, | ||
| cleanup_mock_web_api_server, | ||
| ) | ||
| from tests.utils import remove_os_env_temporarily, restore_os_env | ||
|
|
||
|
|
||
| class TestListenerMiddleware: | ||
| signing_secret = "secret" | ||
| valid_token = "xoxb-valid" | ||
| mock_api_server_base_url = "http://localhost:8888" | ||
| signature_verifier = SignatureVerifier(signing_secret) | ||
| web_client = WebClient( | ||
| token=valid_token, | ||
| base_url=mock_api_server_base_url, | ||
| ) | ||
|
|
||
| def setup_method(self): | ||
| self.old_os_env = remove_os_env_temporarily() | ||
| setup_mock_web_api_server(self) | ||
|
|
||
| def teardown_method(self): | ||
| cleanup_mock_web_api_server(self) | ||
| restore_os_env(self.old_os_env) | ||
|
|
||
| body = { | ||
| "type": "shortcut", | ||
| "token": "verification_token", | ||
| "action_ts": "111.111", | ||
| "team": { | ||
| "id": "T111", | ||
| "domain": "workspace-domain", | ||
| "enterprise_id": "E111", | ||
| "enterprise_name": "Org Name", | ||
| }, | ||
| "user": {"id": "W111", "username": "primary-owner", "team_id": "T111"}, | ||
| "callback_id": "test-shortcut", | ||
| "trigger_id": "111.111.xxxxxx", | ||
| } | ||
|
|
||
| def build_request(self) -> BoltRequest: | ||
| timestamp, body = str(int(time())), json.dumps(self.body) | ||
| return BoltRequest( | ||
| body=body, | ||
| headers={ | ||
| "content-type": ["application/json"], | ||
| "x-slack-signature": [ | ||
| self.signature_verifier.generate_signature( | ||
| body=body, | ||
| timestamp=timestamp, | ||
| ) | ||
| ], | ||
| "x-slack-request-timestamp": [timestamp], | ||
| }, | ||
| ) | ||
|
|
||
| def test_return_response(self): | ||
| app = App( | ||
| client=self.web_client, | ||
| signing_secret=self.signing_secret, | ||
| ) | ||
|
|
||
| @app.shortcut( | ||
| constraints="test-shortcut", | ||
| middleware=[listener_middleware_returning_response], | ||
| ) | ||
| def handle(ack): | ||
| ack() | ||
|
|
||
| response = app.dispatch(self.build_request()) | ||
| assert response.status == 200 | ||
| assert response.body == "listener middleware" | ||
|
|
||
| def test_next(self): | ||
| app = App( | ||
| client=self.web_client, | ||
| signing_secret=self.signing_secret, | ||
| ) | ||
|
|
||
| @app.shortcut(constraints="test-shortcut", middleware=[just_next]) | ||
| def handle(ack): | ||
| ack() | ||
|
|
||
| response = app.dispatch(self.build_request()) | ||
| assert response.status == 200 | ||
|
|
||
|
|
||
| def listener_middleware_returning_response(): | ||
| return BoltResponse(status=200, body="listener middleware") | ||
|
|
||
|
|
||
| def just_next(next): | ||
| next() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be the case when a listener middleware sets the response but also calls next, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, it is 👍