Skip to content

Commit

Permalink
Merge pull request #331 from opentok/dev
Browse files Browse the repository at this point in the history
Dev release merge
  • Loading branch information
SecondeJK committed May 10, 2023
2 parents db655f8 + 66425a8 commit 17b535e
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 264 deletions.
10 changes: 7 additions & 3 deletions src/OpenTok/OpenTok.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class OpenTok
private $apiSecret;
/** @internal */
private $client;
/** @internal */
public $options;

/** @internal */
public function __construct($apiKey, $apiSecret, $options = array())
Expand All @@ -36,8 +38,10 @@ public function __construct($apiKey, $apiSecret, $options = array())
'client' => null,
'timeout' => null // In the future we should set this to 2
);
$options = array_merge($defaults, array_intersect_key($options, $defaults));
list($apiUrl, $client, $timeout) = array_values($options);

$this->options = array_merge($defaults, array_intersect_key($options, $defaults));

list($apiUrl, $client, $timeout) = array_values($this->options);

// validate arguments
Validators::validateApiKey($apiKey);
Expand All @@ -52,7 +56,7 @@ public function __construct($apiKey, $apiSecret, $options = array())
$apiKey,
$apiSecret,
$apiUrl,
['timeout' => $timeout]
array_merge(['timeout' => $timeout], $this->options)
);
}
$this->apiKey = $apiKey;
Expand Down
47 changes: 35 additions & 12 deletions src/OpenTok/Util/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace OpenTok\Util;

use Composer\InstalledVersions;
use Exception as GlobalException;
use GuzzleHttp\Utils;
use OpenTok\Layout;
use Firebase\JWT\JWT;
use OpenTok\MediaMode;
Expand All @@ -18,7 +20,6 @@
use GuzzleHttp\Exception\ServerException;
use OpenTok\Exception\BroadcastException;
use GuzzleHttp\Exception\RequestException;
use function GuzzleHttp\default_user_agent;
use OpenTok\Exception\ArchiveDomainException;
use OpenTok\Exception\AuthenticationException;
use OpenTok\Exception\BroadcastDomainException;
Expand All @@ -36,17 +37,13 @@
use OpenTok\Exception\ForceDisconnectAuthenticationException;
use OpenTok\Exception\ForceDisconnectUnexpectedValueException;

// TODO: build this dynamically
/** @internal */
define('OPENTOK_SDK_VERSION', '4.12.0');
/** @internal */
define('OPENTOK_SDK_USER_AGENT', 'OpenTok-PHP-SDK/' . OPENTOK_SDK_VERSION);

/**
* @internal
*/
* @internal
*/
class Client
{
public const OPENTOK_SDK_USER_AGENT_IDENTIFIER = 'OpenTok-PHP-SDK/';

protected $apiKey;
protected $apiSecret;
protected $configured = false;
Expand All @@ -56,18 +53,24 @@ class Client
*/
protected $client;

/**
* @var array|mixed
*/
public $options;

public function configure($apiKey, $apiSecret, $apiUrl, $options = array())
{
$this->options = $options;
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;

if (isset($options['client'])) {
if (isset($this->options['client'])) {
$this->client = $options['client'];
} else {
$clientOptions = [
'base_uri' => $apiUrl,
'headers' => [
'User-Agent' => OPENTOK_SDK_USER_AGENT . ' ' . default_user_agent(),
'User-Agent' => $this->buildUserAgentString()
],
];

Expand All @@ -94,6 +97,26 @@ public function configure($apiKey, $apiSecret, $apiUrl, $options = array())
$this->configured = true;
}

private function buildUserAgentString(): string
{
$userAgent = [];

$userAgent[] = self::OPENTOK_SDK_USER_AGENT_IDENTIFIER
. InstalledVersions::getVersion('opentok/opentok');

$userAgent[] = 'php/' . PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION;

if (isset($this->options['app'])) {
$app = $this->options['app'];
if (isset($app['name'], $app['version'])) {
// You must use both of these for custom agent strings
$userAgent[] = $app['name'] . '/' . $app['version'];
}
}

return implode(' ', $userAgent);
}

public function isConfigured()
{
return $this->configured;
Expand Down Expand Up @@ -939,7 +962,7 @@ private function handleSignalingException(ClientException $e)
throw new SignalConnectionException($message, $responseCode);
case 413:
$message = 'The type string exceeds the maximum length (128 bytes),'
. ' or the data string exceeds the maximum size (8 kB).';
. ' or the data string exceeds the maximum size (8 kB).';
throw new SignalUnexpectedValueException($message, $responseCode);
default:
break;
Expand Down
10 changes: 0 additions & 10 deletions tests/OpenTokTest/ArchiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,6 @@ public function testStopsArchive()
$authString = $request->getHeaderLine('X-OPENTOK-AUTH');
$this->assertEquals(true, TestHelpers::validateOpenTokAuthHeader($this->API_KEY, $this->API_SECRET, $authString));

// TODO: test the dynamically built User Agent string
$userAgent = $request->getHeaderLine('User-Agent');
$this->assertNotEmpty($userAgent);
$this->assertStringStartsWith('OpenTok-PHP-SDK/4.12.0', $userAgent);

// TODO: test the properties of the actual archive object
$this->assertEquals('stopped', $this->archive->status);

Expand Down Expand Up @@ -277,11 +272,6 @@ public function testDeletesArchive()
$authString = $request->getHeaderLine('X-OPENTOK-AUTH');
$this->assertEquals(true, TestHelpers::validateOpenTokAuthHeader($this->API_KEY, $this->API_SECRET, $authString));

// TODO: test the dynamically built User Agent string
$userAgent = $request->getHeaderLine('User-Agent');
$this->assertNotEmpty($userAgent);
$this->assertStringStartsWith('OpenTok-PHP-SDK/4.12.0', $userAgent);

$this->assertTrue($success);
// TODO: assert that all properties of the archive object were cleared
}
Expand Down

0 comments on commit 17b535e

Please sign in to comment.