Skip to content

Commit abefd6e

Browse files
Fix HTTP request retries for document uploads
1 parent c369fa7 commit abefd6e

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77

8+
## [Unreleased]
9+
### Added
10+
### Changed
11+
### Deprecated
12+
### Removed
13+
### Fixed
14+
* Fix HTTP request retries for document uploads.
15+
### Security
16+
17+
818
## [1.1.3] - 2021-09-27
919
### Changed
1020
* Loosen requirement for requests to 2.0 or higher.
@@ -71,6 +81,7 @@ Version increased to avoid conflicts with old packages on PyPI.
7181
Initial version.
7282

7383

84+
[Unreleased]: https://github.com/DeepLcom/deepl-python/compare/v1.1.3...HEAD
7485
[1.1.3]: https://github.com/DeepLcom/deepl-python/compare/v1.1.2...v1.1.3
7586
[1.1.2]: https://github.com/DeepLcom/deepl-python/compare/v1.1.1...v1.1.2
7687
[1.1.1]: https://github.com/DeepLcom/deepl-python/compare/v1.1.0...v1.1.1

deepl/http_client.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# license that can be found in the LICENSE file.
44

55
from . import version
6-
from .exceptions import ConnectionException
6+
from .exceptions import ConnectionException, DeepLException
77
import http
88
import random
99
import requests
@@ -67,17 +67,32 @@ def close(self):
6767
self._session.close()
6868

6969
def request_with_backoff(
70-
self, method: str, url: str, data: Optional[dict], **kwargs
70+
self,
71+
method: str,
72+
url: str,
73+
data: Optional[dict],
74+
stream: bool = False,
75+
**kwargs,
7176
) -> Tuple[int, Union[str, requests.Response]]:
7277
"""Makes API request, retrying if necessary, and returns response.
7378
7479
Return and exceptions are the same as function request()."""
7580
backoff = _BackoffTimer()
81+
82+
try:
83+
request = requests.Request(
84+
method, url, data=data, **kwargs
85+
).prepare()
86+
except Exception as e:
87+
raise DeepLException(
88+
f"Error occurred while preparing request: {e}"
89+
) from e
90+
7691
while True:
7792
response: Optional[Tuple[int, Union[str, requests.Response]]]
7893
try:
79-
response = self.request(
80-
method, url, data, timeout=backoff.get_timeout(), **kwargs
94+
response = self._internal_request(
95+
request, stream=stream, timeout=backoff.get_timeout()
8196
)
8297
exception = None
8398
except Exception as e:
@@ -108,7 +123,6 @@ def request(
108123
method: str,
109124
url: str,
110125
data: Optional[dict],
111-
timeout: float,
112126
stream: bool = False,
113127
**kwargs,
114128
) -> Tuple[int, Union[str, requests.Response]]:
@@ -118,22 +132,31 @@ def request(
118132
stream is False) or response (if stream is True).
119133
120134
If no response is received will raise ConnectionException."""
135+
121136
try:
137+
request = requests.Request(
138+
method, url, data=data, **kwargs
139+
).prepare()
140+
except Exception as e:
141+
raise DeepLException(
142+
f"Error occurred while preparing request: {e}"
143+
) from e
144+
return self._internal_request(request, stream, stream=stream)
145+
146+
def _internal_request(
147+
self,
148+
request: requests.PreparedRequest,
149+
stream: bool,
150+
timeout: float = min_connection_timeout,
151+
**kwargs,
152+
) -> Tuple[int, Union[str, requests.Response]]:
153+
try:
154+
response = self._session.send(
155+
request, stream=stream, timeout=timeout, **kwargs
156+
)
122157
if stream:
123-
response = self._session.request(
124-
method,
125-
url,
126-
data=data,
127-
timeout=timeout,
128-
stream=True,
129-
**kwargs,
130-
)
131158
return response.status_code, response
132-
133159
else:
134-
response = self._session.request(
135-
method, url, data=data, timeout=timeout, **kwargs
136-
)
137160
try:
138161
response.encoding = "UTF-8"
139162
return response.status_code, response.text

tests/test_general.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_invalid_authkey(server):
3636

3737
def test_invalid_server_url(server):
3838
translator = deepl.Translator("invalid", server_url="http:/api.deepl.com")
39-
with pytest.raises(deepl.exceptions.ConnectionException):
39+
with pytest.raises(deepl.exceptions.DeepLException):
4040
translator.get_usage()
4141

4242

0 commit comments

Comments
 (0)