Skip to content

Commit

Permalink
feat: add verify_ssl option to control how requests verifies SSL cert…
Browse files Browse the repository at this point in the history
…ificates
  • Loading branch information
daniel-jones-dev committed Feb 21, 2023
1 parent a827c0a commit 515a594
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Add [example](examples/json) to translate JSON inputs.
* Added platform and python version information to the user-agent string that is sent with API calls, along with an opt-out.
* Added method for applications that use this library to identify themselves in API requests they make.
* Added `verify_ssl` option to `Translator` to control underlying `requests` session.
* Thanks to [andrefloriani](https://github.com/andrefloriani) for the
suggestion in [#60](https://github.com/DeepLcom/deepl-python/issues/60).


## [1.13.0] - 2023-01-26
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,19 @@ The proxy argument is passed to the underlying `requests` session, see the
[documentation for requests][requests-proxy-docs]; a dictionary of schemes to
proxy URLs is also accepted.

#### Override SSL verification

You can control how `requests` performs SSL verification by specifying the
`verify_ssl` option when constructing a `deepl.Translator`, for example to
disable SSL certificate verification:

```python
translator = deepl.Translator(..., verify_ssl=False)
```

This option is passed to the underlying `requests` session as the `verify`
option, see the [documentation for requests][requests-verify-ssl-docs].

#### Anonymous platform information

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:
Expand Down Expand Up @@ -599,3 +612,5 @@ environment variables defined referring to the mock-server.
[pro-account]: https://www.deepl.com/pro-account/?utm_source=github&utm_medium=github-python-readme

[requests-proxy-docs]: https://docs.python-requests.org/en/latest/user/advanced/#proxies

[requests-verify-ssl-docs]: https://docs.python-requests.org/en/latest/user/advanced/#ssl-cert-verification
3 changes: 3 additions & 0 deletions deepl/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def __init__(
self,
proxy: Union[Dict, str, None] = None,
send_platform_info: bool = True,
verify_ssl: Union[bool, str, None] = None,
):
self._session = requests.Session()
if proxy:
Expand All @@ -75,6 +76,8 @@ def __init__(
"containing URL strings for the http and https keys."
)
self._session.proxies.update(proxy)
if verify_ssl is not None:
self._session.verify = verify_ssl
self._send_platform_info = send_platform_info
self._app_info_name = None
self._app_info_version = None
Expand Down
8 changes: 7 additions & 1 deletion deepl/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,9 @@ class Translator:
library can send basic platform info (python version, OS, http library
version) to the DeepL API. True = send info, False = only send client
library version
:param verify_ssl: (Optional) Controls how requests verifies SSL
certificates. This is passed to the underlying requests session, see
the requests verify documentation for more information.
:param skip_language_check: Deprecated, and now has no effect as the
corresponding internal functionality has been removed. This parameter
will be removed in a future version.
Expand All @@ -480,6 +483,7 @@ def __init__(
server_url: Optional[str] = None,
proxy: Union[Dict, str, None] = None,
send_platform_info: bool = True,
verify_ssl: Union[bool, str, None] = None,
skip_language_check: bool = False,
):
if not auth_key:
Expand All @@ -493,7 +497,9 @@ def __init__(
)

self._server_url = server_url
self._client = http_client.HttpClient(proxy, send_platform_info)
self._client = http_client.HttpClient(
proxy, send_platform_info, verify_ssl
)
self.headers = {"Authorization": f"DeepL-Auth-Key {auth_key}"}

def __del__(self):
Expand Down

0 comments on commit 515a594

Please sign in to comment.