mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 20:42:22 +02:00
MDL-83376 AI: Add is_request_allowed() to the base provider
This commit is contained in:
parent
09880d7d78
commit
60fb2c1232
@ -72,6 +72,14 @@ abstract class provider {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the request is allowed by the rate limiter.
|
||||
*
|
||||
* @param aiactions\base $action The action to check.
|
||||
* @return array|bool True on success, array of error details on failure.
|
||||
*/
|
||||
abstract public function is_request_allowed(aiactions\base $action): array|bool;
|
||||
|
||||
/**
|
||||
* Check if a provider has the minimal configuration to work.
|
||||
*
|
||||
|
@ -100,12 +100,7 @@ class provider extends \core_ai\provider {
|
||||
->withAddedHeader('api-key', $this->apikey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the request is allowed by the rate limiter.
|
||||
*
|
||||
* @param aiactions\base $action The action to check.
|
||||
* @return array|bool True on success, array of error details on failure.
|
||||
*/
|
||||
#[\Override]
|
||||
public function is_request_allowed(aiactions\base $action): array|bool {
|
||||
$ratelimiter = \core\di::get(rate_limiter::class);
|
||||
$component = \core\component::get_component_from_classname(get_class($this));
|
||||
|
@ -102,12 +102,7 @@ class provider extends \core_ai\provider {
|
||||
->withAddedHeader('OpenAI-Organization', $this->orgid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the request is allowed by the rate limiter.
|
||||
*
|
||||
* @param aiactions\base $action The action to check.
|
||||
* @return array|bool True on success, array of error details on failure.
|
||||
*/
|
||||
#[\Override]
|
||||
public function is_request_allowed(aiactions\base $action): array|bool {
|
||||
$ratelimiter = \core\di::get(rate_limiter::class);
|
||||
$component = \core\component::get_component_from_classname(get_class($this));
|
||||
|
@ -1530,4 +1530,22 @@ final class provider_test extends \advanced_testcase {
|
||||
|
||||
$this->assertEquals(get_string('pluginname', 'aiprovider_openai'), $provider->get_name());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the is_request_allowed method of the provider abstract class.
|
||||
*/
|
||||
public function test_is_request_allowed(): void {
|
||||
|
||||
$stub = $this->getMockBuilder(\core_ai\provider::class)
|
||||
->onlyMethods(['is_request_allowed'])
|
||||
->getMockForAbstractClass();
|
||||
|
||||
$stub->expects($this->once())
|
||||
->method('is_request_allowed')
|
||||
->willReturn(true);
|
||||
|
||||
$actionmock = $this->createMock(\core_ai\aiactions\base::class);
|
||||
|
||||
$this->assertTrue($stub->is_request_allowed($actionmock));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user