This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
74 lines (58 loc) · 1.97 KB
/
handler.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
67
68
69
70
71
72
73
74
import json
import sys
from devtools import debug
def handle(event, context) -> dict:
"""
Escalates or de-escalates depending on the incoming event type.
For more details on the event object format, refer to our reporting docs:
https://docs.symops.com/docs/reporting
"""
print("Got event:")
debug(event)
try:
username = resolve_user(event)
message = update_user(username, event)
return {"body": {"message": message}, "errors": []}
except Exception as e:
return {"body": {}, "errors": [str(e)]}
def resolve_user(event) -> str:
"""
Placeholder to take the requesting user and resolve to the right
user id for the system you're escalating the user in.
"""
return event["run"]["actors"]["request"]["username"]
def update_user(username, event) -> str:
"""
Placeholder to handle updating the given user based on the event type
"""
event_type = event["event"]["type"]
if event_type == "escalate":
message = f"Escalating user: {username}"
elif event_type == "deescalate":
message = f"Deescalating user: {username}"
else:
raise RuntimeError(f"Unsupported event type: {event_type}")
return message
def resolve_local_json(arg) -> str:
"""Find the right test json file based on the arg"""
if arg == "-d":
file = "deescalate.json"
elif arg == "-e":
file = "escalate.json"
else:
raise RuntimeError(f"Specify either -e or -d, you supplied: {arg}")
return f"../test/{file}"
def run_local() -> dict:
"""
This lets you test your function code locally, with an escalate or
deescalate payload (in the ../test) directory.
$ python handler.py [-e | -d]
"""
arg = None if len(sys.argv) < 2 else sys.argv[1]
path = resolve_local_json(arg)
with open(path, "r") as payload:
event = json.load(payload)
return handle(event, {})
if __name__ == "__main__":
result = run_local()
debug(result)