-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlambda_test.py
More file actions
352 lines (315 loc) · 11.9 KB
/
Copy pathlambda_test.py
File metadata and controls
352 lines (315 loc) · 11.9 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import json
import logging
import os
from openedx_rest_api_client import client as openedx_client
from functools import reduce
logger = logging.getLogger()
logger.setLevel(int(os.environ.get("LOGGING_LEVEL", logging.INFO)))
LMS_CLIENT_ID = os.environ.get('LMS_CLIENT_ID')
LMS_CLIENT_SECRET = os.environ.get('LMS_CLIENT_SECRET')
LMS_URL = os.environ.get('LMS_URL')
HUBSPOT_TOKEN = os.environ.get('HUBSPOT_TOKEN')
lms_client = openedx_client.OpenedxRESTAPIClient(
base_url=LMS_URL, client_id=LMS_CLIENT_ID, client_secret=LMS_CLIENT_SECRET)
def lambda_handler(event, context):
"""
Handle webhooks and filters coming from Hubspot.
Parameters
----------
event: dict, required
API Gateway Lambda Proxy Input Format
Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
context: object, required
Lambda Context runtime methods and attributes
Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html
Returns
------
API Gateway Lambda Proxy Output Format: dict
Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
"""
logger.info(f"Event: {json.dumps(event)}")
body_str = event.get('body')
if body_str:
try:
body = json.loads(body_str)
except json.JSONDecodeError as e:
message = f"JSON Decode Error '{e.msg}' loading '{e.doc}' in position {e.pos}"
logger.error(message)
return {
'status_code': 406,
'message': message
}
else:
message = "No body in event"
logger.error(message)
return {
'status_code': 406,
'message': message
}
email = body.get('email')
first_name = body.get('First_Name')
last_name = body.get('Last_name')
full_name = ' '.join([first_name, last_name])
course_id = body.get('Course_Id')
password = str(body.get('Password'))
messages = []
# initially take the user name as the first word of the first name.
username = first_name.split(' ')[0]
logger.info(f"Registering {full_name} ({email}) with password {password} and enrolling into {course_id}.")
# Create a client to make API calls
lms_client = openedx_client.OpenedxRESTAPIClient(
base_url=LMS_URL, client_id=LMS_CLIENT_ID, client_secret=LMS_CLIENT_SECRET)
# Validate the data
validation_result = lms_client.validation_registration(
email=email,
username=username,
name=full_name,
password=password,
)
validation_decisions = validation_result.get('validation_decisions', {})
if not validation_decisions:
return {
'status_code': 400,
'validation_result': validation_result
}
# Check if the user exists. In this case, don't stop, just don't register and continue
user_exists = validation_decisions.get('email', "") == f"It looks like {email} belongs to an existing account. Try again with a different email address."
if user_exists:
message = f"User with email {email} already exists. Skipping registration."
logger.warning(message)
messages.append(message)
else:
# If the user does not exist, register it.
# First check if the form is valid in the other fields, except username errors (handled separately)
form_is_valid = not bool(reduce(lambda a, b: a+b, [v for k, v in validation_decisions.items() if k != 'username'], ''))
if not form_is_valid:
message = (f"Error validating data for {email}. "
f"{'. '.join([k + ': ' + v for k,v in validation_decisions.items() if v])}")
logger.error(message)
return {
"statusCode": 422,
"validation_errors": message,
}
logger.info("Form is valid.")
# Handle errors in username, replace by one of the suggestions
if validation_decisions.get('username') == (f"It looks like {username} belongs to an existing account. "
f"Try again with a different username."):
username_suggestions = validation_result.get('username_suggestions')
message = (f"Invalid username '{username}': '{validation_decisions.get('username')}'. "
f"Replacing by '{username_suggestions[0]}'")
logger.warning(message)
username = username_suggestions[0]
messages.append(message)
# Handle other type of username errors
elif validation_decisions.get('username'):
message = f"Error validating data for {email}. username: {validation_decisions.get('username')}"
return {
'status_code': 422,
'validation_errors': message
}
# Register the user
registration_result = lms_client.register_account(
email=email,
username=username,
name=full_name,
password=password,
course_id=course_id,
terms_of_service='true',
)
if not registration_result.get('success'):
error_message = {
k: v[0].get('user_message')
for k, v in registration_result.items()
if k not in {'error_code', 'username_suggestions'}
}
message = f"Error registering {email}. {'. '.join([k + ': ' + v for k,v in error_message.items()])}"
logger.error(message)
return {
"statusCode": 422,
"message": message,
}
else:
message = f"Successfully registered {full_name} ({email}) with password {password} and username '{username}'."
logger.info(message)
messages.append(message)
lms_client2 = openedx_client.OpenedxRESTAPIClient(
base_url=LMS_URL, client_id=LMS_CLIENT_ID, client_secret=LMS_CLIENT_SECRET)
# Enroll the user in the course
enrollment_result = lms_client2.change_enrollment(
emails=[email],
courses=[course_id],
action='unenroll',
email_students=False,
)
if 'detail' in enrollment_result:
error_message = enrollment_result.get('detail')
logger.error(f"Error enrolling {email} in {course_id}: {error_message}")
return {
"statusCode": 422,
"message": error_message,
}
elif 'status_code' in enrollment_result:
error_message = enrollment_result.get('response')
logger.error(f"Error {enrollment_result.get('status_code')} enrolling {email} in {course_id}: {error_message}")
return {
"statusCode": enrollment_result.get('status_code'),
"message": error_message,
}
else:
message = f"Successfully enrolled {email} in course {course_id}."
logger.info(message)
messages.append(message)
return {
"statusCode": 200,
"messages": messages
}
event = {
"resource": "/hubspot_webhook",
"path": "/hubspot_webhook/",
"httpMethod": "POST",
"headers": {
"Accept": "application/json",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-ASN": "14618",
"CloudFront-Viewer-Country": "US",
"Content-Type": "application/json",
"Host": "k5bqz34xfi.execute-api.us-east-1.amazonaws.com",
"User-Agent": "HubSpot Connect 2.0 (http://dev.hubspot.com/) (namespace: WebhooksPlatformExecution) - WebhooksPlatformService-userweb",
"Via": "1.1 3c7ef4fe3f4a27b69b8df486fc6210e4.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "l20OhUnGPw627SZwfot7qLrI5pCP69qiOwz8tRKd-uC5LgulKPkdpQ==",
"X-Amzn-Trace-Id": "Root=1-650b5f81-511c4eff1fc2385739854506",
"X-Forwarded-For": "54.174.62.149, 130.176.137.73",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https",
"X-HS-Internal-Request": "0",
"X-HS-Internal-User-Request": "0",
"X-HS-User-Request": "1",
"X-HubSpot-Client-IP": "190.229.27.129",
"X-HubSpot-Correlation-Id": "8b46dac7-c49d-48e0-a634-caa7d539bd84",
"X-HubSpot-Requesting-Chain-bin": "CpYBChxhdXRvbWF0aW9uLXVpLWNhbnZhc0AxLjM1MDc4EgJVSRpyaHR0cHM6Ly9hcHAuaHVic3BvdC5jb20vd29ya2Zsb3dzLzI4Njk4OTEvcGxhdGZvcm0vZmxvdy80NzE0NDQ0OTQvZWRpdC9hY3Rpb25zL25ldy1hY3Rpb24vd2ViaG9vaz9lZGdlSWQ9NC1lZGdlLTEmEgIIBA==",
"X-HubSpot-Timeout-Millis": "24988",
"X-Trace": ""
},
"multiValueHeaders": {
"Accept": [
"application/json"
],
"CloudFront-Forwarded-Proto": [
"https"
],
"CloudFront-Is-Desktop-Viewer": [
"true"
],
"CloudFront-Is-Mobile-Viewer": [
"false"
],
"CloudFront-Is-SmartTV-Viewer": [
"false"
],
"CloudFront-Is-Tablet-Viewer": [
"false"
],
"CloudFront-Viewer-ASN": [
"14618"
],
"CloudFront-Viewer-Country": [
"US"
],
"Content-Type": [
"application/json"
],
"Host": [
"k5bqz34xfi.execute-api.us-east-1.amazonaws.com"
],
"User-Agent": [
"HubSpot Connect 2.0 (http://dev.hubspot.com/) (namespace: WebhooksPlatformExecution) - WebhooksPlatformService-userweb"
],
"Via": [
"1.1 3c7ef4fe3f4a27b69b8df486fc6210e4.cloudfront.net (CloudFront)"
],
"X-Amz-Cf-Id": [
"l20OhUnGPw627SZwfot7qLrI5pCP69qiOwz8tRKd-uC5LgulKPkdpQ=="
],
"X-Amzn-Trace-Id": [
"Root=1-650b5f81-511c4eff1fc2385739854506"
],
"X-Forwarded-For": [
"54.174.62.149, 130.176.137.73"
],
"X-Forwarded-Port": [
"443"
],
"X-Forwarded-Proto": [
"https"
],
"X-HS-Internal-Request": [
"0"
],
"X-HS-Internal-User-Request": [
"0"
],
"X-HS-User-Request": [
"1"
],
"X-HubSpot-Client-IP": [
"190.229.27.129"
],
"X-HubSpot-Correlation-Id": [
"8b46dac7-c49d-48e0-a634-caa7d539bd84"
],
"X-HubSpot-Requesting-Chain-bin": [
"CpYBChxhdXRvbWF0aW9uLXVpLWNhbnZhc0AxLjM1MDc4EgJVSRpyaHR0cHM6Ly9hcHAuaHVic3BvdC5jb20vd29ya2Zsb3dzLzI4Njk4OTEvcGxhdGZvcm0vZmxvdy80NzE0NDQ0OTQvZWRpdC9hY3Rpb25zL25ldy1hY3Rpb24vd2ViaG9vaz9lZGdlSWQ9NC1lZGdlLTEmEgIIBA=="
],
"X-HubSpot-Timeout-Millis": [
"24988"
],
"X-Trace": [
""
]
},
"queryStringParameters": None,
"multiValueQueryStringParameters": None,
"pathParameters": None,
"stageVariables": None,
"requestContext": {
"resourceId": "wctzch",
"resourcePath": "/hubspot_webhook",
"httpMethod": "POST",
"extendedRequestId": "LkvcOFxKoAMFj-A=",
"requestTime": "20/Sep/2023:21:09:21 +0000",
"path": "/Prod/hubspot_webhook/",
"accountId": "212507341974",
"protocol": "HTTP/1.1",
"stage": "Prod",
"domainPrefix": "k5bqz34xfi",
"requestTimeEpoch": 1695244161189,
"requestId": "30361206-c19c-4260-894d-14433d37a443",
"identity": {
"cognitoIdentityPoolId": None,
"accountId": None,
"cognitoIdentityId": None,
"caller": None,
"sourceIp": "54.174.62.149",
"principalOrgId": None,
"accessKey": None,
"cognitoAuthenticationType": None,
"cognitoAuthenticationProvider": None,
"userArn": None,
"userAgent": "HubSpot Connect 2.0 (http://dev.hubspot.com/) (namespace: WebhooksPlatformExecution) - WebhooksPlatformService-userweb",
"user": None
},
"domainName": "k5bqz34xfi.execute-api.us-east-1.amazonaws.com",
"apiId": "k5bqz34xfi"
},
"body": "{\"email\":\"jaimematarazzo+webhooktest3@gmail.com\",\"First_Name\":\"Jaime\",\"Last_name\":\"Test\",\"Course_Id\":\"course-v1:ALEPH+DIG-AD-SP+ENG01\",\"Password\":123456}",
"isBase64Encoded": False
}
for i in range(1,2):
r = lambda_handler(event, None)
print(i, r)
if r.get('statusCode') != 200:
break