diff --git a/classes/api/client.php b/classes/api/client.php index 423043cbe7dc8c6734aa63d857ceb07b9f7185f6..4b0f5fd055f3b8356b1900380ca8d5401e407471 100755 --- a/classes/api/client.php +++ b/classes/api/client.php @@ -29,7 +29,6 @@ namespace local_asystgrade\api; use Exception; use local_asystgrade\api\http_client_interface; -use local_asystgrade\api\http_client_interface; /** * Client class for handling HTTP requests to Flask ML backend. @@ -39,8 +38,10 @@ use local_asystgrade\api\http_client_interface; * @package local_asystgrade */ class client { + + /** @var ?client This variable holds an object type for http_client */ - private static ?client $instance = null; + private static ?client $instance = null; /** * Client class for handling HTTP requests to Flask ML backend. @@ -48,7 +49,13 @@ class client { * @param \local_asystgrade\api\http_client_interface $httpclient This variable holds an interface for http_client */ private function __construct( - private string $endpoint, + /** + * @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 ) { } diff --git a/classes/api/http_client.php b/classes/api/http_client.php index cc82fa32660d66dc8491b310bad40b43cb059f3f..fdc8e755e39c007ae9e0213dd8e2ef9607c4bb5d 100755 --- a/classes/api/http_client.php +++ b/classes/api/http_client.php @@ -27,44 +27,33 @@ 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 to the specified URL with the provided data. + * Sends a POST request using Moodle's curl wrapper. * - * @param string $url An endpoint URL. Could be an IP, a domain, or a container name, like flask. + * @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 { - $ch = curl_init($url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); - curl_setopt($ch, CURLOPT_POST, 1); - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); - $response = curl_exec($ch); - - if (curl_errno($ch)) { - throw new Exception('Curl error: ' . curl_error($ch)); - } - - $statuscode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); + $curl = new curl(); + $options = [ + 'CURLOPT_HTTPHEADER' => ['Content-Type: application/json'], + ]; - if ($statuscode != 200) { - debugging("API Error: HTTP $statuscode - $response"); - die('Error from API. Response code ' . $statuscode); - } + $response = $curl->post($url, json_encode($data), $options); + $info = $curl->get_info(); - if ($response === false) { - throw new Exception('Error sending data to API'); + if ($info['http_code'] !== 200) { + debugging("API Error: HTTP {$info['http_code']} - $response"); + throw new Exception("HTTP request error: {$info['http_code']}"); } return $response; diff --git a/flask_ml_api/requirements.txt b/flask_ml_api/requirements.txt index 4bcbfa47423a8d4e119e535a4e3fc2909a292179..e29e875b0ba8d511070694a7caf586de63bd4d91 100755 --- a/flask_ml_api/requirements.txt +++ b/flask_ml_api/requirements.txt @@ -4,4 +4,4 @@ matplotlib scikit-learn transformers pandas -sentence_transformers \ No newline at end of file +sentence_transformers diff --git a/lib.php b/lib.php index 8b478aac6a7c52ba35f80109334315a15c010f68..b27e2c982fca22b24c6f8519ce4bd593c40112b6 100755 --- a/lib.php +++ b/lib.php @@ -79,8 +79,8 @@ function local_asystgrade_before_footer(): void { function is_flask_backend_running(int $timeout = 3): bool { $apiendpoint = utils::get_api_endpoint(); $urlcomponents = parse_url($apiendpoint); - $host = $urlcomponents['host'] ?? '127.0.0.1'; - $port = $urlcomponents['port'] ?? 5001; + $host = $urlcomponents['host']; + $port = $urlcomponents['port']; $connection = @fsockopen($host, $port, $errno, $errstr, $timeout); if (is_resource($connection)) { @@ -92,13 +92,3 @@ function is_flask_backend_running(int $timeout = 3): bool { return false; } } - -/** - * Obtains URL Flask API from plugin settings. - * - * @return string URL API. - * @throws dml_exception - */ -function local_asystgrade_get_api_endpoint(): string { - return get_config('local_asystgrade', 'apiendpoint') ?? 'http://127.0.0.1:5001/api/autograde'; -}