Skip to content

Commit 3609cbc

Browse files
committed
Fix #712 by adding timeout arg support in sync WebClient
1 parent af377a5 commit 3609cbc

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

slack/web/base_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,9 @@ def _perform_urllib_http_request(
558558
f"Invalid proxy detected: {self.proxy} must be a str value"
559559
)
560560

561-
resp: HTTPResponse = urlopen(req, context=self.ssl)
561+
resp: HTTPResponse = urlopen(
562+
req, context=self.ssl, timeout=self.timeout
563+
)
562564
charset = resp.headers.get_content_charset()
563565
body: str = resp.read().decode(charset) # read the response body here
564566
return {"status": resp.code, "headers": resp.headers, "body": body}

tests/web/mock_web_api_server.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import re
44
import threading
5+
import time
56
from http import HTTPStatus
67
from http.server import HTTPServer, SimpleHTTPRequestHandler
78
from typing import Type
@@ -90,6 +91,13 @@ def _handle(self):
9091
self.wfile.close()
9192
return
9293

94+
if pattern == "timeout":
95+
time.sleep(2)
96+
self.send_response(200)
97+
self.wfile.write("""{"ok":true}""".encode("utf-8"))
98+
self.wfile.close()
99+
return
100+
93101
if request_body and "cursor" in request_body:
94102
page = request_body["cursor"]
95103
pattern = f"{pattern}_{page}"

tests/web/test_web_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import re
3+
import socket
34
import unittest
45

56
import slack.errors as err
@@ -211,3 +212,14 @@ async def test_token_param_async(self):
211212
self.assertIsNone(resp["error"])
212213
with self.assertRaises(err.SlackApiError):
213214
await client.users_list()
215+
216+
def test_timeout_issue_712(self):
217+
client = WebClient(base_url="http://localhost:8888", timeout=1)
218+
with self.assertRaises(socket.timeout):
219+
client.users_list(token="xoxb-timeout")
220+
221+
@async_test
222+
async def test_timeout_issue_712_async(self):
223+
client = WebClient(base_url="http://localhost:8888", timeout=1, run_async=True)
224+
with self.assertRaises(asyncio.exceptions.TimeoutError):
225+
await client.users_list(token="xoxb-timeout")

0 commit comments

Comments
 (0)