MDL-83376 AI: Add is_request_allowed() to the base provider

This commit is contained in:
meirzamoodle 2024-10-10 07:58:57 +07:00
parent 09880d7d78
commit 60fb2c1232
4 changed files with 28 additions and 12 deletions

View File

@ -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.
*

View File

@ -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));

View File

@ -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));

View File

@ -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));
}
}