Skip to content

Commit 20933f2

Browse files
committed
Fix comments and remove unnecessary logic
1 parent c7d0be8 commit 20933f2

File tree

1 file changed

+25
-33
lines changed

1 file changed

+25
-33
lines changed

slack/webhook/client.py

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class WebhookClient:
1717
def __init__(
1818
self, url: str, default_headers: Dict[str, str] = {},
1919
):
20-
"""urllib-based API client.
20+
"""API client for Incoming Webhooks and response_url
21+
:param url: a complete URL to send data (e.g., https://hooks.slack.com/XXX)
2122
:param default_headers: request headers to add to all requests
2223
"""
2324
self.url = url
@@ -27,52 +28,45 @@ def send(
2728
self, body: Dict[str, any], additional_headers: Dict[str, str] = {},
2829
) -> WebhookResponse:
2930
"""Performs a Slack API request and returns the result.
30-
:param url: a complete URL (e.g., https://hooks.slack.com/XXX)
31-
:param json_body: json data structure (it's still a dict at this point),
31+
:param body: json data structure (it's still a dict at this point),
3232
if you give this argument, body_params and files will be skipped
33-
:param body_params: form params
34-
:param additional_headers: request headers to append
33+
:param additional_headers: request headers to append only for this request
3534
:return: API response
3635
"""
37-
3836
body = convert_bool_to_0_or_1(body)
3937
self._parse_blocks(body)
4038
if self.logger.level <= logging.DEBUG:
4139
self.logger.debug(
42-
f"Slack API Request - url: {self.url}, "
40+
f"Sending a request - url: {self.url}, "
4341
f"body: {body}, "
4442
f"additional_headers: {additional_headers}"
4543
)
4644

47-
request_headers = self._build_request_headers(
48-
has_json=json is not None, additional_headers=additional_headers,
45+
return self._perform_http_request(
46+
url=self.url,
47+
body=body,
48+
headers=self._build_request_headers(additional_headers),
4949
)
50-
args = {
51-
"headers": request_headers,
52-
"body": body,
53-
}
54-
return self._perform_http_request(url=self.url, args=args)
5550

5651
def _perform_http_request(
57-
self, *, url: str, args: Dict[str, Dict[str, any]]
52+
self, *, url: str, body: Dict[str, any], headers: Dict[str, str]
5853
) -> WebhookResponse:
5954
"""Performs an HTTP request and parses the response.
60-
:param url: a complete URL (e.g., https://www.slack.com/api/chat.postMessage)
61-
:param args: args has "headers", "data", "params", and "json"
62-
"headers": Dict[str, str]
63-
"params": Dict[str, str],
64-
"json": Dict[str, any],
65-
:return: a tuple (HTTP response and its body)
55+
:param url: a complete URL to send data (e.g., https://hooks.slack.com/XXX)
56+
:param body: request body data
57+
:param headers: complete set of request headers
58+
:return: API response
6659
"""
67-
headers = args["headers"]
68-
body = json.dumps(args["body"]).encode("utf-8")
60+
body = json.dumps(body).encode("utf-8")
6961
headers["Content-Type"] = "application/json;charset=utf-8"
7062

7163
try:
64+
# for security
7265
if url.lower().startswith("http"):
7366
req = Request(method="POST", url=url, data=body, headers=headers)
7467
else:
7568
raise SlackRequestError(f"Invalid URL detected: {url}")
69+
7670
resp: HTTPResponse = urlopen(req)
7771
charset = resp.headers.get_content_charset() or "utf-8"
7872
return WebhookResponse(
@@ -98,21 +92,19 @@ def _perform_http_request(
9892
raise err
9993

10094
def _build_request_headers(
101-
self, has_json: bool, additional_headers: dict,
102-
):
103-
headers = {
95+
self, additional_headers: Dict[str, str],
96+
) -> Dict[str, str]:
97+
request_headers = {
10498
"User-Agent": get_user_agent(),
105-
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
99+
"Content-Type": "application/json;charset=utf-8",
106100
}
107-
headers.update(self.default_headers)
101+
request_headers.update(self.default_headers)
108102
if additional_headers:
109-
headers.update(additional_headers)
110-
if has_json:
111-
headers.update({"Content-Type": "application/json;charset=utf-8"})
112-
return headers
103+
request_headers.update(additional_headers)
104+
return request_headers
113105

114106
@staticmethod
115-
def _parse_blocks(body):
107+
def _parse_blocks(body) -> None:
116108
blocks = body.get("blocks", None)
117109

118110
def to_dict(b: Union[Dict, Block]):

0 commit comments

Comments
 (0)