Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
KNIGHT
Asyst Moodle Plugin
Commits
95948a42
Commit
95948a42
authored
Dec 01, 2024
by
Artem Baranovskyi
Browse files
Merge branch 'issue-2-use-moodle-curl-wrapper' into asyst-moodle-plugin
parents
9fdc9f0b
ad85c7e7
Changes
4
Hide whitespace changes
Inline
Side-by-side
classes/api/client.php
View file @
95948a42
...
@@ -29,7 +29,6 @@ namespace local_asystgrade\api;
...
@@ -29,7 +29,6 @@ namespace local_asystgrade\api;
use
Exception
;
use
Exception
;
use
local_asystgrade\api\http_client_interface
;
use
local_asystgrade\api\http_client_interface
;
use
local_asystgrade\api\http_client_interface
;
/**
/**
* Client class for handling HTTP requests to Flask ML backend.
* Client class for handling HTTP requests to Flask ML backend.
...
@@ -39,8 +38,10 @@ use local_asystgrade\api\http_client_interface;
...
@@ -39,8 +38,10 @@ use local_asystgrade\api\http_client_interface;
* @package local_asystgrade
* @package local_asystgrade
*/
*/
class
client
{
class
client
{
/** @var ?client This variable holds an object type for http_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.
* Client class for handling HTTP requests to Flask ML backend.
...
@@ -48,7 +49,13 @@ class client {
...
@@ -48,7 +49,13 @@ class client {
* @param \local_asystgrade\api\http_client_interface $httpclient This variable holds an interface for http_client
* @param \local_asystgrade\api\http_client_interface $httpclient This variable holds an interface for http_client
*/
*/
private
function
__construct
(
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
private
http_client_interface
$httpclient
)
{
)
{
}
}
...
...
classes/api/http_client.php
View file @
95948a42
...
@@ -27,44 +27,33 @@
...
@@ -27,44 +27,33 @@
namespace
local_asystgrade\api
;
namespace
local_asystgrade\api
;
use
curl
;
use
Exception
;
use
Exception
;
/**
/**
* HTTP client class for handling HTTP POST requests.
* HTTP client class for handling HTTP POST requests.
*/
*/
class
http_client
implements
http_client_interface
{
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.
* @param array $data The request payload.
*
* @return bool|string
* @return bool|string
* @throws Exception
* @throws Exception
*/
*/
public
function
post
(
string
$url
,
array
$data
):
bool
|
string
{
public
function
post
(
string
$url
,
array
$data
):
bool
|
string
{
$ch
=
curl_init
(
$url
);
$curl
=
new
curl
();
curl_setopt
(
$ch
,
CURLOPT_RETURNTRANSFER
,
true
);
$options
=
[
curl_setopt
(
$ch
,
CURLOPT_HTTPHEADER
,
[
'Content-Type: application/json'
]);
'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
);
if
(
$statuscode
!=
200
)
{
$response
=
$curl
->
post
(
$url
,
json_encode
(
$data
),
$options
);
debugging
(
"API Error: HTTP
$statuscode
-
$response
"
);
$info
=
$curl
->
get_info
();
die
(
'Error from API. Response code '
.
$statuscode
);
}
if
(
$response
===
false
)
{
if
(
$info
[
'http_code'
]
!==
200
)
{
throw
new
Exception
(
'Error sending data to API'
);
debugging
(
"API Error: HTTP
{
$info
[
'http_code'
]
}
-
$response
"
);
throw
new
Exception
(
"HTTP request error:
{
$info
[
'http_code'
]
}
"
);
}
}
return
$response
;
return
$response
;
...
...
flask_ml_api/requirements.txt
View file @
95948a42
...
@@ -4,4 +4,4 @@ matplotlib
...
@@ -4,4 +4,4 @@ matplotlib
scikit-learn
scikit-learn
transformers
transformers
pandas
pandas
sentence_transformers
sentence_transformers
\ No newline at end of file
lib.php
View file @
95948a42
...
@@ -79,8 +79,8 @@ function local_asystgrade_before_footer(): void {
...
@@ -79,8 +79,8 @@ function local_asystgrade_before_footer(): void {
function
is_flask_backend_running
(
int
$timeout
=
3
):
bool
{
function
is_flask_backend_running
(
int
$timeout
=
3
):
bool
{
$apiendpoint
=
utils
::
get_api_endpoint
();
$apiendpoint
=
utils
::
get_api_endpoint
();
$urlcomponents
=
parse_url
(
$apiendpoint
);
$urlcomponents
=
parse_url
(
$apiendpoint
);
$host
=
$urlcomponents
[
'host'
]
??
'127.0.0.1'
;
$host
=
$urlcomponents
[
'host'
];
$port
=
$urlcomponents
[
'port'
]
??
5001
;
$port
=
$urlcomponents
[
'port'
];
$connection
=
@
fsockopen
(
$host
,
$port
,
$errno
,
$errstr
,
$timeout
);
$connection
=
@
fsockopen
(
$host
,
$port
,
$errno
,
$errstr
,
$timeout
);
if
(
is_resource
(
$connection
))
{
if
(
is_resource
(
$connection
))
{
...
@@ -92,13 +92,3 @@ function is_flask_backend_running(int $timeout = 3): bool {
...
@@ -92,13 +92,3 @@ function is_flask_backend_running(int $timeout = 3): bool {
return
false
;
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'
;
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment