|
| 1 | +# managers/feedback.py |
| 2 | + |
| 3 | +from typing import List |
| 4 | +from flask import session |
| 5 | +from . import post_auth, server_response, URL |
| 6 | +from .logger import Logger |
| 7 | +import json |
| 8 | + |
| 9 | + |
| 10 | +def get_feedback(sid: str) -> List[dict]: |
| 11 | + """ |
| 12 | + Fetches feedback for a given session ID by calling the internal |
| 13 | + /data/session/select/feedback endpoint, then returns a list of |
| 14 | + { "question": ..., "answer": ... } dictionaries. |
| 15 | + """ |
| 16 | + feedback_list: List[dict] = [] |
| 17 | + try: |
| 18 | + # Build request body |
| 19 | + body = {"sessionId": sid} |
| 20 | + |
| 21 | + # Perform authenticated POST to the internal endpoint |
| 22 | + token = session.get('token', '') |
| 23 | + headers = { |
| 24 | + "Authorization": f"Bearer {token}", |
| 25 | + "Content-Type": "application/json" |
| 26 | + } |
| 27 | + response = post_auth(f"{URL}/data/session/select/feedback", json=body, headers=headers) |
| 28 | + |
| 29 | + # Only proceed if we got a 200 or 201 |
| 30 | + if response.status_code in (200, 201): |
| 31 | + ser_res = server_response(response) |
| 32 | + if ser_res.get_success(): |
| 33 | + raw_payload = ser_res.get_payload() or [] |
| 34 | + for item in raw_payload: |
| 35 | + try: |
| 36 | + # If payload items are JSON‐strings, parse them |
| 37 | + feedback_list.append(json.loads(item)) |
| 38 | + except Exception: |
| 39 | + # If already a dict, just append |
| 40 | + if isinstance(item, dict): |
| 41 | + feedback_list.append(item) |
| 42 | + return feedback_list |
| 43 | + |
| 44 | + Logger.log_error(f"get_feedback – API returned error: {ser_res.get_message()}") |
| 45 | + return [] |
| 46 | + |
| 47 | + Logger.log_error(f"get_feedback – HTTP {response.status_code}") |
| 48 | + return [] |
| 49 | + |
| 50 | + except Exception as e: |
| 51 | + Logger.log_error(f"get_feedback – Exception: {e}") |
| 52 | + return [] |
| 53 | + |
| 54 | + |
| 55 | +def get_experiment_feedback(exp_id: str) -> List[dict]: |
| 56 | + """ |
| 57 | + Fetch all feedback for experiment (= lobby) with ID = exp_id. |
| 58 | + Calls internal /data/experiment/select/feedback endpoint. |
| 59 | + Returns a list of dictionaries like { "pid": ..., "question": ..., "answer": ... }. |
| 60 | + """ |
| 61 | + feedback_list: List[dict] = [] |
| 62 | + try: |
| 63 | + # Build request body |
| 64 | + body = {"expId": exp_id} |
| 65 | + |
| 66 | + # Perform authenticated POST to the internal endpoint |
| 67 | + token = session.get('token', '') |
| 68 | + headers = { |
| 69 | + "Authorization": f"Bearer {token}", |
| 70 | + "Content-Type": "application/json" |
| 71 | + } |
| 72 | + response = post_auth(f"{URL}/data/experiment/select/feedback", json=body, headers=headers) |
| 73 | + |
| 74 | + # Check HTTP status |
| 75 | + if response.status_code in (200, 201): |
| 76 | + ser_res = server_response(response) |
| 77 | + if ser_res.get_success(): |
| 78 | + raw_payload = ser_res.get_payload() or [] |
| 79 | + for item in raw_payload: |
| 80 | + try: |
| 81 | + # If payload items are JSON‐strings, parse them |
| 82 | + feedback_list.append(json.loads(item)) |
| 83 | + except Exception: |
| 84 | + # If already a dict, just append |
| 85 | + if isinstance(item, dict): |
| 86 | + feedback_list.append(item) |
| 87 | + return feedback_list |
| 88 | + |
| 89 | + Logger.log_error(f"get_experiment_feedback – API error: {ser_res.get_message()}") |
| 90 | + return [] |
| 91 | + else: |
| 92 | + Logger.log_error(f"get_experiment_feedback – HTTP {response.status_code}") |
| 93 | + return [] |
| 94 | + |
| 95 | + except Exception as e: |
| 96 | + Logger.log_error(f"get_experiment_feedback – Exception: {e}") |
| 97 | + return [] |
| 98 | + |
0 commit comments