Skip to content

Commit 515a594

Browse files
feat: add verify_ssl option to control how requests verifies SSL certificates
1 parent a827c0a commit 515a594

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
* Add [example](examples/json) to translate JSON inputs.
1111
* Added platform and python version information to the user-agent string that is sent with API calls, along with an opt-out.
1212
* Added method for applications that use this library to identify themselves in API requests they make.
13+
* Added `verify_ssl` option to `Translator` to control underlying `requests` session.
14+
* Thanks to [andrefloriani](https://github.com/andrefloriani) for the
15+
suggestion in [#60](https://github.com/DeepLcom/deepl-python/issues/60).
1316

1417

1518
## [1.13.0] - 2023-01-26

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,19 @@ The proxy argument is passed to the underlying `requests` session, see the
495495
[documentation for requests][requests-proxy-docs]; a dictionary of schemes to
496496
proxy URLs is also accepted.
497497

498+
#### Override SSL verification
499+
500+
You can control how `requests` performs SSL verification by specifying the
501+
`verify_ssl` option when constructing a `deepl.Translator`, for example to
502+
disable SSL certificate verification:
503+
504+
```python
505+
translator = deepl.Translator(..., verify_ssl=False)
506+
```
507+
508+
This option is passed to the underlying `requests` session as the `verify`
509+
option, see the [documentation for requests][requests-verify-ssl-docs].
510+
498511
#### Anonymous platform information
499512

500513
By default, we send some basic information about the platform the client library is running on with each request, see [here for an explanation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent). This data is completely anonymous and only used to improve our product, not track any individual users. If you do not wish to send this data, you can opt-out when creating your `deepl.Translator` object by setting the `send_platform_info` flag like so:
@@ -599,3 +612,5 @@ environment variables defined referring to the mock-server.
599612
[pro-account]: https://www.deepl.com/pro-account/?utm_source=github&utm_medium=github-python-readme
600613

601614
[requests-proxy-docs]: https://docs.python-requests.org/en/latest/user/advanced/#proxies
615+
616+
[requests-verify-ssl-docs]: https://docs.python-requests.org/en/latest/user/advanced/#ssl-cert-verification

deepl/http_client.py

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def __init__(
6464
self,
6565
proxy: Union[Dict, str, None] = None,
6666
send_platform_info: bool = True,
67+
verify_ssl: Union[bool, str, None] = None,
6768
):
6869
self._session = requests.Session()
6970
if proxy:
@@ -75,6 +76,8 @@ def __init__(
7576
"containing URL strings for the http and https keys."
7677
)
7778
self._session.proxies.update(proxy)
79+
if verify_ssl is not None:
80+
self._session.verify = verify_ssl
7881
self._send_platform_info = send_platform_info
7982
self._app_info_name = None
8083
self._app_info_version = None

deepl/translator.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,9 @@ class Translator:
458458
library can send basic platform info (python version, OS, http library
459459
version) to the DeepL API. True = send info, False = only send client
460460
library version
461+
:param verify_ssl: (Optional) Controls how requests verifies SSL
462+
certificates. This is passed to the underlying requests session, see
463+
the requests verify documentation for more information.
461464
:param skip_language_check: Deprecated, and now has no effect as the
462465
corresponding internal functionality has been removed. This parameter
463466
will be removed in a future version.
@@ -480,6 +483,7 @@ def __init__(
480483
server_url: Optional[str] = None,
481484
proxy: Union[Dict, str, None] = None,
482485
send_platform_info: bool = True,
486+
verify_ssl: Union[bool, str, None] = None,
483487
skip_language_check: bool = False,
484488
):
485489
if not auth_key:
@@ -493,7 +497,9 @@ def __init__(
493497
)
494498

495499
self._server_url = server_url
496-
self._client = http_client.HttpClient(proxy, send_platform_info)
500+
self._client = http_client.HttpClient(
501+
proxy, send_platform_info, verify_ssl
502+
)
497503
self.headers = {"Authorization": f"DeepL-Auth-Key {auth_key}"}
498504

499505
def __del__(self):

0 commit comments

Comments
 (0)