Skip to content

Commit

Permalink
Fix slackapi#793 by adding pyramid_request property to Bolt context (s…
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch authored Dec 19, 2022
1 parent 9f64401 commit 114f2f0
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions slack_bolt/adapter/pyramid/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,34 @@ def handle(self, request: Request) -> Response:
if self.app.oauth_flow is not None:
oauth_flow: OAuthFlow = self.app.oauth_flow
if request.path == oauth_flow.install_path:
bolt_resp = oauth_flow.handle_installation(to_bolt_request(request))
bolt_req = _attach_pyramid_request_to_context(to_bolt_request(request), request)
bolt_resp = oauth_flow.handle_installation(bolt_req)
return to_pyramid_response(bolt_resp)
elif request.path == oauth_flow.redirect_uri_path:
bolt_resp = oauth_flow.handle_callback(to_bolt_request(request))
bolt_req = _attach_pyramid_request_to_context(to_bolt_request(request), request)
bolt_resp = oauth_flow.handle_callback(bolt_req)
return to_pyramid_response(bolt_resp)
elif request.method == "POST":
bolt_req = to_bolt_request(request)
bolt_req = _attach_pyramid_request_to_context(to_bolt_request(request), request)
bolt_resp = self.app.dispatch(bolt_req)
return to_pyramid_response(bolt_resp)

return Response(status=404, body="Not found")


def _attach_pyramid_request_to_context(
bolt_req: BoltRequest,
request: Request,
) -> BoltRequest:
# To enable developers to access request-scope attributes such as dbsession,
# this adapter exposes the underlying pyramid_request object to Bolt listeners
#
# Developers can access request props this way:
# @app.event("app_mention")
# def handle_app_mention_events(context, logger):
# req = context["pyramid_request"]
# all = req.dbsession.query(MyModel).all()
# logger.info(all)
#
bolt_req.context["pyramid_request"] = request
return bolt_req

0 comments on commit 114f2f0

Please sign in to comment.