Skip to content

Commit

Permalink
Merge pull request #3 from merch-one/#3
Browse files Browse the repository at this point in the history
Added X-Library header
  • Loading branch information
merch-one committed Apr 14, 2023
2 parents 2d5e24d + c8ced97 commit b152f7b
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"type": "library",
"license": "MIT",
"version": "1.0.3",
"version": "1.0.4",
"require": {
"php": ">=7.4",
"guzzlehttp/guzzle": "^6|^7",
Expand Down
22 changes: 21 additions & 1 deletion src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class Client implements HttpClient
*/
protected array $clientOptions = [
'headers' => [
'User-Agent' => 'MerchOne PHP SDK v1.0.2',
'User-Agent' => null,
'X-Library' => null,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
Expand All @@ -60,6 +61,7 @@ public function __construct(
) {
$this->apiVersion = $version;
$this->setClientOptions($clientOptions);
$this->setClientHeaders();

$this->buildClient();
}
Expand Down Expand Up @@ -234,4 +236,22 @@ private function buildClient(): void

$this->httpClient = new GuzzleClient($this->clientOptions);
}

/**
* @return void
*/
private function setClientHeaders(): void
{
$this->clientOptions['headers']['X-Library'] = MerchOneApi::getSdkVersion();

$userAgent = new Collection([
'host' => $_SERVER['HTTP_HOST'] ?? 'unknown',
'software' => $_SERVER['SERVER_SOFTWARE'] ?? 'unknown',
'php' => PHP_VERSION,
]);

$this->clientOptions['headers']['User-Agent'] = $userAgent->map(
static fn ($value, $key) => "{$key}@{$value}"
)->implode(' ');
}
}
8 changes: 8 additions & 0 deletions src/Util/MerchOneApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public static function getVersions(): array
)->values()->toArray();
}

/**
* @return string
*/
public static function getSdkVersion(): string
{
return (new PackageInfo())->getVersionName();
}

/**
* @param string $version
* @return void
Expand Down
77 changes: 77 additions & 0 deletions src/Util/PackageInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace MerchOne\PhpApiSdk\Util;

use Composer\InstalledVersions;

final class PackageInfo
{
/**
* @var string
*/
private string $name = 'PHP';

/**
* @var string
*/
private string $version = 'unknown';

public function __construct()
{
$this->boot();
}

/**
* @return string
*/
public function getVersionName(): string
{
return str_replace('merch-one/', '', $this->name) . '/' . $this->version;
}

/**
* @return void
*/
private function boot(): void
{
$name = $this->name;

try {
$this->name = $this->currentPackage();
$this->version = InstalledVersions::getPrettyVersion($this->name);
} catch (\Throwable $e) {
$this->name = $name;
$this->version = $this->getVersionFromComposerJson();
}
}

/**
* @return string
* @noinspection JsonEncodingApiUsageInspection
*/
private function getVersionFromComposerJson(): string
{
try {
$json = json_decode(
file_get_contents(__DIR__ . '/../../composer.json'),
true
);

return $json['version'];
} catch (\Throwable $e) {
return 'unknown';
}
}

/**
* @return string
*/
private function currentPackage(): string
{
if (InstalledVersions::isInstalled('merch-one/laravel-api-sdk')) {
return 'merch-one/laravel-api-sdk';
}

return 'merch-one/php-api-sdk';
}
}
9 changes: 8 additions & 1 deletion tests/Unit/Http/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use MerchOne\PhpApiSdk\Exceptions\InvalidApiVersionException;
use MerchOne\PhpApiSdk\Http\Client;
use MerchOne\PhpApiSdk\Tests\TestCase;
use MerchOne\PhpApiSdk\Util\MerchOneApi;
use ReflectionException;

class ClientTest extends TestCase
Expand Down Expand Up @@ -109,19 +110,25 @@ public function testDefaultClientOptionsAreImmutable(): void
'user-agent' => 'test-user-agent',
'accept' => 'test-accept',
'content-type' => 'test-content-type',
'x-library' => 'test-library',
],
'http_errors' => true,
]);

$options = $this->getObjectProperty($client, 'clientOptions');

$this->assertArrayHasKey('headers', $options);
$this->assertEquals(MerchOneApi::getSdkVersion(), $options['headers']['X-Library']);

$this->assertArrayHasKey('User-Agent', $options['headers']);
$this->assertEquals('MerchOne PHP SDK v1.0.2', $options['headers']['User-Agent']);
$this->assertStringContainsString('php@' . PHP_VERSION, $options['headers']['User-Agent']);

$this->assertEquals('application/json', $options['headers']['Accept']);
$this->assertEquals('application/json', $options['headers']['Content-Type']);

$this->assertArrayHasKey('http_errors', $options);
$this->assertFalse($options['http_errors']);

$this->assertArrayHasKey('X-Test', $options['headers']);
$this->assertEquals('test-header', $options['headers']['X-Test']);
}
Expand Down

0 comments on commit b152f7b

Please sign in to comment.