frontend stuff elkay
Browse files- FinED-Front-output.md +0 -0
- FinED-Front-output.txt +0 -0
- utils/api.py +34 -27
FinED-Front-output.md
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
FinED-Front-output.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
utils/api.py
CHANGED
|
@@ -1,46 +1,53 @@
|
|
| 1 |
# utils/api.py
|
| 2 |
import os, requests
|
| 3 |
|
| 4 |
-
BACKEND = (os.getenv("BACKEND_URL") or "").strip()
|
| 5 |
if not BACKEND:
|
| 6 |
raise RuntimeError("BACKEND_URL is not set in Space secrets.")
|
| 7 |
-
BACKEND = BACKEND.rstrip("/")
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def health():
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
def get_quiz(student_id:int, lesson_id:int, level_slug:str):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return r.json()["items"]
|
| 24 |
|
| 25 |
def grade_quiz(student_id:int, lesson_id:int, level_slug:str, answers:list[str], assignment_id:int|None=None):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
def next_step(student_id:int, lesson_id:int, level_slug:str, answers:list[str], assignment_id:int|None=None):
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
def login(email: str, password: str):
|
| 36 |
-
|
| 37 |
-
r.raise_for_status()
|
| 38 |
-
return r.json()
|
| 39 |
|
| 40 |
def signup_student(name: str, email: str, password: str, level_label: str, country_label: str):
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
})
|
| 45 |
-
r.raise_for_status()
|
| 46 |
-
return r.json()
|
|
|
|
| 1 |
# utils/api.py
|
| 2 |
import os, requests
|
| 3 |
|
| 4 |
+
BACKEND = (os.getenv("BACKEND_URL") or "").strip().rstrip("/")
|
| 5 |
if not BACKEND:
|
| 6 |
raise RuntimeError("BACKEND_URL is not set in Space secrets.")
|
|
|
|
| 7 |
|
| 8 |
+
TOKEN = (os.getenv("BACKEND_TOKEN") or "").strip()
|
| 9 |
|
| 10 |
+
_session = requests.Session()
|
| 11 |
+
_session.timeout = 30 # default timeout for all calls via this session
|
| 12 |
+
if TOKEN:
|
| 13 |
+
_session.headers["Authorization"] = f"Bearer {TOKEN}"
|
| 14 |
+
|
| 15 |
+
def _req(method: str, path: str, **kw):
|
| 16 |
+
url = f"{BACKEND}{path}"
|
| 17 |
+
r = _session.request(method, url, **kw)
|
| 18 |
+
try:
|
| 19 |
+
r.raise_for_status()
|
| 20 |
+
except requests.HTTPError as e:
|
| 21 |
+
# make errors readable in Streamlit
|
| 22 |
+
raise RuntimeError(f"{method} {path} failed [{r.status_code}]: {r.text[:500]}") from e
|
| 23 |
+
return r
|
| 24 |
|
| 25 |
def health():
|
| 26 |
+
return _req("GET", "/health").json()
|
| 27 |
+
|
| 28 |
+
def start_agent(student_id:int, lesson_id:int, level_slug:str):
|
| 29 |
+
return _req("POST", "/agent/start",
|
| 30 |
+
json={"student_id":student_id,"lesson_id":lesson_id,"level_slug":level_slug}).json()
|
| 31 |
|
| 32 |
def get_quiz(student_id:int, lesson_id:int, level_slug:str):
|
| 33 |
+
return _req("POST", "/agent/quiz",
|
| 34 |
+
json={"student_id":student_id,"lesson_id":lesson_id,"level_slug":level_slug}).json()["items"]
|
|
|
|
| 35 |
|
| 36 |
def grade_quiz(student_id:int, lesson_id:int, level_slug:str, answers:list[str], assignment_id:int|None=None):
|
| 37 |
+
d = _req("POST", "/agent/grade",
|
| 38 |
+
json={"student_id":student_id,"lesson_id":lesson_id,"level_slug":level_slug,
|
| 39 |
+
"answers":answers,"assignment_id":assignment_id}).json()
|
| 40 |
+
return d["score"], d["total"]
|
| 41 |
|
| 42 |
def next_step(student_id:int, lesson_id:int, level_slug:str, answers:list[str], assignment_id:int|None=None):
|
| 43 |
+
return _req("POST", "/agent/coach_or_celebrate",
|
| 44 |
+
json={"student_id":student_id,"lesson_id":lesson_id,"level_slug":level_slug,
|
| 45 |
+
"answers":answers,"assignment_id":assignment_id}).json()
|
| 46 |
|
| 47 |
def login(email: str, password: str):
|
| 48 |
+
return _req("POST", "/auth/login", json={"email": email, "password": password}).json()
|
|
|
|
|
|
|
| 49 |
|
| 50 |
def signup_student(name: str, email: str, password: str, level_label: str, country_label: str):
|
| 51 |
+
return _req("POST", "/auth/signup/student",
|
| 52 |
+
json={"name": name, "email": email, "password": password,
|
| 53 |
+
"level_label": level_label, "country_label": country_label}).json()
|
|
|
|
|
|
|
|
|