-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_new_employee_webhook_receiver.py
67 lines (55 loc) · 2.59 KB
/
add_new_employee_webhook_receiver.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
import traceback
import json
import requests
import os
from ssm_util import fetch_ssm_params
from customlist_data_builder import CustomlistItemDataBuilder
from constants import IMAGE_PATH
from builder_client import BuilderClient
def add_employee_to_guide(event, context):
"""
This lambda adds a new employee
to the guide after they are added
in Zenefits
"""
try:
# Fetch the Builder API key, the guide ID of the guide where the content
# is published, and the custom list ID that the items are associated with
api_key, guide_and_list_ids, zenefits_app_key = fetch_ssm_params()
# Initialize a Session
builder_client = BuilderClient(api_key)
for guide_id, employee_customlist_id in guide_and_list_ids:
# Use the lambda event data to build a CustomListItem
employee_data = event["data"]
customlist_data_builder = CustomlistItemDataBuilder(guide_id, zenefits_app_key)
customlist_data = customlist_data_builder.build(employee_data)
# Create a new CustomListItem in Builder
url = f"https://builder.guidebook.com/open-api/v1/custom-list-items/?guide={guide_id}&custom_lists={employee_customlist_id}"
photo_available = False
if employee_data.get('photo_url'):
img_response = requests.get(employee_data['photo_url'])
photo_available = True if img_response.status_code == 200 else False
if photo_available:
with open(IMAGE_PATH, 'wb') as handler:
handler.write(img_response.content)
with open(IMAGE_PATH, 'rb') as handler:
response = builder_client.post(url, customlist_data, {"thumbnail": handler})
os.remove(IMAGE_PATH)
else:
response = builder_client.post(url, customlist_data)
# Create a new CustomListItemRelation in Builder
relations_data = {
"custom_list": employee_customlist_id,
"custom_list_item": response.json()["id"],
}
url = "https://builder.guidebook.com/open-api/v1/custom-list-item-relations/"
builder_client.post(url, data=relations_data)
# Publish the changes
response = builder_client.post(f"https://builder.guidebook.com/open-api/v1/guides/{guide_id}/publish/", raise_error=False)
if response.status_code == 403:
print(response.content)
except Exception as e:
print(e)
traceback.print_exc()
return {"statusCode": 500}
return {"statusCode": 200}