lanna_lalala;- commited on
Commit ·
91d48f5
1
Parent(s): c2769bf
init frontend
Browse files5 unmodified lines67891011121314151617181920212223242526272829303132333435363738394041424344454647481 unmodified line505152535 unmodified linesraise RuntimeError("BACKEND_URL is not set in Space secrets.")TOKEN = (os.getenv("BACKEND_TOKEN") or "").strip()_session = requests.Session()_session.timeout = 30 # default timeout for all calls via this sessionif TOKEN:_session.headers["Authorization"] = f"Bearer {TOKEN}"def _req(method: str, path: str, **kw):url = f"{BACKEND}{path}"r = _session.request(method, url, **kw)try:r.raise_for_status()except requests.HTTPError as e:# make errors readable in Streamlitraise RuntimeError(f"{method} {path} failed [{r.status_code}]: {r.text[:500]}") from ereturn rdef health():return _req("GET", "/health").json()def start_agent(student_id:int, lesson_id:int, level_slug:str):return _req("POST", "/agent/start",json={"student_id":student_id,"lesson_id":lesson_id,"level_slug":level_slug}).json()def get_quiz(student_id:int, lesson_id:int, level_slug:str):return _req("POST", "/agent/quiz",json={"student_id":student_id,"lesson_id":lesson_id,"level_slug":level_slug}).json()["items"]def grade_quiz(student_id:int, lesson_id:int, level_slug:str, answers:list[str], assignment_id:int|None=None):d = _req("POST", "/agent/grade",json={"student_id":student_id,"lesson_id":lesson_id,"level_slug":level_slug,"answers":answers,"assignment_id":assignment_id}).json()return d["score"], d["total"]def next_step(student_id:int, lesson_id:int, level_slug:str, answers:list[str], assignment_id:int|None=None):return _req("POST", "/agent/coach_or_celebrate",json={"student_id":student_id,"lesson_id":lesson_id,"level_slug":level_slug,"answers":answers,"assignment_id":assignment_id}).json()def login(email: str, password: str):return _req("POST", "/auth/login", json={"email": email, "password": password}).json()1 unmodified linedef signup_student(name: str, email: str, password: str, level_label: str, country_label: str):return _req("POST", "/auth/signup/student",json={"name": name, "email": email, "password": password,"level_label": level_label, "country_label": country_label}).json()5 unmodified lines67891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162631 unmodified line6566676869707172735 unmodified linesraise RuntimeError("BACKEND_URL is not set in Space secrets.")TOKEN = (os.getenv("BACKEND_TOKEN") or "").strip()DEFAULT_TIMEOUT = int(os.getenv("BACKEND_TIMEOUT", "30"))_session = requests.Session()if TOKEN:_session.headers["Authorization"] = f"Bearer {TOKEN}"def _req(method: str, path: str, **kw):# make sure path starts with '/'if not path.startswith("/"):path = "/" + pathurl = f"{BACKEND}{path}"# apply a default timeout unless the caller overrides itkw.setdefault("timeout", DEFAULT_TIMEOUT)try:r = _session.request(method, url, **kw)r.raise_for_status()except requests.HTTPError as e:# Surface readable API errors in the UIbody = ""try:body = r.text[:500]except Exception:passraise RuntimeError(f"{method} {path} failed [{r.status_code}]: {body}") from eexcept requests.RequestException as e:# Network/DNS/timeouts, etc.raise RuntimeError(f"{method} {path} failed: {e.__class__.__name__}: {e}") from ereturn rdef health():return _req("GET", "/health").json()def start_agent(student_id: int, lesson_id: int, level_slug: str):return _req("POST", "/agent/start",json={"student_id": student_id, "lesson_id": lesson_id, "level_slug": level_slug}).json()def get_quiz(student_id: int, lesson_id: int, level_slug: str):return _req("POST", "/agent/quiz",json={"student_id": student_id, "lesson_id": lesson_id, "level_slug": level_slug}).json()["items"]def grade_quiz(student_id: int, lesson_id: int, level_slug: str,answers: list[str], assignment_id: int | None = None):d = _req("POST", "/agent/grade",json={"student_id": student_id, "lesson_id": lesson_id, "level_slug": level_slug,"answers": answers, "assignment_id": assignment_id}).json()return d["score"], d["total"]def next_step(student_id: int, lesson_id: int, level_slug: str,answers: list[str], assignment_id: int | None = None):return _req("POST", "/agent/coach_or_celebrate",json={"student_id": student_id, "lesson_id": lesson_id, "level_slug": level_slug,"answers": answers, "assignment_id": assignment_id}).json()def login(email: str, password: str):return _req("POST", "/auth/login", json={"email": email, "password": password}).json()1 unmodified linedef signup_student(name: str, email: str, password: str, level_label: str, country_label: str):return _req("POST", "/auth/signup/student",json={"name": name, "email": email, "password": password,"level_label": level_label, "country_label": country_label}).json()# Optional: if your frontend has a teacher sign-up flowdef signup_teacher(title: str, name: str, email: str, password: str):return _req("POST", "/auth/signup/teacher",json={"title": title, "name": name, "email": email, "password": password}).json()
utils/api.py+35-15