Skip to content
This repository was archived by the owner on Nov 6, 2025. It is now read-only.

Commit 57fc579

Browse files
committed
Better exception handling
1 parent e21c22e commit 57fc579

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

postnl_api/postnl_api.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
from datetime import datetime
55
import requests
66

7-
_LOGGER = logging.getLogger(__name__)
8-
97
BASE_URL = 'https://jouw.postnl.nl'
108

119
AUTHENTICATE_URL = BASE_URL + '/mobile/token'
1210
SHIPMENTS_URL = BASE_URL + '/mobile/api/shipments'
1311
PROFILE_URL = BASE_URL + '/mobile/api/profile'
1412
LETTERS_URL = BASE_URL + '/mobile/api/letters'
13+
VALIDATE_LETTERS_URL = BASE_URL + '/mobile/api/letters/validation'
1514

1615
class PostNL_API(object):
1716
""" Interface class for the PostNL API """
@@ -42,15 +41,14 @@ def __init__(self, user, password):
4241
data = response.json()
4342

4443
except Exception:
45-
_LOGGER.exception('Credentials are wrong')
44+
raise(Exception)
4645

47-
# if response['error']:
48-
# raise Exception(response['error']['error_description'])
46+
if data['error']:
47+
raise Exception(data['error'])
4948

5049
self._access_token = data['access_token']
5150
self._refresh_token = data['refresh_token']
52-
# TODO Add logic to refresh on invalidate
53-
self._token_expires_in = data['expires_in']
51+
self._token_expires_in = data['expires_in'] # TODO Add logic to refresh on invalidate
5452

5553
def refresh_token(self):
5654
""" Refresh access_token """
@@ -134,6 +132,26 @@ def get_profile(self):
134132

135133
return profile
136134

135+
def validate_letters(self):
136+
""" Retrieve letter validation status """
137+
138+
headers = {
139+
'api-version': '4.7',
140+
'user-agent': 'PostNL/1 CFNetwork/889.3 Darwin/17.2.0',
141+
'authorization': 'Bearer ' + self._access_token
142+
}
143+
144+
response = requests.request(
145+
'GET', VALIDATE_LETTERS_URL, headers=headers)
146+
147+
if response.status_code == 401:
148+
self.refresh_token()
149+
validation = self.validate_letters()
150+
else:
151+
validation = response.json()
152+
153+
return validation
154+
137155
def get_letters(self):
138156
""" Retrieve letters """
139157

0 commit comments

Comments
 (0)