Skip to content

Commit

Permalink
Update honda_api.py
Browse files Browse the repository at this point in the history
Replace this generic exception class with a more specific one.
"Exception" and "BaseException" should not be raised python:S112
  • Loading branch information
Danieldiazi authored Dec 9, 2024
1 parent 61dff3f commit eeaba1d
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions custom_components/honda_recall_check/honda_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import requests
import json

class HondaRecallAPIError(Exception):
"""Excepción personalizada para errores en la consulta a la API de Honda."""
pass

class HondaRecallAPI:
"""Clase que interactúa con la API de Honda para verificar llamadas a revisión."""
Expand All @@ -10,32 +15,33 @@ def __init__(self, vin):
def check_recall(self):
"""Envía el VIN al endpoint y procesa la respuesta JSON."""
try:
# Configurar los headers
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded"
}
payload = {"vin": self.vin}

# Realizar la solicitud POST
response = requests.post(self.BASE_URL, headers=headers, data=payload)
response.raise_for_status() # Lanza un error si la respuesta no es 200 OK
response.raise_for_status() # Lanza requests.exceptions.HTTPError si no es 200 OK

# Parsear la respuesta JSON
json_response = response.json()

# Extraer detalles de llamadas a revisión si existen
if "recall" in json_response and "updateDetails" in json_response["recall"]:
updates = json_response["recall"]["updateDetails"]
return [
{"campaign": update["campaign"], "description": update["description"]}
for update in updates
]

# No hay llamadas a revisión
return []
except Exception as e:
raise Exception(f"Error al consultar Honda API: {e}")
except requests.exceptions.RequestException as e:
# Error relacionado con la solicitud HTTP
raise HondaRecallAPIError(f"Error de red o HTTP al consultar Honda API: {e}")
except json.JSONDecodeError as e:
# Error al parsear el JSON
raise HondaRecallAPIError(f"Error al parsear la respuesta JSON: {e}")
except KeyError as e:
# Error al acceder a llaves inexistentes en el JSON
raise HondaRecallAPIError(f"La estructura del JSON no es la esperada: faltante {e}")



Expand Down

0 comments on commit eeaba1d

Please sign in to comment.