-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_classrooms.py
95 lines (88 loc) · 2.97 KB
/
get_classrooms.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#VIA API
import json
import os
import boto3
def return_error(statusCode, message):
response = {
"statusCode": statusCode,
"headers": {
"Access-Control-Allow-Origin" : "*"
},
"body": message
}
return response
def parse_dynamo_response(classrooms):
classrooms_dict = []
print("Parsing the user given from dynamo")
try:
for classroom in classrooms:
classroom_dic = {}
for key, value in classroom.items():
for k, v in value.items():
classroom_dic[key] = v
classrooms_dict.append(classroom_dic)
print("New Classroom dic "+str(classroom_dic))
return classrooms_dict
except Exception as e:
print("Error parsing user obtained from dynamo db to User Dic"+str(e))
return False
def get_classrooms_from_dynamoDB(level=None):
scan ={}
if level is not None:
scan['Level'] = {
'AttributeValueList':[{
'S': level
}
],
'ComparisonOperator': 'EQ'
}
print('Scan: ')
print(scan)
client = boto3.client('dynamodb')
response = client.scan(
TableName=os.environ['tableClassroom'],
Select='ALL_ATTRIBUTES',
ScanFilter=scan
)
return response["Items"]
def handler(event, context):
level = None
print("Event Initial: "+str(event))
if 'queryStringParameters' in event:
query = event['queryStringParameters']
# Se mira si hay alguna query dentro de la URL para poder filtrar.
if query is not None:
if 'level' in query:
level = query['level']
response = get_classrooms_from_dynamoDB(level)
print(response)
classrooms = parse_dynamo_response(response)
return {
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin" : "*"
},
"body": json.dumps(classrooms)
}
def handler_with_path_parameters(event, context):
print("Event Initial: "+str(event))
if 'pathParameters' is None:
print('pathParameters is None')
return return_error(404, "Path Parameters not given. URL used should be similar to /classroom/Infantil")
pathParameters = event['pathParameters']
# Se mira si hay alguna query dentro de la URL para poder filtrar.
if 'level' not in pathParameters:
return return_error(404, "Level Path Parameters not given. URL used should be similar to /classroom/Infantil")
else:
level = pathParameters['level']
print('Level given in path parameters: '+level)
response = get_classrooms_from_dynamoDB(level)
print(response)
classrooms = parse_dynamo_response(response)
return {
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin" : "*"
},
"body": json.dumps(classrooms)
}