Skip to content

Commit

Permalink
Add the ability to provide extra custom user-agents
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoocasali committed Aug 12, 2022
1 parent 43a5d1e commit 050bbc3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ public function __construct(
string $url,
string $apiKey = null,
ClientInterface $httpClient = null,
RequestFactoryInterface $requestFactory = null
RequestFactoryInterface $requestFactory = null,
array $clientAgents = []
) {
$this->http = new Http\Client($url, $apiKey, $httpClient, $requestFactory);
$this->http = new Http\Client($url, $apiKey, $httpClient, $requestFactory, $clientAgents);
$this->index = new Indexes($this->http);
$this->health = new Health($this->http);
$this->version = new Version($this->http);
Expand Down
5 changes: 3 additions & 2 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ public function __construct(
string $url,
string $apiKey = null,
ClientInterface $httpClient = null,
RequestFactoryInterface $reqFactory = null
RequestFactoryInterface $reqFactory = null,
array $clientAgents = []
) {
$this->baseUrl = $url;
$this->apiKey = $apiKey;
$this->http = $httpClient ?? Psr18ClientDiscovery::find();
$this->requestFactory = $reqFactory ?? Psr17FactoryDiscovery::findRequestFactory();
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
$this->headers = array_filter([
'User-Agent' => MeiliSearch::qualifiedVersion(),
'User-Agent' => implode(';', array_merge($clientAgents, [MeiliSearch::qualifiedVersion()])),
]);
if (null != $this->apiKey) {
$this->headers['Authorization'] = sprintf('Bearer %s', $this->apiKey);
Expand Down
30 changes: 30 additions & 0 deletions tests/Http/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,36 @@ public function testClientHasDefaultUserAgent(): void
$client->health();
}

public function testClientHasCustomUserAgent(): void
{
$customAgent = 'Meilisearch Symfony (v0.10.0)';
$httpClient = $this->createHttpClientMock(200, '{}');
$reqFactory = $this->createMock(RequestFactoryInterface::class);
$requestStub = $this->createMock(RequestInterface::class);

$requestStub->expects($this->any())
->method('withAddedHeader')
->withConsecutive(
[
$this->equalTo('User-Agent'),
$this->equalTo($customAgent.';'.MeiliSearch::qualifiedVersion()),
],
[
$this->equalTo('Authorization'),
$this->equalTo('Bearer masterKey'),
],
)
->willReturnOnConsecutiveCalls($requestStub, $requestStub);

$reqFactory->expects($this->any())
->method('createRequest')
->willReturn($requestStub);

$client = new \MeiliSearch\Client('http://localhost:7070', 'masterKey', $httpClient, $reqFactory, [$customAgent]);

$client->health();
}

public function testParseResponseReturnsNullForNoContent(): void
{
$response = $this->createMock(ResponseInterface::class);
Expand Down

0 comments on commit 050bbc3

Please sign in to comment.