|
4 | 4 | from datetime import datetime |
5 | 5 | import requests |
6 | 6 |
|
7 | | -_LOGGER = logging.getLogger(__name__) |
8 | | - |
9 | 7 | BASE_URL = 'https://jouw.postnl.nl' |
10 | 8 |
|
11 | 9 | AUTHENTICATE_URL = BASE_URL + '/mobile/token' |
12 | 10 | SHIPMENTS_URL = BASE_URL + '/mobile/api/shipments' |
13 | 11 | PROFILE_URL = BASE_URL + '/mobile/api/profile' |
14 | 12 | LETTERS_URL = BASE_URL + '/mobile/api/letters' |
| 13 | +VALIDATE_LETTERS_URL = BASE_URL + '/mobile/api/letters/validation' |
15 | 14 |
|
16 | 15 | class PostNL_API(object): |
17 | 16 | """ Interface class for the PostNL API """ |
@@ -42,15 +41,14 @@ def __init__(self, user, password): |
42 | 41 | data = response.json() |
43 | 42 |
|
44 | 43 | except Exception: |
45 | | - _LOGGER.exception('Credentials are wrong') |
| 44 | + raise(Exception) |
46 | 45 |
|
47 | | - # if response['error']: |
48 | | - # raise Exception(response['error']['error_description']) |
| 46 | + if data['error']: |
| 47 | + raise Exception(data['error']) |
49 | 48 |
|
50 | 49 | self._access_token = data['access_token'] |
51 | 50 | 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 |
54 | 52 |
|
55 | 53 | def refresh_token(self): |
56 | 54 | """ Refresh access_token """ |
@@ -134,6 +132,26 @@ def get_profile(self): |
134 | 132 |
|
135 | 133 | return profile |
136 | 134 |
|
| 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 | + |
137 | 155 | def get_letters(self): |
138 | 156 | """ Retrieve letters """ |
139 | 157 |
|
|
0 commit comments