Skip to content

Commit a50ca99

Browse files
author
Alexandru Cheltuitor
committed
Merge branch 'fix/add-exceptions' into 'develop'
Fix/add exceptions See merge request ProtonVPN/linux/proton-python-client!32
2 parents bfe7454 + ec6b7c4 commit a50ca99

File tree

9 files changed

+65
-34
lines changed

9 files changed

+65
-34
lines changed

arch/PKGBUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Maintainer: Proton Technologies AG <[email protected]>
22
pkgname=python-proton-client
3-
pkgver=0.4.1
3+
pkgver=0.5.0
44
pkgrel=1
55
pkgdesc="Safely login with ProtonVPN credentials to connect to Proton."
66
arch=("any")

debian/changelog

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
proton-python-client (0.4.1-1) unstable; urgency=medium
1+
proton-python-client (0.5.0-1) unstable; urgency=medium
2+
3+
* Add new exceptions
4+
* Throw custom exceptions in case of network errors, abstracting from the package that is being used for requests
5+
6+
-- Proton Technologies AG <[email protected]> Fri, 30 Apr 2021 18:15:00 +0100
7+
8+
proton-python-client (0.4.1-1) unstable; urgency=medium
29

310
* Add long description to setup.py
411

proton/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
from .api import Session, ProtonError # noqa
1+
from .api import Session # noqa
2+
from .exceptions import ProtonError # noqa

proton/api.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
import base64
22
import json
3-
43
import gnupg
54
import requests
65

76
from .cert_pinning import TLSPinningAdapter
87
from .srp import User as PmsrpUser
98
from .constants import DEFAULT_TIMEOUT, SRP_MODULUS_KEY, SRP_MODULUS_KEY_FINGERPRINT
10-
11-
12-
class ProtonError(Exception):
13-
def __init__(self, ret):
14-
self.code = ret['Code']
15-
self.error = ret['Error']
16-
try:
17-
self.headers = ret["Headers"]
18-
except KeyError:
19-
self.headers = ""
20-
21-
super().__init__("[{}] {} {}".format(
22-
self.code, self.error, self.headers
23-
))
9+
from .exceptions import ProtonError, TLSPinningError, NewConnectionError, UnknownConnectionError
2410

2511

2612
class Session:
@@ -106,12 +92,19 @@ def api_request(
10692
if fct is None:
10793
raise ValueError("Unknown method: {}".format(method))
10894

109-
ret = fct(
110-
self.__api_url + endpoint,
111-
headers=additional_headers,
112-
json=jsondata,
113-
timeout=self.__timeout
114-
)
95+
try:
96+
ret = fct(
97+
self.__api_url + endpoint,
98+
headers=additional_headers,
99+
json=jsondata,
100+
timeout=self.__timeout
101+
)
102+
except requests.exceptions.ConnectionError as e:
103+
raise NewConnectionError(e)
104+
except TLSPinningError as e:
105+
raise TLSPinningError(e)
106+
except (Exception, requests.exceptions.BaseHTTPError) as e:
107+
raise UnknownConnectionError(e)
115108

116109
try:
117110
ret = ret.json()

proton/cert_pinning.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,15 @@
22
import hashlib
33
from ssl import DER_cert_to_PEM_cert
44

5-
import requests
65
from OpenSSL import crypto
76
from requests.adapters import HTTPAdapter
87
from urllib3.connectionpool import HTTPSConnectionPool
98
from urllib3.poolmanager import PoolManager
109
from urllib3.util.timeout import Timeout
11-
10+
from .exceptions import TLSPinningError
1211
from .constants import PUBKEY_HASH_DICT
1312

1413

15-
class TLSPinningError(requests.exceptions.SSLError):
16-
def __init__(self, strerror):
17-
self.strerror = strerror
18-
super(TLSPinningError, self).__init__(strerror)
19-
20-
2114
class TLSPinningHTTPSConnectionPool(HTTPSConnectionPool):
2215
"""Verify the certificate upon each connection"""
2316
def __init__(

proton/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
VERSION = "0.4.1"
2+
VERSION = "0.5.0"
33
DEFAULT_TIMEOUT = (10, 30)
44
PUBKEY_HASH_DICT = {
55
"api.protonvpn.ch": [

proton/exceptions.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class ProtonError(Exception):
2+
def __init__(self, ret):
3+
self.code = ret['Code']
4+
self.error = ret['Error']
5+
try:
6+
self.headers = ret["Headers"]
7+
except KeyError:
8+
self.headers = ""
9+
10+
super().__init__("{}".format(self.error))
11+
12+
13+
class ProtonNetworkError(Exception):
14+
def __init__(self, message, additional_context=None):
15+
self.message = message
16+
self.additional_context = additional_context
17+
super().__init__(self.message)
18+
19+
20+
class TLSPinningError(ProtonNetworkError):
21+
"""TLS Pinning exception"""
22+
23+
24+
class NewConnectionError(ProtonNetworkError):
25+
"""Network Error"""
26+
27+
28+
class UnknownConnectionError(ProtonNetworkError):
29+
"""UnknownConnectionError"""

rpmbuild/SPECS/python3-proton-client.spec

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
%define unmangled_name proton-client
2-
%define version 0.4.1
2+
%define version 0.5.0
33
%define release 1
44

55
Prefix: %{_prefix}
@@ -48,6 +48,10 @@ rm -rf $RPM_BUILD_ROOT
4848
%defattr(-,root,root)
4949

5050
%changelog
51+
* Wed Apr 21 2021 Proton Technologies AG <[email protected]> 0.5.0-1
52+
- Add new exceptions
53+
- Throw custom exceptions in case of network errors, abstracting from the package that is being used for requests
54+
5155
* Wed Apr 21 2021 Proton Technologies AG <[email protected]> 0.4.1-1
5256
- Add long description to setup.py
5357

setup.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
[flake8]
2+
ignore = C901, W503, E402
3+
max-line-length = 100
4+
15
[metadata]
26
long_description = file: README.md
37
long_description_content_type = text/markdown

0 commit comments

Comments
 (0)