File size: 5,438 Bytes
092e3d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import requests
import streamlit as st
from .constants import BACKEND_URL
from typing import Optional, Dict, Any
import json


class APIClient:
    """API client for backend communication"""

    BASE_URL = BACKEND_URL

    @staticmethod
    def create_room(room_name: str, teacher_id: str, teacher_name: str) -> Optional[Dict]:
        """Create exam room"""
        try:
            response = requests.post(
                f"{APIClient.BASE_URL}/api/rooms/create",
                params={
                    "room_name": room_name,
                    "teacher_id": teacher_id,
                    "teacher_name": teacher_name
                }
            )
            response.raise_for_status()
            return response.json()
        except Exception as e:
            st.error(f"Failed to create room: {str(e)}")
            return None

    @staticmethod
    def get_room(room_id: str) -> Optional[Dict]:
        """Get room details"""
        try:
            response = requests.get(f"{APIClient.BASE_URL}/api/rooms/{room_id}")
            response.raise_for_status()
            return response.json()
        except Exception as e:
            st.error(f"Room not found: {str(e)}")
            return None

    @staticmethod
    def join_room(room_id: str, student_id: str, student_name: str) -> Optional[Dict]:
        """Student joins room"""
        try:
            response = requests.post(
                f"{APIClient.BASE_URL}/api/rooms/{room_id}/join",
                params={
                    "student_id": student_id,
                    "student_name": student_name
                }
            )
            response.raise_for_status()
            return response.json()
        except Exception as e:
            st.error(f"Failed to join room: {str(e)}")
            return None

    @staticmethod
    def create_question(room_id: str, title: str, description: str, languages: list = None) -> Optional[Dict]:
        """Create question"""
        try:
            response = requests.post(
                f"{APIClient.BASE_URL}/api/rooms/{room_id}/questions",
                params={
                    "title": title,
                    "description": description,
                    "allowed_languages": languages or ["python", "javascript", "java", "cpp"]
                }
            )
            response.raise_for_status()
            return response.json()
        except Exception as e:
            st.error(f"Failed to create question: {str(e)}")
            return None

    @staticmethod
    def get_questions(room_id: str) -> Optional[Dict]:
        """Get all questions in room"""
        try:
            response = requests.get(f"{APIClient.BASE_URL}/api/rooms/{room_id}/questions")
            response.raise_for_status()
            return response.json()
        except Exception as e:
            st.error(f"Failed to load questions: {str(e)}")
            return None

    @staticmethod
    def get_worksheet(room_id: str, student_id: str, question_id: str) -> Optional[Dict]:
        """Get student's worksheet"""
        try:
            response = requests.get(
                f"{APIClient.BASE_URL}/api/worksheets/{room_id}/{student_id}/{question_id}"
            )
            response.raise_for_status()
            return response.json()
        except Exception as e:
            st.error(f"Failed to load worksheet: {str(e)}")
            return None

    @staticmethod
    def save_code(worksheet_id: str, code: str, language: str) -> bool:
        """Auto-save code"""
        try:
            response = requests.post(
                f"{APIClient.BASE_URL}/api/worksheets/{worksheet_id}/save",
                params={
                    "code": code,
                    "language": language
                }
            )
            response.raise_for_status()
            return True
        except:
            return False

    @staticmethod
    def execute_code(worksheet_id: str, code: str, language: str, input_data: str = "") -> Optional[Dict]:
        """Execute code"""
        try:
            response = requests.post(
                f"{APIClient.BASE_URL}/api/worksheets/{worksheet_id}/execute",
                json={
                    "code": code,
                    "language": language,
                    "input_data": input_data
                }
            )
            response.raise_for_status()
            return response.json()
        except Exception as e:
            return {
                "status": "error",
                "output": "None",
                "stderr": str(e)
            }

    @staticmethod
    def get_students(room_id: str) -> Optional[Dict]:
        """Get room students (Teacher)"""
        try:
            response = requests.get(f"{APIClient.BASE_URL}/api/rooms/{room_id}/students")
            response.raise_for_status()
            return response.json()
        except Exception as e:
            st.error(f"Failed to load students: {str(e)}")
            return None

    @staticmethod
    def get_student_progress(room_id: str, student_id: str) -> Optional[Dict]:
        """Get student progress"""
        try:
            response = requests.get(
                f"{APIClient.BASE_URL}/api/rooms/{room_id}/student/{student_id}/progress"
            )
            response.raise_for_status()
            return response.json()
        except Exception as e:
            return None