File size: 1,562 Bytes
6fe09f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Implements: GET /questions, GET /random-question, GET /files/{task_id}, POST /submit

Base URL: https://agents-course-unit4-scoring.hf.space
"""
from typing import Any, Dict, List, Optional
import requests


class ScoringAPIClient:
    def __init__(self, base_url: str = "https://agents-course-unit4-scoring.hf.space"):
        self.base_url = base_url.rstrip("/")

    def _url(self, path: str) -> str:
        return f"{self.base_url}{path}"

    def get_questions(self) -> List[Dict[str, Any]]:
        resp = requests.get(self._url("/questions"))
        resp.raise_for_status()
        return resp.json()

    def get_random_question(self) -> Dict[str, Any]:
        resp = requests.get(self._url("/random-question"))
        resp.raise_for_status()
        return resp.json()

    def download_file(self, task_id: int, dest_path: str) -> None:
        resp = requests.get(self._url(f"/files/{task_id}"), stream=True)
        resp.raise_for_status()
        with open(dest_path, "wb") as f:
            for chunk in resp.iter_content(chunk_size=8192):
                if chunk:
                    f.write(chunk)

    def submit(self, username: str, agent_code: str, answers: List[Dict[str, Any]]) -> Dict[str, Any]:
        payload = {"username": username, "agent_code": agent_code, "answers": answers}
        resp = requests.post(self._url("/submit"), json=payload)
        resp.raise_for_status()
        return resp.json()


if __name__ == "__main__":
    c = ScoringAPIClient()
    qs = c.get_questions()
    print(f"Loaded {len(qs)} questions")