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

Send client version header #19

Merged
merged 1 commit into from
Nov 22, 2017
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
12 changes: 12 additions & 0 deletions src/Http/RequestManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace Lmc\Matej\Http;

use Http\Client\Common\Plugin\AuthenticationPlugin;
use Http\Client\Common\Plugin\HeaderSetPlugin;
use Http\Client\Common\PluginClient;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\MessageFactory;
use Lmc\Matej\Http\Plugin\ExceptionPlugin;
use Lmc\Matej\Matej;
use Lmc\Matej\Model\Request;
use Lmc\Matej\Model\Response;
use Psr\Http\Message\RequestInterface;
Expand All @@ -19,6 +21,8 @@
*/
class RequestManager
{
public const CLIENT_VERSION_HEADER = 'Matej-Client-Version';

/** @var string */
protected $accountId;
/** @var string */
Expand Down Expand Up @@ -99,6 +103,7 @@ protected function createConfiguredHttpClient(): HttpClient
return new PluginClient(
$this->getHttpClient(),
[
new HeaderSetPlugin($this->getDefaultHeaders()),
new AuthenticationPlugin(new HmacAuthentication($this->apiKey)),
new ExceptionPlugin(),
]
Expand All @@ -123,4 +128,11 @@ protected function buildBaseUrl(): string
{
return sprintf('https://%s.matej.lmc.cz', $this->accountId);
}

private function getDefaultHeaders(): array
{
return [
self::CLIENT_VERSION_HEADER => Matej::CLIENT_ID . '/' . Matej::VERSION,
];
}
}
3 changes: 3 additions & 0 deletions src/Matej.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

class Matej
{
public const CLIENT_ID = 'php-client';
public const VERSION = '0.0.0';

/** @var string */
private $clientId;
/** @var string */
Expand Down
5 changes: 5 additions & 0 deletions tests/Http/RequestManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Fig\Http\Message\RequestMethodInterface;
use Http\Mock\Client;
use Lmc\Matej\Matej;
use Lmc\Matej\Model\Request;
use Lmc\Matej\Model\Response;
use Lmc\Matej\TestCase;
Expand Down Expand Up @@ -53,5 +54,9 @@ public function shouldSendAndDecodeRequest(): void
$recordedRequests[0]->getBody()->__toString()
);
$this->assertSame(['application/json'], $recordedRequests[0]->getHeader('Content-Type'));
$this->assertSame(
'php-client/' . Matej::VERSION,
$recordedRequests[0]->getHeader(RequestManager::CLIENT_VERSION_HEADER)[0]
);
}
}