Skip to content

Commit 2128645

Browse files
committed
update sanic response via add_cookie
1 parent 072714f commit 2128645

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

slack_bolt/adapter/sanic/async_handler.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,36 @@ def to_async_bolt_request(req: Request, addition_context_properties: Optional[Di
2424

2525

2626
def to_sanic_response(bolt_resp: BoltResponse) -> HTTPResponse:
27+
28+
# Create the HTTPResponse with BoltResponse attributes
2729
resp = HTTPResponse(
2830
status=bolt_resp.status,
2931
body=bolt_resp.body,
3032
headers=bolt_resp.first_headers_without_set_cookie(),
3133
)
34+
35+
# Iterate over cookies and add them using Sanic's add_cookie method
3236
for cookie in bolt_resp.cookies():
33-
for name, c in cookie.items():
34-
resp.cookies[name] = c.value
37+
for key, c in cookie.items():
38+
# Convert "expires" field if provided
3539
expire_value = c.get("expires")
36-
if expire_value is not None and expire_value != "":
37-
expire = datetime.strptime(expire_value, "%a, %d %b %Y %H:%M:%S %Z")
38-
resp.cookies[name]["expires"] = expire
39-
resp.cookies[name]["path"] = c.get("path")
40-
resp.cookies[name]["domain"] = c.get("domain")
41-
if c.get("max-age") is not None and len(c.get("max-age")) > 0: # type: ignore[arg-type]
42-
resp.cookies[name]["max-age"] = int(c.get("max-age")) # type: ignore[arg-type]
43-
resp.cookies[name]["secure"] = True
44-
resp.cookies[name]["httponly"] = True
40+
expires = datetime.strptime(expire_value, "%a, %d %b %Y %H:%M:%S %Z") if expire_value else None
41+
42+
# Convert "max-age" if provided
43+
max_age = int(c["max-age"]) if c.get("max-age") else None
44+
45+
# Add cookie with Sanic's add_cookie method
46+
resp.add_cookie(
47+
key=key,
48+
value=c.value,
49+
expires=expires,
50+
path=c.get("path"),
51+
domain=c.get("domain"),
52+
max_age=max_age,
53+
secure=True,
54+
httponly=True,
55+
)
56+
4557
return resp
4658

4759

0 commit comments

Comments
 (0)