Skip to content

Commit

Permalink
Allow to make and retrieve non-json responses from API
Browse files Browse the repository at this point in the history
  • Loading branch information
calexandru2018 committed Sep 28, 2021
1 parent cb70c59 commit 4010a58
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
proton-python-client (0.7.0-1) unstable; urgency=medium

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

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

Expand Down
40 changes: 27 additions & 13 deletions proton/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,14 @@ def api_request(
"""Make API request.
Args:
endpoint (string): API endpoint
jsondata (json): json for the body to attach to the request
(if files or data is not specified)
additional_headers (dict): additional (dictionary of) headers to send
method (string): get|post|put|delete|patch
endpoint (string): API endpoint.
jsondata (json): json to send in the body.
additional_headers (dict): additional (dictionary of) headers to send.
method (string): get|post|put|delete|patch.
params (dict|tuple): URL parameters to append to the URL. If a dictionary or
list of tuples ``[(key, value)]`` is provided, form-encoding will
take place.
_skip_alt_routing_for_api_check (bool): used to temporarly skip alt routing
since the goal is to ensure if the API is reacheable or not.
_skip_alt_routing_for_api_check (bool): used to temporarly skip alt routing.
Returns:
requests.Response
Expand Down Expand Up @@ -262,9 +260,18 @@ def api_request(
response = self.__try_with_alt_routing(fct, **request_params)

try:
status_code = response.status_code
except: # noqa
status_code = False

try:
json_error = False
response = response.json()
except json.decoder.JSONDecodeError as e:
self._logger.exception(e)
json_error = e

if json_error and status_code != 200:
self._logger.exception(json_error)
raise ProtonAPIError(
{
"Code": response.status_code,
Expand All @@ -273,11 +280,18 @@ def api_request(
}
)

if response["Code"] not in [1000, 1001]:
if response["Code"] == 9001:
self.__captcha_token = response["Details"]["HumanVerificationToken"]

raise ProtonAPIError(response)
# This check is needed for routers or any other clients that will ask for other
# data that is not provided in json format, such as when asking /vpn/config for
# a .ovpn template
try:
if response["Code"] not in [1000, 1001]:
if response["Code"] == 9001:
self.__captcha_token = response["Details"]["HumanVerificationToken"]

raise ProtonAPIError(response)
except TypeError as e:
if status_code != 200:
raise TypeError(e)

return response

Expand Down
1 change: 1 addition & 0 deletions rpmbuild/SPECS/python3-proton-client.spec
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ rm -rf $RPM_BUILD_ROOT
%changelog
* Fri Sep 24 2021 Proton Technologies AG <[email protected]> 0.7.0-1
- Feature: Request human verification
- Fix: Allow to make and retrieve non-json responses from API

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

0 comments on commit 4010a58

Please sign in to comment.