Skip to content

Commit 4010a58

Browse files
Allow to make and retrieve non-json responses from API
1 parent cb70c59 commit 4010a58

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

debian/changelog

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
proton-python-client (0.7.0-1) unstable; urgency=medium
22

33
* Feature: Request human verification
4+
* Fix: Allow to make and retrieve non-json responses from API
45

56
-- Proton Technologies AG <[email protected]> Fri, 24 Sep 2021 11:30:00 +0100
67

proton/api.py

+27-13
Original file line numberDiff line numberDiff line change
@@ -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

rpmbuild/SPECS/python3-proton-client.spec

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ rm -rf $RPM_BUILD_ROOT
5151
%changelog
5252
* Fri Sep 24 2021 Proton Technologies AG <[email protected]> 0.7.0-1
5353
- Feature: Request human verification
54+
- Fix: Allow to make and retrieve non-json responses from API
5455

5556
* Thu Jul 08 2021 Proton Technologies AG <[email protected]> 0.6.1-4
5657
- Feature: Alternative Routing

0 commit comments

Comments
 (0)