From de3d61474485655b612a5bcbe621b781352db648 Mon Sep 17 00:00:00 2001 From: John Sonnenschein Date: Sat, 19 Mar 2022 21:19:04 -0700 Subject: [PATCH] add quart adapter --- slack_bolt/adapter/quart/__init__.py | 1 + slack_bolt/adapter/quart/async_handler.py | 48 +++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 slack_bolt/adapter/quart/__init__.py create mode 100644 slack_bolt/adapter/quart/async_handler.py diff --git a/slack_bolt/adapter/quart/__init__.py b/slack_bolt/adapter/quart/__init__.py new file mode 100644 index 00000000..cf7d5ec6 --- /dev/null +++ b/slack_bolt/adapter/quart/__init__.py @@ -0,0 +1 @@ +from .async_handler import AsyncSlackRequestHandler diff --git a/slack_bolt/adapter/quart/async_handler.py b/slack_bolt/adapter/quart/async_handler.py new file mode 100644 index 00000000..869544c1 --- /dev/null +++ b/slack_bolt/adapter/quart/async_handler.py @@ -0,0 +1,48 @@ +from quart import Request, Response, make_response + +from slack_bolt import BoltResponse +from slack_bolt.async_app import AsyncApp, AsyncBoltRequest +from slack_bolt.oauth.async_oauth_flow import AsyncOAuthFlow + +async def to_async_bolt_request(req: Request) -> AsyncBoltRequest: + body = await req.get_data(as_text=True) + return AsyncBoltRequest( + body=body, + query=req.query_string.decode("utf-8"), + headers=req.headers, + ) + +async def to_quart_response(bolt_resp: BoltResponse) -> Response: + resp = await make_response(bolt_resp.body, bolt_resp.status) + for k, values in bolt_resp.headers.items(): + if k.lower() == "content-type" and resp.headers.get("content-type") is not None: + # Remove the one set by Flask + resp.headers.pop("content-type") + for v in values: + resp.headers.add_header(k, v) + return resp + +class AsyncSlackRequestHandler: + def __init__(self, app: AsyncApp): # type: ignore + self.app = app + + async def handle(self, req: Request) -> Response: + if req.method == "GET": + if self.app.oauth_flow is not None: + oauth_flow: AsyncOAuthFlow = self.app.oauth_flow + if req.path == oauth_flow.install_path: + bolt_resp = await oauth_flow.handle_installation( + await to_async_bolt_request(req) + ) + return await to_quart_response(bolt_resp) + elif req.path == oauth_flow.redirect_uri_path: + bolt_resp = await oauth_flow.handle_callback( + await to_async_bolt_request(req) + ) + return await to_quart_response(bolt_resp) + + elif req.method == "POST": + bolt_resp = await self.app.async_dispatch(await to_async_bolt_request(req)) + return await to_quart_response(bolt_resp) + + return await make_response("Not Found", 404)