Skip to content

Add option to append to user agent #3

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

Merged
merged 2 commits into from
Apr 25, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
needs: [ install_dependencies ]
strategy:
matrix:
php-versions: [ "7.4", "8.0", "8.1", "8.2" ]
php-versions: [ "7.4", "8.0", "8.1", "8.2", "8.3" ]

runs-on: ubuntu-latest

Expand Down Expand Up @@ -69,4 +69,4 @@ jobs:
run: composer install --prefer-dist --no-progress

- name: Run test suite
run: composer run-script test
run: composer run-script test
20 changes: 18 additions & 2 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Connection

private const TOKEN_URL = '/oauth/token';

private const VERSION = '1.0.1';
private const VERSION = '1.0.2';

/** @var Client|null */
private ?Client $client = null;
Expand All @@ -59,6 +59,9 @@ class Connection
/** @var string The URL as configured with the OAuth client */
private string $redirectUrl;

/** @var string The appendix for the user agent */
private string $userAgentAppendix = '';

/** @var mixed */
private $state = null;

Expand Down Expand Up @@ -87,12 +90,14 @@ public function getClient(): Client
$userAgent .= ' OAuth/2.0';
}

$userAgent .= " {$this->userAgentAppendix}";

$this->client = new Client([
'http_errors' => true,
'expect' => false,
'base_uri' => self::BASE_URL,
'headers' => [
'User-Agent' => $userAgent,
'User-Agent' => trim($userAgent),
]
]);

Expand All @@ -107,6 +112,17 @@ public function setClient(Client $client): void
$this->client = $client;
}

/**
* @param string $userAgentAppendix
* @return Connection
*/
public function setUserAgentAppendix(string $userAgentAppendix): Connection
{
$this->userAgentAppendix = $userAgentAppendix;

return $this;
}

/**
* @param string $clientId
* @return Connection
Expand Down
12 changes: 10 additions & 2 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,23 @@ public function testUserAgentIsSet(): void
$connection = new Connection();

$this->assertEquals(
sprintf('Sendy/1.0.1 PHP/%s', phpversion()),
sprintf('Sendy/1.0.2 PHP/%s', phpversion()),
$connection->getClient()->getConfig('headers')['User-Agent']
);

$connection = new Connection();
$connection->setUserAgentAppendix('WooCommerce/6.2');

$this->assertEquals(
sprintf('Sendy/1.0.2 PHP/%s WooCommerce/6.2', phpversion()),
$connection->getClient()->getConfig('headers')['User-Agent']
);

$connection = new Connection();
$connection->setOauthClient(true);

$this->assertEquals(
sprintf('Sendy/1.0.1 PHP/%s OAuth/2.0', phpversion()),
sprintf('Sendy/1.0.2 PHP/%s OAuth/2.0', phpversion()),
$connection->getClient()->getConfig('headers')['User-Agent']
);
}
Expand Down
Loading