Skip to content

Commit a9ead73

Browse files
Add option for additional context to Sanic adapter handler (#1135)
1 parent 9dcc0fb commit a9ead73

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

slack_bolt/adapter/sanic/async_handler.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime
2+
from typing import Any, Dict, Optional
23

34
from sanic.request import Request
45
from sanic.response import HTTPResponse
@@ -8,13 +9,19 @@
89
from slack_bolt.oauth.async_oauth_flow import AsyncOAuthFlow
910

1011

11-
def to_async_bolt_request(req: Request) -> AsyncBoltRequest:
12-
return AsyncBoltRequest(
12+
def to_async_bolt_request(req: Request, addition_context_properties: Optional[Dict[str, Any]] = None) -> AsyncBoltRequest:
13+
request = AsyncBoltRequest(
1314
body=req.body.decode("utf-8"),
1415
query=req.query_string,
1516
headers=req.headers, # type: ignore[arg-type]
1617
)
1718

19+
if addition_context_properties is not None:
20+
for k, v in addition_context_properties.items():
21+
request.context[k] = v
22+
23+
return request
24+
1825

1926
def to_sanic_response(bolt_resp: BoltResponse) -> HTTPResponse:
2027
resp = HTTPResponse(
@@ -42,19 +49,19 @@ class AsyncSlackRequestHandler:
4249
def __init__(self, app: AsyncApp):
4350
self.app = app
4451

45-
async def handle(self, req: Request) -> HTTPResponse:
52+
async def handle(self, req: Request, addition_context_properties: Optional[Dict[str, Any]] = None) -> HTTPResponse:
4653
if req.method == "GET":
4754
if self.app.oauth_flow is not None:
4855
oauth_flow: AsyncOAuthFlow = self.app.oauth_flow
4956
if req.path == oauth_flow.install_path:
50-
bolt_resp = await oauth_flow.handle_installation(to_async_bolt_request(req))
57+
bolt_resp = await oauth_flow.handle_installation(to_async_bolt_request(req, addition_context_properties))
5158
return to_sanic_response(bolt_resp)
5259
elif req.path == oauth_flow.redirect_uri_path:
53-
bolt_resp = await oauth_flow.handle_callback(to_async_bolt_request(req))
60+
bolt_resp = await oauth_flow.handle_callback(to_async_bolt_request(req, addition_context_properties))
5461
return to_sanic_response(bolt_resp)
5562

5663
elif req.method == "POST":
57-
bolt_resp = await self.app.async_dispatch(to_async_bolt_request(req))
64+
bolt_resp = await self.app.async_dispatch(to_async_bolt_request(req, addition_context_properties))
5865
return to_sanic_response(bolt_resp)
5966

6067
return HTTPResponse(

0 commit comments

Comments
 (0)