3
3
# license that can be found in the LICENSE file.
4
4
5
5
from . import version
6
- from .exceptions import ConnectionException
6
+ from .exceptions import ConnectionException , DeepLException
7
7
import http
8
8
import random
9
9
import requests
@@ -67,17 +67,32 @@ def close(self):
67
67
self ._session .close ()
68
68
69
69
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 ,
71
76
) -> Tuple [int , Union [str , requests .Response ]]:
72
77
"""Makes API request, retrying if necessary, and returns response.
73
78
74
79
Return and exceptions are the same as function request()."""
75
80
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
+
76
91
while True :
77
92
response : Optional [Tuple [int , Union [str , requests .Response ]]]
78
93
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 ()
81
96
)
82
97
exception = None
83
98
except Exception as e :
@@ -108,7 +123,6 @@ def request(
108
123
method : str ,
109
124
url : str ,
110
125
data : Optional [dict ],
111
- timeout : float ,
112
126
stream : bool = False ,
113
127
** kwargs ,
114
128
) -> Tuple [int , Union [str , requests .Response ]]:
@@ -118,22 +132,31 @@ def request(
118
132
stream is False) or response (if stream is True).
119
133
120
134
If no response is received will raise ConnectionException."""
135
+
121
136
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
+ )
122
157
if stream :
123
- response = self ._session .request (
124
- method ,
125
- url ,
126
- data = data ,
127
- timeout = timeout ,
128
- stream = True ,
129
- ** kwargs ,
130
- )
131
158
return response .status_code , response
132
-
133
159
else :
134
- response = self ._session .request (
135
- method , url , data = data , timeout = timeout , ** kwargs
136
- )
137
160
try :
138
161
response .encoding = "UTF-8"
139
162
return response .status_code , response .text
0 commit comments