-
Notifications
You must be signed in to change notification settings - Fork 21
/
lambda_function.py
66 lines (58 loc) · 1.78 KB
/
lambda_function.py
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
import json
import traceback
from lib.sync import Sync
from lib.config import config
from lib.state import state
from lib.log import log
from lib import bunq_api
def add_callbacks(sync):
url = state.get("aws_callback")
log.info("SSM callback = \"{}\"".format(url))
if not url:
return
log.info("Adding callbacks...")
for uid in sync.get_bunq_user_ids():
callback_marker = config.get("callback_marker") or "bunq2ynab-autosync"
bunq_api.add_callback(uid, callback_marker, url)
def get_iban_from_event(event):
body_str = event.get("body")
if not body_str:
log.info("No request body found")
return
try:
body = json.loads(body_str)
except json.JSONDecodeError as e:
log.error("Error decoding quest body as JSON: {}".format(e))
return
nu = body.get("NotificationUrl", {})
category = nu.get("category")
if category != "MUTATION":
log.error("Category is not MUTATION but {}".format(e))
return
iban = nu.get("object", {}).get("Payment", {}).get("alias", {}).get("iban")
if not iban:
log.error("No IBAN found in request body")
return
log.info("Found IBAN {} in request body".format(iban))
return iban
def lambda_handler(event, context):
try:
config.load()
iban = get_iban_from_event(event)
sync = Sync()
sync.populate()
if iban:
result = sync.synchronize_iban(iban)
else:
result = sync.synchronize()
add_callbacks(sync)
return {
"statusCode": 200,
"body": result
}
except Exception as e:
log.exception("Exception occurred")
return {
"statusCode": 500,
"body": traceback.format_exc()
}