-
Notifications
You must be signed in to change notification settings - Fork 50
Adds built-in support for basic PSR7 and 17 support #208
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
Merged
+496
−4
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
95 changes: 95 additions & 0 deletions
95
src/Providers/Http/Abstracts/AbstractClientDiscoveryStrategy.php
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,95 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace WordPress\AiClient\Providers\Http\Abstracts; | ||
|
|
||
| use Http\Discovery\Psr18ClientDiscovery; | ||
| use Http\Discovery\Strategy\DiscoveryStrategy; | ||
| use Nyholm\Psr7\Factory\Psr17Factory; | ||
| use Psr\Http\Client\ClientInterface; | ||
|
|
||
| /** | ||
| * Abstract discovery strategy for HTTP client implementations. | ||
| * | ||
| * Provides a base for registering custom HTTP client implementations | ||
| * with HTTPlug's discovery mechanism. Subclasses must implement | ||
| * the createClient() method to provide their specific PSR-18 | ||
| * HTTP client instance using the provided Psr17Factory. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| abstract class AbstractClientDiscoveryStrategy implements DiscoveryStrategy | ||
| { | ||
| /** | ||
| * Initializes and registers the discovery strategy. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return void | ||
| */ | ||
| public static function init(): void | ||
| { | ||
| if (!class_exists('\Http\Discovery\Psr18ClientDiscovery')) { | ||
| return; | ||
| } | ||
|
|
||
| Psr18ClientDiscovery::prependStrategy(static::class); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param string $type The type of discovery. | ||
| * @return array<array<string, mixed>> The discovery candidates. | ||
| */ | ||
| public static function getCandidates($type) | ||
| { | ||
| if (ClientInterface::class === $type) { | ||
| return [ | ||
| [ | ||
| 'class' => static function () { | ||
| $psr17Factory = new Psr17Factory(); | ||
| return static::createClient($psr17Factory); | ||
| }, | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| $psr17Factories = [ | ||
| 'Psr\Http\Message\RequestFactoryInterface', | ||
| 'Psr\Http\Message\ResponseFactoryInterface', | ||
| 'Psr\Http\Message\ServerRequestFactoryInterface', | ||
| 'Psr\Http\Message\StreamFactoryInterface', | ||
| 'Psr\Http\Message\UploadedFileFactoryInterface', | ||
| 'Psr\Http\Message\UriFactoryInterface', | ||
| ]; | ||
|
|
||
| if (in_array($type, $psr17Factories, true)) { | ||
| return [ | ||
| [ | ||
| 'class' => Psr17Factory::class, | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| /** | ||
| * Creates an instance of the HTTP client. | ||
| * | ||
| * Subclasses must implement this method to return their specific | ||
| * PSR-18 HTTP client instance. The provided Psr17Factory implements | ||
| * all PSR-17 interfaces (RequestFactory, ResponseFactory, StreamFactory, | ||
| * etc.) and can be used to satisfy client constructor dependencies. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param Psr17Factory $psr17Factory The PSR-17 factory for creating HTTP messages. | ||
| * @return ClientInterface The PSR-18 HTTP client. | ||
| */ | ||
| abstract protected static function createClient(Psr17Factory $psr17Factory): ClientInterface; | ||
| } |
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.
Super nit-pick, but since you're sorting this differently now, can you make it alphabetically?
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.
They are. 😄