Skip to content

Commit db13f01

Browse files
authored
Merge pull request #44 from packagist/t/api-client-version-tracking
Client: add HTTP header for the api client version
2 parents 4489fb9 + 44fe882 commit db13f01

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"php-http/httplug": "^1.1 || ^2.0",
1717
"php-http/discovery": "^1.0",
1818
"php-http/client-implementation": "^1.0",
19-
"php-http/client-common": "^1.9 || ^2.0"
19+
"php-http/client-common": "^1.9 || ^2.0",
20+
"composer-runtime-api": "^2.0"
2021
},
2122
"require-dev": {
2223
"phpunit/phpunit": "^5.5 || ^7.0",

src/Client.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@ public function __construct(HttpPluginClientBuilder $httpClientBuilder = null, $
2929
{
3030
$this->httpClientBuilder = $builder = $httpClientBuilder ?: new HttpPluginClientBuilder();
3131
$privatePackagistUrl = $privatePackagistUrl ? : 'https://packagist.com';
32-
$this->responseMediator = $responseMediator ? $responseMediator : new ResponseMediator();
32+
$this->responseMediator = $responseMediator ? : new ResponseMediator();
3333

3434
$builder->addPlugin(new Plugin\AddHostPlugin(UriFactoryDiscovery::find()->createUri($privatePackagistUrl)));
3535
$builder->addPlugin(new PathPrepend('/api'));
3636
$builder->addPlugin(new Plugin\RedirectPlugin());
37-
$builder->addPlugin(new Plugin\HeaderDefaultsPlugin([
37+
$headers = [
3838
'User-Agent' => 'php-private-packagist-api (https://github.com/packagist/private-packagist-api-client)',
39-
]));
39+
];
40+
if ($apiClientVersion = $this->getApiClientVersion()) {
41+
$headers['API-CLIENT-VERSION'] = $apiClientVersion;
42+
}
43+
$builder->addPlugin(new Plugin\HeaderDefaultsPlugin($headers));
4044
$builder->addPlugin(new ExceptionThrower($this->responseMediator));
4145
}
4246

@@ -118,4 +122,13 @@ protected function getHttpClientBuilder()
118122
{
119123
return $this->httpClientBuilder;
120124
}
125+
126+
private function getApiClientVersion()
127+
{
128+
try {
129+
return \Composer\InstalledVersions::getVersion('private-packagist/api-client');
130+
} catch (\OutOfBoundsException $exception) {
131+
return null;
132+
}
133+
}
121134
}

0 commit comments

Comments
 (0)