-
Notifications
You must be signed in to change notification settings - Fork 0
/
IoT-II-2023-yaira-cliente-API
52 lines (44 loc) · 1.66 KB
/
IoT-II-2023-yaira-cliente-API
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
import json
import boto3
def lambda_handler(event, context):
# Conexión a DynamoDB
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('/yaira_data')
# Imprimir el evento recibido
print(event)
# Asegurar que 'requestContext' exista antes de intentar acceder a 'routeKey'
if 'requestContext' in event and 'routeKey' in event['requestContext']:
if event['requestContext']['routeKey'] == '$connect':
# Manejar la conexión
connectionid = event['requestContext']['connectionId']
print("Evento de conexión:", event)
# Obtener item de la tabla DynamoDB
response = table.get_item(
Key={'id_key': "connectionid"}
)
# Actualizar o insertar un nuevo item en la tabla
if 'Item' in response:
response = table.update_item(
Key={
'id_key': "connectionid"
},
UpdateExpression="set id=:id",
ExpressionAttributeValues={
':id': connectionid
},
ReturnValues="UPDATED_NEW"
)
else:
table.put_item(
Item={'id_key': "connectionid", 'id': connectionid}
)
elif event['requestContext']['routeKey'] == '$disconnect':
# Manejar la desconexión
response = table.delete_item(
Key={'id_key': "connecionid"}
)
# Respuesta genérica
return {
'statusCode': 200,
'body': json.dumps('¡Hola desde Lambda!')
}