Skip to content

Commit

Permalink
Merge pull request #4 from picqer/user-agent
Browse files Browse the repository at this point in the history
Add support for setting the user agent used when making API calls
  • Loading branch information
kleiram authored Apr 20, 2020
2 parents f2e063e + 276ae6f commit dd76df2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class Client
/** @var bool */
private static $isDemoMode = false;

/** @var string|null */
private static $userAgent = null;

/**
* Set the API credentials of the client.
*
Expand Down Expand Up @@ -86,6 +89,7 @@ public static function setDemoMode(bool $enabled): void
*/
public static function request(string $method, string $uri, array $options = []): ResponseInterface
{
$options = static::addUserAgentOptions($options);
$options = static::addAuthenticationOptions($options);

return static::getHttp()->request($method, $uri, $options);
Expand All @@ -101,6 +105,16 @@ public static function setHttp(HttpInterface $http): void
static::$http = $http;
}

/**
* Set the user agent reported with API calls.
*
* @param string $userAgent
*/
public static function setUserAgent(string $userAgent): void
{
static::$userAgent = $userAgent;
}

private static function addAuthenticationOptions(array $options): array
{
if (!static::isAuthenticated() || !is_array(static::$token)) {
Expand All @@ -116,6 +130,19 @@ private static function addAuthenticationOptions(array $options): array
return $options;
}

private static function addUserAgentOptions(array $options): array
{
if (static::$userAgent === null || strlen(static::$userAgent) === 0) {
return $options;
}

$userAgent = [ 'User-Agent' => static::$userAgent ];

$options['headers'] = array_merge($options['headers'] ?? [], $userAgent);

return $options;
}

private static function getHttp(): HttpInterface
{
if (!static::$http instanceof HttpInterface) {
Expand Down
18 changes: 18 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function setup(): void
Client::setHttp($this->http->reveal());
}

public function tearDown(): void
{
Client::setUserAgent('');
}

public function testAuthenticateWithCredentials()
{
$response = Psr7\parse_response(file_get_contents(__DIR__ . '/Fixtures/http/200-token'));
Expand Down Expand Up @@ -60,4 +65,17 @@ public function testPerformHttpRequestWithOptions()

$this->assertEquals($response, Client::request('GET', 'status', [ 'query' => [ 'foo' => 'bar' ]]));
}

public function testPerformHttpRequestWithUserAgent()
{
$response = $this->prophesize(ResponseInterface::class)->reveal();

$this->http
->request('GET', 'status', [ 'headers' => [ 'User-Agent' => 'foo' ]])
->willReturn($response);

Client::setUserAgent('foo');

$this->assertEquals($response, Client::request('GET', 'status'));
}
}

0 comments on commit dd76df2

Please sign in to comment.