-
Notifications
You must be signed in to change notification settings - Fork 41
Add provider awareness of their authentication method, decoupling from the assumption of all using API keys #149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
felixarntz
wants to merge
3
commits into
trunk
Choose a base branch
from
add/provider-authentication-awareness
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+240
−72
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
8cdf39f
Add provider awareness of their authentication method, decoupling fro…
felixarntz d3ec65e
Merge branch 'trunk' into add/provider-authentication-awareness
felixarntz 8944cd8
Fix unreliable API key assumption for cloud providers, and make getIm…
felixarntz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace WordPress\AiClient\Providers\Http\Enums; | ||
|
|
||
| use WordPress\AiClient\Common\AbstractEnum; | ||
| use WordPress\AiClient\Common\Contracts\WithArrayTransformationInterface; | ||
| use WordPress\AiClient\Providers\Http\Contracts\RequestAuthenticationInterface; | ||
| use WordPress\AiClient\Providers\Http\DTO\ApiKeyRequestAuthentication; | ||
|
|
||
| /** | ||
| * Enum for request authentication methods. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @method static self apiKey() Creates an instance for API_KEY method. | ||
| * @method bool isApiKey() Checks if the method is API_KEY. | ||
| */ | ||
| class RequestAuthenticationMethod extends AbstractEnum | ||
| { | ||
| /** | ||
| * API key authentication. | ||
| */ | ||
| public const API_KEY = 'api_key'; | ||
|
|
||
| /** | ||
| * Gets the implementation class for the authentication method. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return class-string<RequestAuthenticationInterface&WithArrayTransformationInterface> The implementation class. | ||
| * | ||
| * @phpstan-ignore missingType.generics | ||
| */ | ||
| public function getImplementationClass(): string | ||
| { | ||
| // At the moment, this is the only supported method. | ||
| // Once more methods are available, add conditionals here for each method. | ||
| return ApiKeyRequestAuthentication::class; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace WordPress\AiClient\Tests\mocks; | ||
|
|
||
| use WordPress\AiClient\Providers\Contracts\ModelMetadataDirectoryInterface; | ||
| use WordPress\AiClient\Providers\Contracts\ProviderAvailabilityInterface; | ||
| use WordPress\AiClient\Providers\Contracts\ProviderInterface; | ||
| use WordPress\AiClient\Providers\DTO\ProviderMetadata; | ||
| use WordPress\AiClient\Providers\Enums\ProviderTypeEnum; | ||
| use WordPress\AiClient\Providers\Models\Contracts\ModelInterface; | ||
| use WordPress\AiClient\Providers\Models\DTO\ModelConfig; | ||
| use WordPress\AiClient\Providers\Models\DTO\ModelMetadata; | ||
|
|
||
| /** | ||
| * Mock provider that does not require authentication. | ||
| */ | ||
| class MockNoAuthProvider implements ProviderInterface | ||
| { | ||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public static function metadata(): ProviderMetadata | ||
| { | ||
| return new ProviderMetadata('no-auth', 'No Auth Provider', ProviderTypeEnum::server()); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public static function availability(): ProviderAvailabilityInterface | ||
| { | ||
| return new MockProviderAvailability(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public static function modelMetadataDirectory(): ModelMetadataDirectoryInterface | ||
| { | ||
| return new MockModelMetadataDirectory([]); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| public static function model(string $modelId, ?ModelConfig $modelConfig = null): ModelInterface | ||
| { | ||
| return new MockModel(new ModelMetadata('model', 'Model', [], []), new ModelConfig()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused by this logic. We get the authentication method enum from the provider class, then we get the authentication class derived from the enum, null if the enum is null, and then we throw an exception if the authentication class is null saying that it does not expect authentication but has an authentication method enum.
The only way this would get here would be if the
getImplementationClass()method returned null, but that's logically impossible (see my other comment).I also notice this down below, too. I guess it all hinges on whether the
getImplementationClass()return type is in fact nullable or not.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This clause is there because one shouldn't attempt to set a
RequestAuthenticationinstance for a provider that doesn't need any authentication (i.e. whereauthenticationMethodisnull).I agree with your feedback that the
getImplementationClass()return type shouldn't be nullable, so I'll update, and then the logic here has to be adjusted accordingly. But at its core the condition will still be needed - it'll instead just check whether theauthenticationMethodisnullinstead of whether theexpectedClassisnull. Makes sense?