22import sys
33import time
44
5- import requests
6-
75from config import API_BASE_URL
6+ from api ._client import _req
87
98
10- def _req (method , url , ** kwargs ):
11- start = time .time ()
12- resp = requests .request (method , url , ** kwargs )
13- ms = (time .time () - start ) * 1000
14- logging .info (f"[CLIENT] { method .upper ()} { url } -> { resp .status_code } ({ ms :.0f} ms)" )
15- return resp
16-
179
1810def check_api_health (retries = 3 , delay = 3 ):
1911 logging .info (API_BASE_URL )
@@ -31,7 +23,7 @@ def check_api_health(retries=3, delay=3):
3123 sys .exit (1 )
3224
3325
34- class SheetManager :
26+ class ApiClient :
3527 def checkin_by_uuid (self , uuid ):
3628 try :
3729 resp = _req ("GET" , f"{ API_BASE_URL } /check-in/uuid/{ uuid } " , timeout = 10 )
@@ -58,26 +50,50 @@ def set_traffic_light(self, color):
5850
5951 def get_traffic_light (self ):
6052 try :
61- resp = requests . get ( f"{ API_BASE_URL } /traffic-light" , timeout = 5 )
53+ resp = _req ( "GET" , f"{ API_BASE_URL } /traffic-light" , timeout = 5 )
6254 return resp .json ().get ("color" , "off" )
6355 except Exception as e :
6456 logging .error (f"Error getting traffic light: { e } " )
6557 return "off"
6658
67- def create_account (self , first_name , last_name , email , pid , rfid ):
59+ def lookup_by_pid (self , pid ):
60+ """Returns dict with first_name/last_name/email/pid, or None if not found."""
61+ try :
62+ resp = _req ("GET" , f"{ API_BASE_URL } /accounts/lookup/pid/{ pid } " , timeout = 10 )
63+ if resp .status_code == 404 :
64+ return None
65+ resp .raise_for_status ()
66+ return resp .json ()
67+ except Exception as e :
68+ logging .error (f"Error looking up student by pid { pid } : { e } " )
69+ return None
70+
71+ def lookup_by_barcode (self , barcode ):
72+ """Returns dict with first_name/last_name/email/pid, or None if not found."""
73+ try :
74+ resp = _req ("GET" , f"{ API_BASE_URL } /accounts/lookup/barcode/{ barcode } " , timeout = 10 )
75+ if resp .status_code == 404 :
76+ return None
77+ resp .raise_for_status ()
78+ return resp .json ()
79+ except Exception as e :
80+ logging .error (f"Error looking up student by barcode: { e } " )
81+ return None
82+
83+ def create_account (self , rfid , * , barcode = None , pid = None , first_name = None , last_name = None , email = None ):
6884 try :
69- resp = _req (
70- "POST" ,
71- f" { API_BASE_URL } /accounts" ,
72- json = {
73- "first_name" : first_name ,
74- "last_name" : last_name ,
75- "email" : email ,
76- "pid" : pid ,
77- "rfid" : rfid ,
78- },
79- timeout = 30 ,
80- )
85+ payload = { "rfid" : rfid }
86+ if barcode :
87+ payload [ "barcode" ] = barcode
88+ if pid :
89+ payload [ "pid" ] = pid
90+ if first_name :
91+ payload [ "first_name" ] = first_name
92+ if last_name :
93+ payload [ "last_name" ] = last_name
94+ if email :
95+ payload [ "email" ] = email
96+ resp = _req ( "POST" , f" { API_BASE_URL } /accounts" , json = payload , timeout = 30 )
8197 resp .raise_for_status ()
8298 return resp .json ()
8399 except Exception as e :
0 commit comments