. /** * This is an api endpoint that sends a request to a local container with a flask-based server. * The plugin uses the ASYST grading tool * modified to work as a web endpoint. * * @package local_asystgrade * @copyright 2024 Artem Baranovskyi * @copyright based on work by 2023 Ulrike Pado , * @copyright Yunus Eryilmaz & Larissa Kirschner * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once($_SERVER['DOCUMENT_ROOT'] . '/config.php'); require_once('lib.php'); use local_asystgrade\api\client; use local_asystgrade\api\http_client; use local_asystgrade\utils; try { require_login(); } catch (coding_exception | moodle_exception $e) { debugging($e->getMessage()); redirect( new moodle_url('/local/asystgrade/error.php'), get_string('loginerror', 'local_asystgrade') ); } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $data = json_decode(file_get_contents('php://input'), true); if ($data) { // Preparing Flask API. try { $apiendpoint = utils::get_api_endpoint(); } catch (dml_exception $e) { debugging('Failed to get API endpoint setting: ' . $e->getMessage()); } $httpclient = new http_client(); $apiclient = client::getInstance($apiendpoint, $httpclient); $maxretries = 3; $attempts = 0; $success = false; while ($attempts < $maxretries && !$success) { try { // Sending data to Flask and obtaining an answer. $response = $apiclient->send_data($data); $success = true; } catch (Exception $e) { $attempts++; debugging('API request error: ' . $e->getMessage()); if ($attempts >= $maxretries) { echo json_encode(['error' => 'A server error occurred. Please try again later.']); exit; // Ensure to stop further processing. } } } if ($success) { $grades = json_decode($response, true); // Check JSON validity. if (json_last_error() === JSON_ERROR_NONE) { echo json_encode(['success' => true, 'grades' => $grades]); } else { debugging('JSON decode error: ' . json_last_error_msg()); echo json_encode(['error' => 'Invalid JSON from Flask API']); } } } else { echo json_encode(['error' => 'No data received']); } } else { echo json_encode(['error' => 'Invalid request method']); }