import re import requests # (Keep Constants as is) # --- Constants --- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" def get_question() : questions_url = f"{DEFAULT_API_URL}/questions" print(f"Fetching questions from: {questions_url}") try: response = requests.get(questions_url, timeout=15) response.raise_for_status() questions_data = response.json() if not questions_data: print("Fetched questions list is empty.") return "Fetched questions list is empty or invalid format.", None print(f"Fetched {len(questions_data)} questions.") return questions_data except requests.exceptions.RequestException as e: print(f"Error fetching questions: {e}") return f"Error fetching questions: {e}", None except requests.exceptions.JSONDecodeError as e: print(f"Error decoding JSON response from questions endpoint: {e}") print(f"Response text: {response.text[:500]}") return f"Error decoding server response for questions: {e}", None except Exception as e: print(f"An unexpected error occurred fetching questions: {e}") return f"An unexpected error occurred fetching questions: {e}", None def get_random_question(): random_question_url = f"{DEFAULT_API_URL}/random-question" print(f"Fetching a random question from: {random_question_url}") try: response = requests.get(random_question_url, timeout=15) response.raise_for_status() questions_data = response.json() if not questions_data: print("Fetched questions list is empty.") return "Fetched questions list is empty or invalid format.", None return questions_data except requests.exceptions.RequestException as e: print(f"Error fetching a random question: {e}") return f"Error fetching a random question: {e}", None except requests.exceptions.JSONDecodeError as e: print(f"Error decoding JSON response from a random question endpoint: {e}") print(f"Response text: {response.text[:500]}") return f"Error decoding server response for a random question: {e}", None except Exception as e: print(f"An unexpected error occurred fetching a random question: {e}") return f"An unexpected error occurred fetching a random question: {e}", None def get_file_by_task_id(task_id:str): file_by_task_id_url = f"{DEFAULT_API_URL}/files/{task_id}" print(f"Fetching a file from: {file_by_task_id_url}") try: response = requests.get(file_by_task_id_url, timeout=15) response.raise_for_status() content_disp = response.headers.get("Content-Disposition") file_name = re.findall('filename=(.+)', content_disp) questions_data = response.content if not questions_data or not file_name: print("No file to fetch.") return "No file to fetch.", None return questions_data, file_name[0].replace('"', '').strip() except requests.exceptions.RequestException as e: print(f"Error fetching a file: {e}") return f"Error fetching a file: {e}", None except requests.exceptions.JSONDecodeError as e: print(f"Error decoding JSON response from the file by task id endpoint: {e}") print(f"Response text: {response.text[:500]}") return f"Error decoding server response for the file by task id: {e}", None except Exception as e: print(f"An unexpected error occurred fetching a file: {e}") return f"An unexpected error occurred fetching a file: {e}", None