-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgestion_adopciones_lambda.py
310 lines (252 loc) · 12.2 KB
/
gestion_adopciones_lambda.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
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
# Filename: gestion_adopciones_lambda.py
# Author: Gerardo Cataño
# Created: 2024-07-16
# Description: Módulo Back-end de gestión de adopciones para despliegue en servicio AWS Lambda
import json
import boto3
from botocore.exceptions import ClientError
from decimal import Decimal
from boto3.dynamodb.conditions import Key
#Inicializa rutas
solicitudes_path = '/api/solicitudes'
mascotas_path = '/api/mascotas'
usuarios_path = '/api/usuarios'
centros_path = '/api/centros'
hogares_path = '/api/hogares'
#Inicializa cliente DynamoDB y tablas
dynamodb = boto3.resource('dynamodb', region_name='us-east-2') #interfaz de recursos: actualizar region
solicitudes_table = dynamodb.Table('SolicitudAdopcion')
mascotas_table = dynamodb.Table('MascotaAdopcion')
usuarios_table = dynamodb.Table('UsuarioAdoptante')
centros_table = dynamodb.Table('CentroAdopcion')
hogares_table = dynamodb.Table('HogarAdoptante')
def lambda_handler(event, context):
print('***** Debug: event: ', event)
print('***** Debug: method=', event['requestContext']['http']['method'], ' path=', event['requestContext']['http']['path'])
response = None
try:
http_method = event['requestContext']['http']['method']
path = event['requestContext']['http']['path']
#Lógica basada en rutas y posteriormente en métodos
if path == solicitudes_path:
if http_method == 'GET':
#Logica para saber si solicitudId existe en queryStringParameters de evento
isSolicitudIdPresent = False
for i in event:
if 'queryStringParameters' in i:
solicitud_id = event['queryStringParameters']['solicitudId']
response = consulta_item(solicitudes_table, 'solicitudId', solicitud_id)
isSolicitudIdPresent = True
break
#not in for i:
if isSolicitudIdPresent != True:
response = consulta_items(solicitudes_table)
elif http_method == 'POST':
response = alta_item(solicitudes_table, json.loads(event['body']))
elif http_method == 'PUT':
body = json.loads(event['body'])
solicitud_id = event['queryStringParameters']['solicitudId']
response = actualiza_item(solicitudes_table, 'solicitudId', solicitud_id, body['atributo'], body['valor'])
elif http_method == 'DELETE':
solicitud_id = event['queryStringParameters']['solicitudId']
response = baja_item(solicitudes_table, 'solicitudId', solicitud_id)
else:
return build_response(405, '405: Metodo no permitido')
elif path == mascotas_path:
if http_method == 'GET':
#Logica para saber si mascotaId existe en queryStringParameters de evento
isMascotaIdPresent = False
for i in event:
if 'queryStringParameters' in i:
mascota_id = event['queryStringParameters']['mascotaId']
response = consulta_item(mascotas_table, 'mascotaId', mascota_id)
isMascotaIdPresent = True
break
#not in for i:
if isMascotaIdPresent != True:
response = consulta_items(mascotas_table)
elif http_method == 'POST':
response = alta_item(mascotas_table, json.loads(event['body']))
elif http_method == 'PUT':
body = json.loads(event['body'])
mascota_id = event['queryStringParameters']['mascotaId']
response = actualiza_item(mascotas_table, 'mascotaId', mascota_id, body['atributo'], body['valor'])
elif http_method == 'DELETE':
mascota_id = event['queryStringParameters']['mascotaId']
response = baja_item(mascotas_table, 'mascotaId', mascota_id)
else:
return build_response(405, '405: Metodo no permitido')
elif path == usuarios_path:
if http_method == 'GET':
#Logica para saber si usuarioId existe en queryStringParameters de evento
isUsuarioIdPresent = False
for i in event:
if 'queryStringParameters' in i:
usuario_id = event['queryStringParameters']['usuarioId']
response = consulta_item(usuarios_table, 'usuarioId', usuario_id)
isUsuarioIdPresent = True
break
#not in for i:
if isUsuarioIdPresent != True:
response = consulta_items(usuarios_table)
elif http_method == 'POST':
response = alta_item(usuarios_table, json.loads(event['body']))
elif http_method == 'PUT':
body = json.loads(event['body'])
usuario_id = event['queryStringParameters']['usuarioId']
response = actualiza_item(usuarios_table, 'usuarioId', usuario_id, body['atributo'], body['valor'])
elif http_method == 'DELETE':
usuario_id = event['queryStringParameters']['usuarioId']
response = baja_item(usuarios_table, 'usuarioId', usuario_id)
else:
return build_response(405, '405: Metodo no permitido')
elif path == centros_path:
if http_method == 'GET':
#Logica para saber si centroId existe en queryStringParameters de evento
isCentroIdPresent = False
for i in event:
if 'queryStringParameters' in i:
centro_id = event['queryStringParameters']['centroId']
response = consulta_item(centros_table, 'centroId', centro_id)
isCentroIdPresent = True
break
#not in for i:
if isCentroIdPresent != True:
response = consulta_items(centros_table)
elif http_method == 'POST':
response = alta_item(centros_table, json.loads(event['body']))
elif http_method == 'PUT':
body = json.loads(event['body'])
centro_id = event['queryStringParameters']['centroId']
response = actualiza_item(centros_table, 'centroId', centro_id, body['atributo'], body['valor'])
elif http_method == 'DELETE':
centro_id = event['queryStringParameters']['centroId']
response = baja_item(centros_table, 'centroId', centro_id)
else:
return build_response(405, '405: Metodo no permitido')
elif path == hogares_path:
if http_method == 'GET':
#Logica para saber si hogarId existe en queryStringParameters de evento
isHogarIdPresent = False
for i in event:
if 'queryStringParameters' in i:
hogar_id = event['queryStringParameters']['hogarId']
response = consulta_item(hogares_table, 'hogarId', hogar_id)
isHogarIdPresent = True
break
#not in for i:
if isHogarIdPresent != True:
response = consulta_items(hogares_table)
elif http_method == 'POST':
response = alta_item(hogares_table, json.loads(event['body']))
elif http_method == 'PUT':
body = json.loads(event['body'])
hogar_id = event['queryStringParameters']['hogarId']
response = actualiza_item(hogares_table, 'hogarId', hogar_id, body['atributo'], body['valor'])
elif http_method == 'DELETE':
hogar_id = event['queryStringParameters']['hogarId']
response = baja_item(hogares_table, 'hogarId', hogar_id)
else:
return build_response(405, '405: Metodo no permitido')
else:
response = build_response(404, '404: Recurso no encontrado') #####not reachable
except Exception as e:
print('Error:', e)
response = build_response(400, 'Error en peticion HTTP')
return response
def consulta_item(dynamodb_table, item_key, item_id):
try:
response = dynamodb_table.get_item(Key={item_key: item_id})
#Logica para saber si Item existe en response
isItemPresent = False
for i in response:
if 'Item' in i:
return build_response(200, response.get('Item'))
isItemPresent = True
break
#not in for i:
if isItemPresent != True:
return build_response(404, '404: Item no encontrado')
except ClientError as e:
print('Error:', e)
return build_response(400, e.response['Error']['Message'])
def consulta_items(dynamodb_table):
try:
scan_params = {
'TableName': dynamodb_table.name
}
return build_response(200, scan_dynamo_records(dynamodb_table, scan_params, []))
except ClientError as e:
print('Error:', e)
return build_response(400, e.response['Error']['Message'])
def scan_dynamo_records(dynamodb_table, scan_params, item_array):
response = dynamodb_table.scan(**scan_params)
item_array.extend(response.get('Items', []))
if 'LastEvaluatedKey' in response:
scan_params['ExclusiveStartKey'] = response['LastEvaluatedKey']
return scan_dynamo_records(scan_params, item_array)
else:
return {'items': item_array}
def alta_item(dynamodb_table, request_body):
try:
dynamodb_table.put_item(Item=request_body)
body = {
'Operacion': 'Alta',
'Mensaje': 'Exitosa',
'Item': request_body
}
return build_response(201, body)
except ClientError as e:
print('Error:', e)
return build_response(400, e.response['Error']['Message'])
def actualiza_item(dynamodb_table, item_key, item_id, update_key, update_value):
try:
response = dynamodb_table.update_item(
Key={item_key: item_id},
UpdateExpression=f'SET {update_key} = :value',
ExpressionAttributeValues={':value': update_value},
ReturnValues='UPDATED_NEW'
)
body = {
'Operacion': 'Actualizacion',
'Mensaje': 'Exitosa',
'UpdatedAttributes': response
}
return build_response(200, body)
except ClientError as e:
print('Error:', e)
return build_response(400, e.response['Error']['Message'])
def baja_item(dynamodb_table, item_key, item_id):
try:
response = dynamodb_table.delete_item(
Key={item_key: item_id},
ReturnValues='ALL_OLD'
)
body = {
'Operacion': 'Baja',
'Mensaje': 'Exitosa',
'Item': response
}
return build_response(200, body)
except ClientError as e:
print('Error:', e)
return build_response(400, e.response['Error']['Message'])
class DecimalEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Decimal):
# Check if it's an int or a float
if obj % 1 == 0:
return int(obj)
else:
return float(obj)
# Let the base class default method raise the TypeError
return super(DecimalEncoder, self).default(obj)
def build_response(status_code, body):
print('***** Debug: response: status=', status_code, ' body=', body)
return {
'statusCode': status_code,
'headers': {
'Content-Type': 'application/json'
},
'body': json.dumps(body, cls=DecimalEncoder)
}