@@ -73,17 +73,37 @@ <h1 class="title">Module <code>slack_bolt.adapter.pyramid.handler</code></h1>
7373 if self.app.oauth_flow is not None:
7474 oauth_flow: OAuthFlow = self.app.oauth_flow
7575 if request.path == oauth_flow.install_path:
76- bolt_resp = oauth_flow.handle_installation(to_bolt_request(request))
76+ bolt_req = _attach_pyramid_request_to_context(to_bolt_request(request), request)
77+ bolt_resp = oauth_flow.handle_installation(bolt_req)
7778 return to_pyramid_response(bolt_resp)
7879 elif request.path == oauth_flow.redirect_uri_path:
79- bolt_resp = oauth_flow.handle_callback(to_bolt_request(request))
80+ bolt_req = _attach_pyramid_request_to_context(to_bolt_request(request), request)
81+ bolt_resp = oauth_flow.handle_callback(bolt_req)
8082 return to_pyramid_response(bolt_resp)
8183 elif request.method == "POST":
82- bolt_req = to_bolt_request(request)
84+ bolt_req = _attach_pyramid_request_to_context( to_bolt_request(request), request)
8385 bolt_resp = self.app.dispatch(bolt_req)
8486 return to_pyramid_response(bolt_resp)
8587
86- return Response(status=404, body="Not found")</ code > </ pre >
88+ return Response(status=404, body="Not found")
89+
90+
91+ def _attach_pyramid_request_to_context(
92+ bolt_req: BoltRequest,
93+ request: Request,
94+ ) -> BoltRequest:
95+ # To enable developers to access request-scope attributes such as dbsession,
96+ # this adapter exposes the underlying pyramid_request object to Bolt listeners
97+ #
98+ # Developers can access request props this way:
99+ # @app.event("app_mention")
100+ # def handle_app_mention_events(context, logger):
101+ # req = context["pyramid_request"]
102+ # all = req.dbsession.query(MyModel).all()
103+ # logger.info(all)
104+ #
105+ bolt_req.context["pyramid_request"] = request
106+ return bolt_req</ code > </ pre >
87107</ details >
88108</ section >
89109< section >
@@ -164,13 +184,15 @@ <h2 class="section-title" id="header-classes">Classes</h2>
164184 if self.app.oauth_flow is not None:
165185 oauth_flow: OAuthFlow = self.app.oauth_flow
166186 if request.path == oauth_flow.install_path:
167- bolt_resp = oauth_flow.handle_installation(to_bolt_request(request))
187+ bolt_req = _attach_pyramid_request_to_context(to_bolt_request(request), request)
188+ bolt_resp = oauth_flow.handle_installation(bolt_req)
168189 return to_pyramid_response(bolt_resp)
169190 elif request.path == oauth_flow.redirect_uri_path:
170- bolt_resp = oauth_flow.handle_callback(to_bolt_request(request))
191+ bolt_req = _attach_pyramid_request_to_context(to_bolt_request(request), request)
192+ bolt_resp = oauth_flow.handle_callback(bolt_req)
171193 return to_pyramid_response(bolt_resp)
172194 elif request.method == "POST":
173- bolt_req = to_bolt_request(request)
195+ bolt_req = _attach_pyramid_request_to_context( to_bolt_request(request), request)
174196 bolt_resp = self.app.dispatch(bolt_req)
175197 return to_pyramid_response(bolt_resp)
176198
@@ -192,13 +214,15 @@ <h3>Methods</h3>
192214 if self.app.oauth_flow is not None:
193215 oauth_flow: OAuthFlow = self.app.oauth_flow
194216 if request.path == oauth_flow.install_path:
195- bolt_resp = oauth_flow.handle_installation(to_bolt_request(request))
217+ bolt_req = _attach_pyramid_request_to_context(to_bolt_request(request), request)
218+ bolt_resp = oauth_flow.handle_installation(bolt_req)
196219 return to_pyramid_response(bolt_resp)
197220 elif request.path == oauth_flow.redirect_uri_path:
198- bolt_resp = oauth_flow.handle_callback(to_bolt_request(request))
221+ bolt_req = _attach_pyramid_request_to_context(to_bolt_request(request), request)
222+ bolt_resp = oauth_flow.handle_callback(bolt_req)
199223 return to_pyramid_response(bolt_resp)
200224 elif request.method == "POST":
201- bolt_req = to_bolt_request(request)
225+ bolt_req = _attach_pyramid_request_to_context( to_bolt_request(request), request)
202226 bolt_resp = self.app.dispatch(bolt_req)
203227 return to_pyramid_response(bolt_resp)
204228
0 commit comments