Skip to content

Commit

Permalink
Merge pull request #3 from sendynl/add-option-to-append-to-user-agent
Browse files Browse the repository at this point in the history
Add option to append to user agent
  • Loading branch information
wgriffioen authored Apr 25, 2024
2 parents 1c059bc + 14e207a commit 4dd4769
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
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

0 comments on commit 4dd4769

Please sign in to comment.