diff --git a/iol/handler.py b/iol/handler.py index 0a0250d..bd4d5b6 100644 --- a/iol/handler.py +++ b/iol/handler.py @@ -12,10 +12,14 @@ SECRET = open("/var/openfaas/secrets/iol-api-secret").read().strip() router = APIRouter() +last_results = {} + + def get_ttl_hash(seconds=3600): """Return the same value withing `seconds` time period""" return round(time.time() / seconds) + @lru_cache def get_token(ttl_hash=None): logging.error("getting token") @@ -41,11 +45,14 @@ paises = { "AR": "argentina", } + @lru_cache def get(url, ttl_hash=None): logging.error("getting data") access, refresh = get_token(ttl_hash=get_ttl_hash()) - response = r.get(url, headers={"Authorization": "Bearer " + access, "Accept": "application/json"}) + response = r.get( + url, headers={"Authorization": "Bearer " + access, "Accept": "application/json"} + ) return response.json() @@ -61,7 +68,14 @@ def get_accion(secret, pais, accion): + f"/api/v2/Cotizaciones/acciones/{pais}/Todos?cotizacionInstrumentoModel.instrumento=acciones&cotizacionInstrumentoModel.pais={paises[pais]}&api_key={access}" ) data = get(url, ttl_hash=get_ttl_hash()) - return [a for a in data["titulos"] if a["simbolo"] == accion][0] + try: + last_results[f"accion/{pais}/{accion}"] = [ + a for a in data["titulos"] if a["simbolo"] == accion + ][0] + except: + pass + return last_results[f"accion/{pais}/{accion}"] + @router.get("/{secret}/bono/{nombre}") def get_bono(secret, nombre): @@ -74,7 +88,14 @@ def get_bono(secret, nombre): + f"/api/v2/Cotizaciones/titulosPublicos/ar/Todos?cotizacionInstrumentoModel.instrumento=titulosPublicos&cotizacionInstrumentoModel.pais=argentina&api_key={access}" ) data = get(url, ttl_hash=get_ttl_hash()) - return [a for a in data["titulos"] if a["simbolo"] == nombre][0] + try: + last_results[f"bono/{nombre}"] = [ + a for a in data["titulos"] if a["simbolo"] == nombre + ][0] + except: + pass + return last_results[f"bono/{nombre}"] + @router.get("/{secret}/cedear/{nombre}") def get_cedear(secret, nombre): @@ -87,7 +108,14 @@ def get_cedear(secret, nombre): + f"/api/v2/Cotizaciones/cedears/ar/Todos?cotizacionInstrumentoModel.instrumento=cedears&cotizacionInstrumentoModel.pais=argentina&api_key={access}" ) data = get(url, ttl_hash=get_ttl_hash()) - return [a for a in data["titulos"] if a["simbolo"] == nombre][0] + try: + last_results[f"cedear/{nombre}"] = [ + a for a in data["titulos"] if a["simbolo"] == nombre + ][0] + except: + pass + return last_results[f"cedear/{nombre}"] + @router.get("/{secret}/ON/{nombre}") def get_on(secret, nombre): @@ -100,7 +128,14 @@ def get_on(secret, nombre): + f"/api/v2/Cotizaciones/obligacionesnegociables/ar/Todos?cotizacionInstrumentoModel.instrumento=obligacionesNegociables&cotizacionInstrumentoModel.pais=argentina&api_key={access}" ) data = get(url, ttl_hash=get_ttl_hash()) - return [a for a in data["titulos"] if a["simbolo"] == nombre][0] + try: + last_results[f"ON/{nombre}"] = [ + a for a in data["titulos"] if a["simbolo"] == nombre + ][0] + except: + pass + return last_results[f"ON/{nombre}"] + @router.get("/{secret}/ADR/{nombre}") def get_adrs(secret, nombre): @@ -113,7 +148,15 @@ def get_adrs(secret, nombre): + f"/api/v2/Cotizaciones/adrs/us/Todos?cotizacionInstrumentoModel.instrumento=aDRs&cotizacionInstrumentoModel.pais=estados_Unidos&api_key={access}" ) data = get(url, ttl_hash=get_ttl_hash()) - return [a for a in data["titulos"] if a["simbolo"] == nombre][0] + try: + last_results[f"ADR/{nombre}"] = [ + a for a in data["titulos"] if a["simbolo"] == nombre + ][0] + except: + pass + return last_results[f"ADR/{nombre}"] + + @router.get("/{secret}/ON/{nombre}") def get_on(secret, nombre): if secret != SECRET: @@ -125,4 +168,8 @@ def get_on(secret, nombre): + f"/api/v2/Cotizaciones/obligacionesnegociables/ar/Todos?cotizacionInstrumentoModel.instrumento=obligacionesNegociables&cotizacionInstrumentoModel.pais=argentina&api_key={access}" ) data = get(url, ttl_hash=get_ttl_hash()) - return [a for a in data["titulos"] if a["simbolo"] == nombre][0] + try: + last_results[f"ON/{nombre}"]=[a for a in data["titulos"] if a["simbolo"] == nombre][0] + except: + pass + return last_results[f"ON/{nombre}"] \ No newline at end of file