Skip to content

Commit 2b937e4

Browse files
authored
Merge pull request #713 from austinbutler/sync-ssl-context
Use SSL context with sychronous requests
2 parents a866aad + 80ba585 commit 2b937e4

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

README.md

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ Details on the Tokens and Authentication can be found in our [Auth Guide](https:
2727
* [Installation](#installation)
2828
* [Getting started tutorial](#getting-started-tutorial)
2929
* [Basic Usage of the Web Client](#basic-usage-of-the-web-client)
30-
* [Sending a message to Slack](#sending-a-message-to-slack)
31-
* [Uploading files to Slack](#uploading-files-to-slack)
30+
* [Sending a message to Slack](#sending-a-message-to-slack)
31+
* [Uploading files to Slack](#uploading-files-to-slack)
3232
* [Basic Usage of the RTM Client](#basic-usage-of-the-rtm-client)
33-
* [Async Usage](#async-usage)
34-
* [Slackclient as a script](#slackclient-as-a-script)
35-
* [Slackclient in a framework](#slackclient-in-a-framework)
33+
* [Async usage](#async-usage)
34+
* [Slackclient as a script](#slackclient-as-a-script)
35+
* [Slackclient in a framework](#slackclient-in-a-framework)
3636
* [Advanced Options](#advanced-options)
37-
* [Migrating from v1.x](#migrating-from-v1)
37+
* [SSL](#ssl)
38+
* [Proxy](#proxy)
39+
* [DNS performance](#dns-performance)
40+
* [Example](#example)
41+
* [Migrating from v1](#migrating-from-v1)
3842
* [Support](#support)
3943

4044
### Requirements
@@ -174,9 +178,9 @@ rtm_client.start()
174178

175179
### Async usage
176180

177-
slackclient v2 and higher uses aiohttp and asyncio to enable async functionality.
181+
slackclient v2 and higher uses [AIOHttp][aiohttp] under the hood for asynchronous requests and [urllib][urllib] for synchronous requests.
178182

179-
Normal usage of the library does not run it in async, hence a kwarg of run_async=True is needed.
183+
Normal usage of the library does not run it in async, hence a kwarg of `run_async=True` is needed.
180184

181185
When in async mode its important to remember to await or run/run_until_complete the call.
182186

@@ -260,14 +264,28 @@ if __name__ == '__main__':
260264

261265
### Advanced Options
262266

263-
The Python slackclient v2 now uses [AIOHttp][aiohttp] under the hood.
267+
#### SSL
264268

265-
Looking for a performance boost? Installing the optional dependencies (aiodns) may help speed up DNS resolving by the client. We've included it as an extra called "optional":
269+
You can provide a custom SSL context or disable verification by passing the `ssl` option, supported by both the RTM and the Web client.
270+
271+
For async requests, see the [AIOHttp SSL documentation](https://docs.aiohttp.org/en/stable/client_advanced.html#ssl-control-for-tcp-sockets).
272+
273+
For sync requests, see the [urllib SSL documentation](https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen).
274+
275+
#### Proxy
276+
277+
A proxy is supported when making async requests, pass the `proxy` option, supported by both the RTM and the Web client.
278+
279+
For more information, see [AIOHttp Proxy documentation](https://docs.aiohttp.org/en/stable/client_advanced.html#proxy-support).
280+
281+
#### DNS performance
282+
283+
Using the async client and looking for a performance boost? Installing the optional dependencies (aiodns) may help speed up DNS resolving by the client. We've included it as an extra called "optional":
266284
```bash
267285
$ pip3 install slackclient[optional]
268286
```
269287

270-
Interested in SSL or Proxy support? Simply use their built-in [SSL](https://docs.aiohttp.org/en/stable/client_advanced.html#ssl-control-for-tcp-sockets) and [Proxy](https://docs.aiohttp.org/en/stable/client_advanced.html#proxy-support) arguments. You can pass these options directly into both the RTM and the Web client.
288+
#### Example
271289

272290
```python
273291
import os
@@ -290,7 +308,6 @@ response = client.chat_postMessage(
290308
print(response)
291309
```
292310

293-
We will always follow the standard process in AIOHttp for those proxy and SSL settings so for more information, check out their documentation page linked [here][aiohttp].
294311

295312
### Migrating from v1
296313

@@ -338,3 +355,4 @@ Visit the [Slack Community][slack-community] for getting help using Slack Develo
338355
[auth-guide]: documentation_v2/auth.md
339356
[basic-usage]: documentation_v2/basic_usage.md
340357
[aiohttp]: https://aiohttp.readthedocs.io/
358+
[urllib]: https://docs.python.org/3/library/urllib.request.html

slack/web/base_client.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -547,20 +547,13 @@ def _perform_urllib_http_request(
547547
# which might be a security risk if the URL to open can be manipulated by an external user.
548548
if url.lower().startswith("http"):
549549
req = Request(method="POST", url=url, data=body, headers=headers)
550-
resp: HTTPResponse = urlopen(req)
550+
resp: HTTPResponse = urlopen(req, context=self.ssl)
551551
charset = resp.headers.get_content_charset()
552552
body: str = resp.read().decode(charset) # read the response body here
553-
return {
554-
"status": resp.code,
555-
"headers": resp.headers,
556-
"body": body,
557-
}
553+
return {"status": resp.code, "headers": resp.headers, "body": body}
558554
raise SlackRequestError(f"Invalid URL detected: {url}")
559555
except HTTPError as e:
560-
resp = {
561-
"status": e.code,
562-
"headers": e.headers,
563-
}
556+
resp = {"status": e.code, "headers": e.headers}
564557
if e.code == 429:
565558
# for compatibility with aiohttp
566559
resp["headers"]["Retry-After"] = resp["headers"]["retry-after"]
@@ -575,7 +568,7 @@ def _perform_urllib_http_request(
575568
raise err
576569

577570
def _build_urllib_request_headers(
578-
self, token: str, has_json: bool, has_files: bool, additional_headers: dict,
571+
self, token: str, has_json: bool, has_files: bool, additional_headers: dict
579572
):
580573
headers = {
581574
"User-Agent": get_user_agent(),

0 commit comments

Comments
 (0)