@@ -176,16 +176,14 @@ def api_request(
176
176
"""Make API request.
177
177
178
178
Args:
179
- endpoint (string): API endpoint
180
- jsondata (json): json for the body to attach to the request
181
- (if files or data is not specified)
182
- additional_headers (dict): additional (dictionary of) headers to send
183
- method (string): get|post|put|delete|patch
179
+ endpoint (string): API endpoint.
180
+ jsondata (json): json to send in the body.
181
+ additional_headers (dict): additional (dictionary of) headers to send.
182
+ method (string): get|post|put|delete|patch.
184
183
params (dict|tuple): URL parameters to append to the URL. If a dictionary or
185
184
list of tuples ``[(key, value)]`` is provided, form-encoding will
186
185
take place.
187
- _skip_alt_routing_for_api_check (bool): used to temporarly skip alt routing
188
- since the goal is to ensure if the API is reacheable or not.
186
+ _skip_alt_routing_for_api_check (bool): used to temporarly skip alt routing.
189
187
190
188
Returns:
191
189
requests.Response
@@ -262,9 +260,18 @@ def api_request(
262
260
response = self .__try_with_alt_routing (fct , ** request_params )
263
261
264
262
try :
263
+ status_code = response .status_code
264
+ except : # noqa
265
+ status_code = False
266
+
267
+ try :
268
+ json_error = False
265
269
response = response .json ()
266
270
except json .decoder .JSONDecodeError as e :
267
- self ._logger .exception (e )
271
+ json_error = e
272
+
273
+ if json_error and status_code != 200 :
274
+ self ._logger .exception (json_error )
268
275
raise ProtonAPIError (
269
276
{
270
277
"Code" : response .status_code ,
@@ -273,11 +280,18 @@ def api_request(
273
280
}
274
281
)
275
282
276
- if response ["Code" ] not in [1000 , 1001 ]:
277
- if response ["Code" ] == 9001 :
278
- self .__captcha_token = response ["Details" ]["HumanVerificationToken" ]
279
-
280
- raise ProtonAPIError (response )
283
+ # This check is needed for routers or any other clients that will ask for other
284
+ # data that is not provided in json format, such as when asking /vpn/config for
285
+ # a .ovpn template
286
+ try :
287
+ if response ["Code" ] not in [1000 , 1001 ]:
288
+ if response ["Code" ] == 9001 :
289
+ self .__captcha_token = response ["Details" ]["HumanVerificationToken" ]
290
+
291
+ raise ProtonAPIError (response )
292
+ except TypeError as e :
293
+ if status_code != 200 :
294
+ raise TypeError (e )
281
295
282
296
return response
283
297
0 commit comments