Skip to content
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

Allow to configure custom http client #359

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
</service>

<service id="meilisearch.client" class="Meilisearch\Client" public="true" lazy="true">
<argument /><!-- url -->
<argument /><!-- api key -->
<argument type="service" id="psr18.http_client" on-invalid="ignore" /><!-- http client -->
<argument type="abstract">url defined in MeilisearchExtension</argument>
<argument type="abstract">api key defined in MeilisearchExtension</argument>
<argument type="abstract">http client defined in MeilisearchExtension</argument>
<argument>null</argument><!-- request factory -->
<argument /><!-- client agents -->
<argument type="abstract">client agents defined in MeilisearchExtension</argument>
<argument>null</argument><!-- stream factory -->
</service>
<service id="search.client" alias="meilisearch.client" public="true">
Expand Down
3 changes: 3 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('serializer')
->defaultValue('serializer')
->end()
->scalarNode('http_client')
->defaultValue('psr18.http_client')
->end()
->arrayNode('indices')
->arrayPrototype()
->children()
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/MeilisearchExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Meilisearch\Bundle\Services\UnixTimestampNormalizer;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
Expand Down Expand Up @@ -50,6 +51,7 @@ public function load(array $configs, ContainerBuilder $container): void
$container->findDefinition('meilisearch.client')
->replaceArgument(0, $config['url'])
->replaceArgument(1, $config['api_key'])
->replaceArgument(2, new Reference($config['http_client'], ContainerInterface::IGNORE_ON_INVALID_REFERENCE))
->replaceArgument(4, [MeilisearchBundle::qualifiedVersion()]);

$container->findDefinition('meilisearch.service')
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static function dataTestConfigurationTree(): iterable
'batchSize' => 500,
'serializer' => 'serializer',
'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'],
'http_client' => 'psr18.http_client',
'indices' => [],
],
];
Expand All @@ -59,6 +60,7 @@ public static function dataTestConfigurationTree(): iterable
'batchSize' => 100,
'serializer' => 'serializer',
'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'],
'http_client' => 'psr18.http_client',
'indices' => [],
],
];
Expand All @@ -85,6 +87,7 @@ public static function dataTestConfigurationTree(): iterable
'batchSize' => 500,
'serializer' => 'serializer',
'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'],
'http_client' => 'psr18.http_client',
'indices' => [
0 => [
'name' => 'posts',
Expand Down Expand Up @@ -156,6 +159,7 @@ public static function dataTestConfigurationTree(): iterable
'batchSize' => 500,
'serializer' => 'serializer',
'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'],
'http_client' => 'psr18.http_client',
],
];

Expand Down Expand Up @@ -191,6 +195,7 @@ public static function dataTestConfigurationTree(): iterable
'batchSize' => 500,
'serializer' => 'serializer',
'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'],
'http_client' => 'psr18.http_client',
],
];

Expand Down Expand Up @@ -228,6 +233,7 @@ public static function dataTestConfigurationTree(): iterable
'batchSize' => 500,
'serializer' => 'serializer',
'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'],
'http_client' => 'psr18.http_client',
],
];

Expand Down Expand Up @@ -265,6 +271,25 @@ public static function dataTestConfigurationTree(): iterable
'batchSize' => 500,
'serializer' => 'serializer',
'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'],
'http_client' => 'psr18.http_client',
],
];

yield 'custom http client' => [
'inputConfig' => [
'meilisearch' => [
'http_client' => 'acme.http_client',
],
],
'expectedConfig' => [
'url' => 'http://localhost:7700',
'prefix' => null,
'indices' => [],
'nbResults' => 20,
'batchSize' => 500,
'serializer' => 'serializer',
'doctrineSubscribedEvents' => ['postPersist', 'postUpdate', 'preRemove'],
'http_client' => 'acme.http_client',
],
];
}
Expand Down
Loading