-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_profile.py
82 lines (79 loc) · 2.47 KB
/
get_profile.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
# VIA API
# Pending to implement queryparameters
__author__ = 'Randal'
import boto3, os
import datetime
import hashlib
import json
def return_error(statusCode, message):
response = {
"statusCode": statusCode,
"headers": {
"Access-Control-Allow-Origin" : "*"
},
"body": message
}
return response
def handler(event, context):
tags = []
#Values checker
print("Event Initial: "+str(event))
if 'body' in event:
# Si el evento se llama desde apigateway(Lambda Proxy), el evento original vendra en el body
# Y nos los quedaremos. Si no, usamos el evento original ya que traera todos los datos
event = json.loads(event['body'])
print("Event took(Body): "+str(event))
if 'username' not in event:
return return_error(400, "Username not given")
username = event["username"]
user = get_user_given_username(username)
if username is False:
return return_error(404, "User doesn't exist in Dynamo DB ")
user_dic = parse_dynamo_response(user)
if user_dic is False:
return return_error(500, "Error parsing the user obtained from Dynamo DB")
return {
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin" : "*"
},
"body": json.dumps(user_dic)
}
# Notification
def parse_dynamo_response(user):
user_dict = {}
print("Parsing the user given from dynamo")
try:
for key, value in user.items():
for k, v in value.items():
user_dict[key] = v
print("New User dic "+str(user_dict))
return user_dict
except Exception as e:
print("Error parsing user obtaied from dynamo db to User Dic")
return False
#print json.dumps(dict(item))
def get_user_given_username(username):
print("Getting user")
client = boto3.client('dynamodb')
response = client.scan(
TableName=os.environ['tableUsers'],#"Users",#
Select='ALL_ATTRIBUTES',
ScanFilter={
'Username':{
'AttributeValueList': [{
'S': username
}
],
'ComparisonOperator': 'EQ'
}
}
)
try:
return response["Items"][0]
except IndexError as ie:
print("Error in index "+str(ie))
return False
except KeyError as ke:
print("Error in key "+str(ke))
return False