Skip to content

Commit

Permalink
notify errors during verification
Browse files Browse the repository at this point in the history
  • Loading branch information
otariidae committed Aug 25, 2023
1 parent 7292a1c commit c604e83
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
22 changes: 12 additions & 10 deletions km4k2/card_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
9 changes: 8 additions & 1 deletion km4k2/km4k.py
Original file line number Diff line number Diff line change
Expand Up @@ -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() + ")")

Expand Down

0 comments on commit c604e83

Please sign in to comment.