Commit 95948a42 authored by Artem Baranovskyi's avatar Artem Baranovskyi
Browse files

Merge branch 'issue-2-use-moodle-curl-wrapper' into asyst-moodle-plugin

Showing with 25 additions and 39 deletions
+25 -39
......@@ -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
) {
}
......
......@@ -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;
......
......@@ -4,4 +4,4 @@ matplotlib
scikit-learn
transformers
pandas
sentence_transformers
\ No newline at end of file
sentence_transformers
......@@ -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';
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment