. /** * 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 string This variable holds a domain or IP to attached flask ML backend */ private string $endpoint; /** @var http_client_interface This variable holds an interface for http_client */ private http_client_interface $httpclient; /** @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. */ private function __construct(string $endpoint, http_client_interface $httpclient) { $this->endpoint = $endpoint; $this->httpclient = $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()); } } }