. /** * 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 Exception; use local_asystgrade\api\http_client_interface; /** * Client class for handling HTTP requests to Flask ML backend. * * This class provides methods for sending data to a specified endpoint using * an HTTP client interface. * @package local_asystgrade */ class client { /** @var ?client This variable holds an object type for http_client */ private static ?client $instance = null; /** * Client class for handling HTTP requests to Flask ML backend. * @param string $endpoint This variable holds a domain or IP to attached flask ML backend * @param \local_asystgrade\api\http_client_interface $httpclient This variable holds an interface for http_client */ private function __construct( /** * @var string $endpoint This variable holds a domain or IP to attached flask ML backend */ private string $endpoint, /** * @var \local_asystgrade\api\http_client_interface $httpclient This variable holds an interface for http_client */ private http_client_interface $httpclient ) { } /** * Returns the singleton instance of the client. * * @param string $endpoint * @param http_client_interface $httpclient * @return client */ public static function getinstance(string $endpoint, http_client_interface $httpclient): client { if (self::$instance === null) { self::$instance = new client($endpoint, $httpclient); } return self::$instance; } /** * Sends data to the endpoint. * * @param array $data * @return bool|string * @throws Exception */ public function send_data(array $data): bool|string { try { return $this->httpclient->post($this->endpoint, $data); } catch (Exception $e) { throw new Exception('HTTP request error: ' . $e->getMessage()); } } }