From ac325600b3802aea6560e4dce0408896b8f850ee Mon Sep 17 00:00:00 2001 From: meirzamoodle Date: Thu, 10 Oct 2024 07:58:57 +0700 Subject: [PATCH] MDL-83376 AI: Add is_request_allowed() to the base provider --- ai/classes/provider.php | 8 ++++++++ ai/provider/azureai/classes/provider.php | 7 +------ ai/provider/openai/classes/provider.php | 7 +------ ai/tests/provider/provider_test.php | 18 ++++++++++++++++++ 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/ai/classes/provider.php b/ai/classes/provider.php index b53b3687e17..74b8e24802a 100644 --- a/ai/classes/provider.php +++ b/ai/classes/provider.php @@ -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. * diff --git a/ai/provider/azureai/classes/provider.php b/ai/provider/azureai/classes/provider.php index e689070da23..27c703f3531 100644 --- a/ai/provider/azureai/classes/provider.php +++ b/ai/provider/azureai/classes/provider.php @@ -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)); diff --git a/ai/provider/openai/classes/provider.php b/ai/provider/openai/classes/provider.php index d0fb901d2d3..83139fd5fa8 100644 --- a/ai/provider/openai/classes/provider.php +++ b/ai/provider/openai/classes/provider.php @@ -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)); diff --git a/ai/tests/provider/provider_test.php b/ai/tests/provider/provider_test.php index a568846bb93..a7addb09e30 100644 --- a/ai/tests/provider/provider_test.php +++ b/ai/tests/provider/provider_test.php @@ -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)); + } }