11from typing import Dict , Optional , Union , Any , Sequence
22
33from slack_bolt .context .async_context import AsyncBoltContext
4+ from slack_bolt .error import BoltError
45from slack_bolt .request .async_internals import build_async_context
56from slack_bolt .request .internals import (
67 parse_query ,
78 parse_body ,
89 build_normalized_headers ,
910 extract_content_type ,
11+ error_message_raw_body_required_in_http_mode ,
12+ error_message_unknown_request_body_type ,
1013)
1114
1215
@@ -19,31 +22,42 @@ class AsyncBoltRequest:
1922 context : AsyncBoltContext
2023 lazy_only : bool
2124 lazy_function_name : Optional [str ]
25+ mode : str # either "http" or "socket_mode"
2226
2327 def __init__ (
2428 self ,
2529 * ,
26- body : str ,
30+ body : Union [ str , dict ] ,
2731 query : Optional [Union [str , Dict [str , str ], Dict [str , Sequence [str ]]]] = None ,
2832 headers : Optional [Dict [str , Union [str , Sequence [str ]]]] = None ,
2933 context : Optional [Dict [str , str ]] = None ,
34+ mode : str = "http" , # either "http" or "socket_mode"
3035 ):
3136 """Request to a Bolt app.
3237
33- :param body: The raw request body (only plain text is supported)
38+ :param body: The raw request body (only plain text is supported for "http" mode )
3439 :param query: The query string data in any data format.
3540 :param headers: The request headers.
3641 :param context: The context in this request.
42+ :param mode: The mode used for this request. (either "http" or "socket_mode")
3743 """
38- self .raw_body = body
44+ if mode == "http" and not isinstance (body , str ):
45+ raise BoltError (error_message_raw_body_required_in_http_mode ())
46+ self .raw_body = body if mode == "http" else ""
3947 self .query = parse_query (query )
4048 self .headers = build_normalized_headers (headers )
4149 self .content_type = extract_content_type (self .headers )
42- self .body = parse_body (self .raw_body , self .content_type )
50+ if isinstance (body , str ):
51+ self .body = parse_body (self .raw_body , self .content_type )
52+ elif isinstance (body , dict ):
53+ self .body = body
54+ else :
55+ raise BoltError (error_message_unknown_request_body_type ())
4356 self .context = build_async_context (
4457 AsyncBoltContext (context if context else {}), self .body
4558 )
4659 self .lazy_only = self .headers .get ("x-slack-bolt-lazy-only" , [False ])[0 ]
4760 self .lazy_function_name = self .headers .get (
4861 "x-slack-bolt-lazy-function-name" , [None ]
4962 )[0 ]
63+ self .mode = mode
0 commit comments