Skip to content

Commit 0e9f669

Browse files
Merge pull request #258 from opentok/dev
v4.5.0
2 parents f787051 + 3097fab commit 0e9f669

File tree

6 files changed

+169
-56
lines changed

6 files changed

+169
-56
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@ use OpenTok\OpenTok;
5353
$opentok = new OpenTok($apiKey, $apiSecret);
5454
```
5555

56+
#### Initialization Options
57+
58+
The `OpenTok\OpenTok` object just allow for some overrides of values when special needs arise, such as
59+
needing to point to a different datacenter or change the timeout of the underlying HTTP client. For
60+
these situations, you can pass an array of additional options as the third parameter.
61+
62+
We allow the following options:
63+
* `apiUrl` - Change the domain that the SDK points to. Useful when needing to select a specific datacenter
64+
or point to a mock version of the API for testing
65+
* `client` - Custom API client that inherits from `OpenTok\Utils\Client`, useful for customizing an HTTP client
66+
* `timeout` - Change the default HTTP timeout, which defaults to forever. You can pass a number of seconds to change the timeout.
67+
68+
```php
69+
use OpenTok\OpenTok;
70+
use MyCompany\CustomOpenTokClient;
71+
72+
$options = [
73+
'apiUrl' => 'https://custom.domain.com/',
74+
'client' => new CustomOpenTokClient(),
75+
'timeout' => 10,
76+
]
77+
$opentok = new OpenTok($apiKey, $apiSecret, $options);
78+
```
79+
5680
### Creating Sessions
5781

5882
To create an OpenTok Session, use the `createSession($options)` method of the

src/OpenTok/OpenTok.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,29 @@ class OpenTok {
4141
public function __construct($apiKey, $apiSecret, $options = array())
4242
{
4343
// unpack optional arguments (merging with default values) into named variables
44-
$defaults = array('apiUrl' => 'https://api.opentok.com', 'client' => null);
44+
$defaults = array(
45+
'apiUrl' => 'https://api.opentok.com',
46+
'client' => null,
47+
'timeout' => null // In the future we should set this to 2
48+
);
4549
$options = array_merge($defaults, array_intersect_key($options, $defaults));
46-
list($apiUrl, $client) = array_values($options);
50+
list($apiUrl, $client, $timeout) = array_values($options);
4751

4852
// validate arguments
4953
Validators::validateApiKey($apiKey);
5054
Validators::validateApiSecret($apiSecret);
5155
Validators::validateApiUrl($apiUrl);
5256
Validators::validateClient($client);
57+
Validators::validateDefaultTimeout($timeout);
5358

5459
$this->client = isset($client) ? $client : new Client();
5560
if (!$this->client->isConfigured()) {
56-
$this->client->configure($apiKey, $apiSecret, $apiUrl);
61+
$this->client->configure(
62+
$apiKey,
63+
$apiSecret,
64+
$apiUrl,
65+
['timeout' => $timeout]
66+
);
5767
}
5868
$this->apiKey = $apiKey;
5969
$this->apiSecret = $apiSecret;

src/OpenTok/Util/Client.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
// TODO: build this dynamically
4040
/** @internal */
41-
define('OPENTOK_SDK_VERSION', '4.4.1');
41+
define('OPENTOK_SDK_VERSION', '4.5.0');
4242
/** @internal */
4343
define('OPENTOK_SDK_USER_AGENT', 'OpenTok-PHP-SDK/' . OPENTOK_SDK_VERSION);
4444

@@ -57,27 +57,31 @@ public function configure($apiKey, $apiSecret, $apiUrl, $options = array())
5757
$this->apiKey = $apiKey;
5858
$this->apiSecret = $apiSecret;
5959

60+
$clientOptions = [
61+
'base_uri' => $apiUrl,
62+
'headers' => [
63+
'User-Agent' => OPENTOK_SDK_USER_AGENT . ' ' . \GuzzleHttp\default_user_agent(),
64+
],
65+
];
66+
67+
if (!empty($options['timeout'])) {
68+
$clientOptions['timeout'] = $options['timeout'];
69+
}
70+
6071
if (empty($options['handler'])) {
6172
$handlerStack = HandlerStack::create();
6273
} else {
6374
$handlerStack = $options['handler'];
6475
}
76+
$clientOptions['handler'] = $handlerStack;
6577

6678
$handler = Middleware::mapRequest(function (RequestInterface $request) {
6779
$authHeader = $this->createAuthHeader();
6880
return $request->withHeader('X-OPENTOK-AUTH', $authHeader);
6981
});
7082
$handlerStack->push($handler);
7183

72-
$ua = OPENTOK_SDK_USER_AGENT . ' ' . \GuzzleHttp\default_user_agent();
73-
$this->client = new \GuzzleHttp\Client([
74-
'base_uri' => $apiUrl,
75-
'handler' => $handlerStack,
76-
'headers' => [
77-
'User-Agent' => $ua
78-
]
79-
]);
80-
84+
$this->client = new \GuzzleHttp\Client($clientOptions);
8185
$this->configured = true;
8286
}
8387

src/OpenTok/Util/Validators.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,18 @@ public static function validateLayoutClassListItem($layoutClassList)
310310
}
311311
}
312312

313+
public static function validateDefaultTimeout($timeout)
314+
{
315+
// Guzzle defaults to "null" instead of 0, so allowing that through
316+
if (is_null($timeout)) {
317+
return;
318+
}
319+
320+
if (!is_numeric($timeout) || ($timeout < 0)) {
321+
throw new InvalidArgumentException('Default Timeout must be a number greater than zero');
322+
}
323+
}
324+
313325
// Helpers
314326

315327
// credit: http://stackoverflow.com/a/173479

tests/OpenTok/ArchiveTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public function testStopsArchive()
156156
// TODO: test the dynamically built User Agent string
157157
$userAgent = $request->getHeaderLine('User-Agent');
158158
$this->assertNotEmpty($userAgent);
159-
$this->assertStringStartsWith('OpenTok-PHP-SDK/4.4.1', $userAgent);
159+
$this->assertStringStartsWith('OpenTok-PHP-SDK/4.5.0', $userAgent);
160160

161161
// TODO: test the properties of the actual archive object
162162
$this->assertEquals('stopped', $this->archive->status);
@@ -195,7 +195,7 @@ public function testDeletesArchive()
195195
// TODO: test the dynamically built User Agent string
196196
$userAgent = $request->getHeaderLine('User-Agent');
197197
$this->assertNotEmpty($userAgent);
198-
$this->assertStringStartsWith('OpenTok-PHP-SDK/4.4.1', $userAgent);
198+
$this->assertStringStartsWith('OpenTok-PHP-SDK/4.5.0', $userAgent);
199199

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

0 commit comments

Comments
 (0)