aboutsummaryrefslogtreecommitdiffstats
path: root/wee_slack.py
diff options
context:
space:
mode:
authorTrygve Aaberge <trygveaa@gmail.com>2022-01-30 16:52:09 +0100
committerTrygve Aaberge <trygveaa@gmail.com>2022-09-17 18:56:26 +0200
commit2185a5838e551643de31e10e4d57c617194e74d0 (patch)
tree0a068d539ef7019619cd7d97b725b8835fe676ea /wee_slack.py
parent623426e39f067d1c256ca3115bb33a8ba4af5581 (diff)
downloadwee-slack-2185a5838e551643de31e10e4d57c617194e74d0.tar.gz
Support specifying cookies in SlackRequest
This is a necessary step for #844
Diffstat (limited to 'wee_slack.py')
-rw-r--r--wee_slack.py37
1 files changed, 28 insertions, 9 deletions
diff --git a/wee_slack.py b/wee_slack.py
index 1fd4af1..12b310e 100644
--- a/wee_slack.py
+++ b/wee_slack.py
@@ -923,12 +923,11 @@ def local_process_async_slack_api_request(request, event_router):
random.choice(string.ascii_uppercase + string.digits) for _ in range(4)
)
)
- params = {"useragent": "wee_slack {}".format(SCRIPT_VERSION)}
request.tried()
context = event_router.store_context(request)
w.hook_process_hashtable(
weechat_request,
- params,
+ request.options(),
config.slack_timeout,
"receive_httprequest_callback",
context,
@@ -1429,6 +1428,7 @@ class SlackRequest(object):
metadata=None,
retries=3,
token=None,
+ cookies=None,
callback=None,
):
if team is None and token is None:
@@ -1440,6 +1440,7 @@ class SlackRequest(object):
self.metadata = metadata if metadata else {}
self.retries = retries
self.token = token if token else team.token
+ self.cookies = cookies or {}
self.callback = callback
self.domain = "api.slack.com"
self.reset()
@@ -1448,23 +1449,22 @@ class SlackRequest(object):
self.tries = 0
self.start_time = time.time()
self.request_normalized = re.sub(r"\W+", "", self.request)
- self.post_data["token"] = self.token
self.url = "https://{}/api/{}?{}".format(
self.domain, self.request, urlencode(encode_to_utf8(self.post_data))
)
- self.params = {"useragent": "wee_slack {}".format(SCRIPT_VERSION)}
self.response_id = sha1_hex("{}{}".format(self.url, self.start_time))
def __repr__(self):
return (
"SlackRequest(team={}, request='{}', post_data={}, retries={}, token='{}', "
- "tries={}, start_time={})"
+ "cookies={}, tries={}, start_time={})"
).format(
self.team,
self.request,
self.post_data,
self.retries,
token_for_print(self.token),
+ self.cookies,
self.tries,
self.start_time,
)
@@ -1472,6 +1472,23 @@ class SlackRequest(object):
def request_string(self):
return "{}".format(self.url)
+ def options(self):
+ cookies = "; ".join(
+ ["{}={}".format(key, value) for key, value in self.cookies.items()]
+ )
+ return {
+ "useragent": "wee_slack {}".format(SCRIPT_VERSION),
+ "httpheader": "Authorization: Bearer {}".format(self.token),
+ "cookie": cookies,
+ }
+
+ def options_as_cli_args(self):
+ options = self.options()
+ options["user-agent"] = options.pop("useragent")
+ httpheader = options.pop("httpheader")
+ headers = [": ".join(x) for x in options.items()] + httpheader.split("\n")
+ return ["-H{}".format(header) for header in headers]
+
def tried(self):
self.tries += 1
self.response_id = sha1_hex("{}{}".format(self.url, time.time()))
@@ -5866,10 +5883,12 @@ def command_upload(data, current_buffer, args):
if isinstance(channel, SlackThreadChannel):
post_data["thread_ts"] = channel.thread_ts
- url = SlackRequest(
- channel.team, "files.upload", post_data, channel=channel
- ).request_string()
- options = ["-s", "-Ffile=@{}".format(file_path), url]
+ request = SlackRequest(channel.team, "files.upload", post_data, channel=channel)
+ options = request.options_as_cli_args() + [
+ "-s",
+ "-Ffile=@{}".format(file_path),
+ request.request_string(),
+ ]
proxy_string = ProxyWrapper().curl()
if proxy_string: