@@ -176,16 +176,14 @@ def api_request(
176176 """Make API request.
177177
178178 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.
184183 params (dict|tuple): URL parameters to append to the URL. If a dictionary or
185184 list of tuples ``[(key, value)]`` is provided, form-encoding will
186185 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.
189187
190188 Returns:
191189 requests.Response
@@ -262,9 +260,18 @@ def api_request(
262260 response = self .__try_with_alt_routing (fct , ** request_params )
263261
264262 try :
263+ status_code = response .status_code
264+ except : # noqa
265+ status_code = False
266+
267+ try :
268+ json_error = False
265269 response = response .json ()
266270 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 )
268275 raise ProtonAPIError (
269276 {
270277 "Code" : response .status_code ,
@@ -273,11 +280,18 @@ def api_request(
273280 }
274281 )
275282
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 )
281295
282296 return response
283297
0 commit comments