-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambdaFunction.py
48 lines (38 loc) · 1.32 KB
/
lambdaFunction.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
import json
import requests
def lambda_handler(event, context):
#LOG
print(event)
#COMPROBACION DE PARAMETROS
if not 'date' in event or not 'base' in event:
return 'Los parametros son incorrectos'
#DATOS REQUEST API
baseURL = 'http://api.exchangeratesapi.io/v1/'
date = event['date'].split('-')
date = date[0] + f'-{int(date[1]):02d}' + f'-{int(date[2]):02d}'
apiKey = ''
base = event['base']
URL = f'{baseURL}{date}?access_key={apiKey}&base={base}'
#REQUEST
response = requests.get(URL)
responseCode = response.status_code
data = response.json()
#MANEJO DE ERRORES
errors = ({
104: 'Se supero la cuota de consultas mensuales a la API.',
106: 'La solicitud no obtuvo resultados.',
201: 'Se ingreso una moneda invalida.',
301: 'No se ingreso ninguna fecha.',
302: 'Se ingreso una fecha invalida.',
400: 'Ocurrio un error inesperado.' })
if responseCode in errors:
error = f'Error {responseCode}: {errors[responseCode]}'
return {
'success': False,
'Error': str(error)
}
elif 'success' in data and data['success'] == True:
return {
'success': True,
'rates': data['rates']
}