diff --git a/km4k2/card_sdk.py b/km4k2/card_sdk.py index 79a84ad..d75c692 100644 --- a/km4k2/card_sdk.py +++ b/km4k2/card_sdk.py @@ -13,21 +13,23 @@ def __init__(self, base_url: str, api_key: str) -> None: self.api_key = api_key def verify(self, idm: str) -> bool: + """ + Raises: + requests.exceptions.HTTPError: if API returns unexpected HTTP status + requests.exceptions.JSONDecodeError: if API returns invalid JSON + """ url = urljoin(self.base_url, "/api/card/verify") payload = json.dumps({"idm": idm}) headers = {"X-Api-Key": self.api_key, "Content-Type": "application/json"} response = requests.request("GET", url, headers=headers, data=payload) - try: - response.raise_for_status() - except requests.exceptions.HTTPError as e: - print(e) + if response.status_code == requests.codes.not_found: # unregistered return False - - try: - status = response.json() - except requests.exceptions.JSONDecodeError as e: - print(e) + if response.status_code == requests.codes.forbidden: # expired return False - return status["verified"] is not None and status["verified"] + response.raise_for_status() + + # ok + status = response.json() + return status["verified"] diff --git a/km4k2/km4k.py b/km4k2/km4k.py index 5814978..a6a6cef 100644 --- a/km4k2/km4k.py +++ b/km4k2/km4k.py @@ -25,7 +25,14 @@ def start_system(isopen, okled_pin, ngled_pin, verifier: CardVerifierInterface): while True: idm = read_nfc() if idm: - verified = verifier.verify(idm.decode()) + try: + verified = verifier.verify(idm.decode()) + except Exception as e: # noqa: BLE001 + GPIO.output(ngled_pin, GPIO.HIGH) + time.sleep(3) + GPIO.output(ngled_pin, GPIO.LOW) + print(e) + continue if verified: print("Registered (idm:" + idm.decode() + ")")