forked from slackapi/bolt-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.py
More file actions
58 lines (49 loc) · 2.01 KB
/
response.py
File metadata and controls
58 lines (49 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import json
from http.cookies import SimpleCookie
from typing import Union, Dict, Optional, Sequence
class BoltResponse:
status: int
body: str
headers: Dict[str, Sequence[str]]
def __init__(
self,
*,
status: int,
body: Union[str, dict] = "",
headers: Optional[Dict[str, Union[str, Sequence[str]]]] = None,
):
"""The response from a Bolt app.
:param status: HTTP status code
:param body: The response body (plain text response is supported)
:param headers: The response headers.
"""
self.status: int = status
self.body: str = json.dumps(body) if isinstance(body, dict) else body
self.headers: Dict[str, Sequence[str]] = {}
if headers is not None:
for name, value in headers.items():
if value is None:
continue
if isinstance(value, list):
self.headers[name.lower()] = value
elif isinstance(value, set):
self.headers[name.lower()] = list(value)
else:
self.headers[name.lower()] = [str(value)]
if "content-type" not in self.headers.keys():
if self.body and self.body.startswith("{"):
self.headers["content-type"] = ["application/json;charset=utf-8"]
else:
self.headers["content-type"] = ["text/plain;charset=utf-8"]
def first_headers(self) -> Dict[str, str]:
return {k: list(v)[0] for k, v in self.headers.items()}
def first_headers_without_set_cookie(self) -> Dict[str, str]:
return {k: list(v)[0] for k, v in self.headers.items() if k != "set-cookie"}
def cookies(self) -> Sequence[SimpleCookie]:
header_values = self.headers.get("set-cookie", [])
return [self._to_simple_cookie(v) for v in header_values]
@staticmethod
def _to_simple_cookie(header_value: str) -> SimpleCookie:
c = SimpleCookie()
c.load(header_value)
return c