-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakerspaceapi.py
More file actions
306 lines (280 loc) · 10.3 KB
/
Copy pathmakerspaceapi.py
File metadata and controls
306 lines (280 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import requests
from datetime import datetime, timezone
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
import logging
import settings
logger = logging.getLogger(__name__)
def _display_tz():
tz_name = getattr(settings, 'DISPLAY_TIMEZONE', 'Europe/Berlin')
try:
return ZoneInfo(tz_name)
except ZoneInfoNotFoundError:
logger.warning('Unknown DISPLAY_TIMEZONE %r, falling back to Europe/Berlin', tz_name)
return ZoneInfo('Europe/Berlin')
class Transaction:
def __init__(self, desc, value, date):
self.desc = desc
self.value = value
tz = _display_tz()
# date may be a UNIX timestamp (int/float) or ISO string
if isinstance(date, (int, float)):
self.date = datetime.fromtimestamp(date, tz=timezone.utc).astimezone(tz)
else:
try:
dt = datetime.fromisoformat(str(date))
if dt.tzinfo is None:
# treat naive datetimes from API as UTC
dt = dt.replace(tzinfo=timezone.utc)
self.date = dt.astimezone(tz)
except Exception:
self.date = datetime.fromtimestamp(0, tz=timezone.utc).astimezone(tz)
def getDate(self):
return self.date
def getValue(self):
return self.value
def getDesc(self):
return self.desc
def toString(self):
return "%s %s %6.2f" % (self.date.strftime('%d.%m.%y %H:%M'), self.desc, self.value)
class MakerSpaceAPI:
_api_url = 'http://localhost:8000'
_api_token = ''
@classmethod
def configure(cls, api_url, api_token):
cls._api_url = api_url.rstrip('/')
cls._api_token = api_token
@staticmethod
def _headers():
return {'Authorization': f'Bearer {MakerSpaceAPI._api_token}'}
@staticmethod
def _base():
return f'{MakerSpaceAPI._api_url}/api/v1'
@staticmethod
def ping():
'''
Check the public health endpoint to verify all actions can be processed.
Does not verify that the used token is still valid!
'''
try:
r = requests.get(
f'{MakerSpaceAPI._api_url}/api/health',
timeout=3,
)
return r.ok
except requests.RequestException:
return False
def __init__(self, target, uid = 0):
self._target = target
self._uid = 0
if isinstance(uid, (list, bytes, bytearray)):
for i in uid:
self._uid <<= 8
self._uid += i
else:
self._uid = uid
self._pin = None
def changeTarget(self, target):
self._target = target
def getTarget(self):
return self._target
def isAdmin(self):
'''Return True if this user has a PIN set (i.e. is a treasurer).'''
if not self._uid:
return False
try:
r = requests.get(
f'{MakerSpaceAPI._base()}/users/nfc/{self._uid}',
headers=MakerSpaceAPI._headers(),
timeout=5,
)
if r.ok:
return r.json().get('has_pin', False)
except requests.RequestException:
logger.exception('isAdmin check failed')
return False
def checkPin(self, pin):
'''Verify the PIN against the API. Returns True only if the PIN is correct.'''
self._pin = pin
try:
r = requests.post(
f'{MakerSpaceAPI._base()}/bankomat/verify-pin',
headers=MakerSpaceAPI._headers(),
json={'nfc_id': self._uid, 'pin': pin},
timeout=5,
)
return r.ok
except requests.RequestException:
logger.exception('checkPin failed')
return False
def getAdminName(self):
if not self._uid:
return None
try:
r = requests.get(
f'{MakerSpaceAPI._base()}/users/nfc/{self._uid}',
headers=MakerSpaceAPI._headers(),
timeout=5,
)
if r.ok:
return r.json().get('name') or str(self._uid)
except requests.RequestException:
logger.exception('getAdminName failed')
return None
def withdrawValue(self, value):
'''Payout (withdraw) from a booking target. Requires PIN set via checkPin().'''
try:
r = requests.post(
f'{MakerSpaceAPI._base()}/bankomat/payout',
headers=MakerSpaceAPI._headers(),
json={
'nfc_id': self._uid,
'pin': self._pin or '',
'target_slug': self._target,
'amount': value,
'note': f'Auszahlung zur Überweisung auf Bankkonto',
},
timeout=5,
)
if r.ok:
return True
logger.error('withdrawValue failed: %s %s', r.status_code, r.text)
return False
except requests.RequestException:
logger.exception('withdrawValue failed')
return False
def getTotal(self):
try:
r = requests.get(
f'{MakerSpaceAPI._base()}/bankomat/targets',
headers=MakerSpaceAPI._headers(),
timeout=5,
)
if r.ok:
for t in r.json():
if t.get('slug') == self._target:
value = float(t.get('balance', 0))
return value
except requests.RequestException:
logger.exception('getTotal failed')
return 0
def addValue(self, value : float) -> bool:
'''Record cash into a target (no user balance change — e.g. donations, card sales).'''
if value is None or value <= 0:
return False
try:
r = requests.post(
f'{MakerSpaceAPI._base()}/bankomat/target-topup',
headers=MakerSpaceAPI._headers(),
json={'target_slug': self._target, 'amount': value},
timeout=5,
)
if r.ok:
self.getTotal() # update MQTT sensors
return True
logger.error('addValue (target-topup) failed: %s %s', r.status_code, r.text)
return False
except requests.RequestException:
logger.exception('addValue failed for source %s, value %.2f', self.source, value)
return False
def getCardValue(self) -> bool:
try:
r = requests.get(
f'{MakerSpaceAPI._base()}/users/nfc/{self._uid}',
headers=MakerSpaceAPI._headers(),
timeout=5,
)
if r.ok:
return round(float(r.json().get('balance', 0)), 2)
return None
except requests.RequestException:
logger.exception('getCardValue failed')
return None
def addCardValue(self, value : float) -> float:
'''Top up user account and record in nfckasse booking target.'''
logger.info('Adding %.2f to nfckasse account uid %d', value, self._uid)
if value is None or value <= 0:
return False
try:
r = requests.post(
f'{MakerSpaceAPI._base()}/bankomat/topup',
headers=MakerSpaceAPI._headers(),
json={
'nfc_id': self._uid,
'amount': value,
'target_slug': 'nfckasse',
},
timeout=5,
)
if r.ok:
return round(float(r.json().get('balance', 0)), 2)
logger.error('addValue (topup) failed: %s %s', r.status_code, r.text)
return None
except requests.RequestException:
logger.exception('addValue failed')
return None
def transfer(self, value, destcard):
'''Transfer balance to another card. destcard is raw bytes from NFC reader.'''
dest_uid = 0
for b in destcard:
dest_uid <<= 8
dest_uid += b
if dest_uid == self._uid:
logger.info('Transfer to same account stopped')
return 1
src_val = self.getCardValue()
if src_val is None or value > src_val:
return -1
try:
r = requests.post(
f'{MakerSpaceAPI._base()}/bankomat/transfer',
headers=MakerSpaceAPI._headers(),
json={
'from_nfc_id': self._uid,
'to_nfc_id': dest_uid,
'amount': value,
},
timeout=5,
)
if r.ok:
logger.info('Transfer successful')
return 0
if r.status_code == 404:
logger.info('Destination account not found')
return 2
if r.status_code == 402:
return -1
logger.error('Transfer failed: %s %s', r.status_code, r.text)
return 3
except requests.RequestException:
logger.exception('transfer failed')
return 3
def getTransactions(self, offset=0):
try:
limit = 4
r = requests.get(
f'{MakerSpaceAPI._base()}/bankomat/transactions/{self._uid}',
headers=MakerSpaceAPI._headers(),
params={'limit': limit + offset},
timeout=5,
)
if not r.ok:
return []
all_tx = r.json()
transactions = []
for tx in all_tx[offset:offset + limit]:
tx_type = tx.get('type', '')
amount = float(tx.get('amount', 0))
date = tx.get('created_at', 0)
if tx_type == 'purchase':
desc = tx.get('note') or 'Kauf'
elif tx_type in ('topup', 'booking_target_topup'):
desc = 'Aufladung'
elif tx_type in ('transfer_out', 'transfer_in'):
desc = 'Überweisung'
else:
desc = tx.get('note') or tx_type or 'Unbekannt'
transactions.append(Transaction(desc, amount, date))
return transactions
except requests.RequestException:
logger.exception('getTransactions failed')
return []