aboutsummaryrefslogtreecommitdiffstats
path: root/slack/util.py
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2024-01-06 16:32:14 +0100
committerTrygve Aaberge <trygveaa@gmail.com>2024-02-18 11:32:54 +0100
commitaf2c56f96d8be2f783332f5e4c502e8d15835b32 (patch)
tree66a7620e5ef5a288f0f17e0cb9473a2188cbce79 /slack/util.py
parent30e500d4067a161a3e6ea3161ff3440b6959d263 (diff)
downloadwee-slack-af2c56f96d8be2f783332f5e4c502e8d15835b32.tar.gz
URL encode cookies if not encoded
The cookies need to be encoded for the Slack API to accept them, so this makes it work if the config is set with cookies that are not encoded.
Diffstat (limited to 'slack/util.py')
-rw-r--r--slack/util.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/slack/util.py b/slack/util.py
index c988f1f..14d18dc 100644
--- a/slack/util.py
+++ b/slack/util.py
@@ -12,6 +12,7 @@ from typing import (
TypeVar,
Union,
)
+from urllib.parse import quote, unquote
import weechat
@@ -45,6 +46,22 @@ def unhtmlescape(text: str) -> str:
return text.replace("&lt;", "<").replace("&gt;", ">").replace("&amp;", "&")
+def url_encode_if_not_encoded(value: str) -> str:
+ is_encoded = value != unquote(value)
+ if is_encoded:
+ return value
+ else:
+ return quote(value)
+
+
+def get_cookies(cookie_config: str) -> str:
+ cookie_pairs = [cookie.split("=", 1) for cookie in cookie_config.split(";")]
+ for cookie_pair in cookie_pairs:
+ cookie_pair[0] = cookie_pair[0].strip()
+ cookie_pair[1] = url_encode_if_not_encoded(cookie_pair[1].strip())
+ return "; ".join("=".join(cookie_pair) for cookie_pair in cookie_pairs)
+
+
# From https://github.com/more-itertools/more-itertools/blob/v10.1.0/more_itertools/recipes.py#L93-L106
def take(n: int, iterable: Iterable[T]) -> List[T]:
"""Return first *n* items of the iterable as a list.