. /** * 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 Padó , * @copyright Yunus Eryilmaz & Larissa Kirschner * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace local_asystgrade\api; use curl; use Exception; /** * HTTP client class for handling HTTP POST requests. */ class http_client implements http_client_interface { /** * Sends a POST request using Moodle's curl wrapper. * * @param string $url An endpoint URL. * @param array $data The request payload. * @return bool|string * @throws Exception */ public function post(string $url, array $data): bool|string { $curl = new curl(); $options = [ 'CURLOPT_HTTPHEADER' => ['Content-Type: application/json'], ]; $response = $curl->post($url, json_encode($data), $options); $info = $curl->get_info(); if ($info['http_code'] !== 200) { debugging("API Error: HTTP {$info['http_code']} - $response"); throw new Exception("HTTP request error: {$info['http_code']}"); } return $response; } }