예제 #1
0
    def test_parse_query(self):
        expected = {"foo": ["bar"], "baz": ["123"]}

        q = parse_query("foo=bar&baz=123")
        assert q == expected

        q = parse_query({"foo": "bar", "baz": "123"})
        assert q == expected

        q = parse_query({"foo": ["bar"], "baz": ["123"]})
        assert q == expected

        with pytest.raises(ValueError):
            parse_query({"foo": {"bar": "ZZZ"}, "baz": {"123": "111"}})
예제 #2
0
    def __init__(
        self,
        *,
        body: str,
        query: Optional[Union[str, Dict[str, str],
                              Dict[str, Sequence[str]]]] = None,
        headers: Optional[Dict[str, Union[str, Sequence[str]]]] = None,
        context: Optional[Dict[str, str]] = None,
    ):
        """Request to a Bolt app.

        :param body: The raw request body (only plain text is supported)
        :param query: The query string data in any data format.
        :param headers: The request headers.
        :param context: The context in this request.
        """
        self.raw_body = body
        self.query = parse_query(query)
        self.headers = build_normalized_headers(headers)
        self.content_type = extract_content_type(self.headers)
        self.body = parse_body(self.raw_body, self.content_type)
        self.context = build_async_context(
            AsyncBoltContext(context if context else {}), self.body)
        self.lazy_only = self.headers.get("x-slack-bolt-lazy-only", [False])[0]
        self.lazy_function_name = self.headers.get(
            "x-slack-bolt-lazy-function-name", [None])[0]
예제 #3
0
    def __init__(
            self,
            *,
            body: Union[str, dict],
            query: Optional[Union[str, Dict[str, str],
                                  Dict[str, Sequence[str]]]] = None,
            headers: Optional[Dict[str, Union[str, Sequence[str]]]] = None,
            context: Optional[Dict[str, str]] = None,
            mode: str = "http",  # either "http" or "socket_mode"
    ):
        """Request to a Bolt app.

        Args:
            body: The raw request body (only plain text is supported for "http" mode)
            query: The query string data in any data format.
            headers: The request headers.
            context: The context in this request.
            mode: The mode used for this request. (either "http" or "socket_mode")
        """

        if mode == "http":
            # HTTP Mode
            if body is not None and not isinstance(body, str):
                raise BoltError(error_message_raw_body_required_in_http_mode())
            self.raw_body = body if body is not None else ""
        else:
            # Socket Mode
            if body is not None and isinstance(body, str):
                self.raw_body = body
            else:
                # We don't convert the dict value to str
                # as doing so does not guarantee to keep the original structure/format.
                self.raw_body = ""

        self.query = parse_query(query)
        self.headers = build_normalized_headers(headers)
        self.content_type = extract_content_type(self.headers)

        if isinstance(body, str):
            self.body = parse_body(self.raw_body, self.content_type)
        elif isinstance(body, dict):
            self.body = body
        else:
            self.body = {}

        self.context = build_async_context(
            AsyncBoltContext(context if context else {}), self.body)
        self.lazy_only = bool(
            self.headers.get("x-slack-bolt-lazy-only", [False])[0])
        self.lazy_function_name = self.headers.get(
            "x-slack-bolt-lazy-function-name", [None])[0]
        self.mode = mode
예제 #4
0
 def __init__(
     self,
     *,
     body: str,
     query: Optional[Union[str, Dict[str, str], Dict[str,
                                                     List[str]]]] = None,
     # many framework use Dict[str, str] but the reality is Dict[str, List[str]]
     headers: Optional[Dict[str, Union[str, List[str]]]] = None,
     context: Optional[Dict[str, str]] = None,
 ):
     self.body = body
     self.query = parse_query(query)
     self.headers = build_normalized_headers(headers)
     self.content_type = extract_content_type(self.headers)
     self.payload = parse_payload(self.body, self.content_type)
     self.context = build_async_context(
         AsyncBoltContext(context if context else {}), self.payload)
예제 #5
0
    def __init__(
            self,
            *,
            body: Union[str, dict],
            query: Optional[Union[str, Dict[str, str],
                                  Dict[str, Sequence[str]]]] = None,
            headers: Optional[Dict[str, Union[str, Sequence[str]]]] = None,
            context: Optional[Dict[str, str]] = None,
            mode: str = "http",  # either "http" or "socket_mode"
    ):
        """Request to a Bolt app.

        Args:
            body: The raw request body (only plain text is supported for "http" mode)
            query: The query string data in any data format.
            headers: The request headers.
            context: The context in this request.
            mode: The mode used for this request. (either "http" or "socket_mode")
        """
        if mode == "http" and not isinstance(body, str):
            raise BoltError(error_message_raw_body_required_in_http_mode())
        self.raw_body = body if mode == "http" else ""
        self.query = parse_query(query)
        self.headers = build_normalized_headers(headers)
        self.content_type = extract_content_type(self.headers)
        if isinstance(body, str):
            self.body = parse_body(self.raw_body, self.content_type)
        elif isinstance(body, dict):
            self.body = body
        else:
            raise BoltError(error_message_unknown_request_body_type())

        self.context = build_context(BoltContext(context if context else {}),
                                     self.body)
        self.lazy_only = self.headers.get("x-slack-bolt-lazy-only", [False])[0]
        self.lazy_function_name = self.headers.get(
            "x-slack-bolt-lazy-function-name", [None])[0]
        self.mode = mode