Skip to content

Commit

Permalink
Add start stop caption methods
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondeJK committed Sep 11, 2023
1 parent 57d9bd6 commit 33a6776
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/OpenTok/OpenTok.php
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,30 @@ public function connectAudio(string $sessionId, string $token, array $websocketO
return $this->client->connectAudio($sessionId, $token, $websocketOptions);
}

public function startCaptions(
string $sessionId,
string $token,
?string $languageCode = null,
?int $maxDuration = null,
?bool $partialCaptions = null,
?string $statusCallbackUrl = null
): array
{
return $this->client->startCaptions(
$sessionId,
$token,
$languageCode,
$maxDuration,
$partialCaptions,
$statusCallbackUrl
);
}

public function stopCaptions(string $captionsId)
{
return $this->client->stopCaptions($captionsId);
}

/** @internal */
private function signString($string, $secret)
{
Expand Down
66 changes: 66 additions & 0 deletions src/OpenTok/Util/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -881,9 +881,75 @@ public function connectAudio(string $sessionId, string $token, array $websocketO
$this->handleException($e);
return false;
}

return $jsonResponse;
}

public function startCaptions(
string $sessionId,
string $token,
?string $languageCode,
?int $maxDuration,
?bool $partialCaptions,
?string $statusCallbackUrl
)
{
$request = new Request(
'POST',
'/v2/project/' . $this->apiKey . '/captions'
);

$body = [
'sessionId' => $sessionId,
'token' => $token,
];

if ($languageCode !== null) {
$body['languageCode'] = $languageCode;
}

if ($maxDuration !== null) {
$body['maxDuration'] = $maxDuration;
}

if ($partialCaptions !== null) {
$body['partialCaptions'] = $partialCaptions;
}

if ($statusCallbackUrl !== null) {
$body['statusCallbackUrl'] = $statusCallbackUrl;
}

try {
$response = $this->client->send($request, [
'debug' => $this->isDebug(),
'json' => $body
]);
$jsonResponse = json_decode($response->getBody(), true);
} catch (\Exception $e) {
$this->handleException($e);
}

return $jsonResponse;
}

public function stopCaptions(string $captionsId)
{
$request = new Request(
'POST',
'/v2/project/' . $this->apiKey . '/captions/' . $captionsId . '/stop'
);

try {
$this->client->send($request, [
'debug' => $this->isDebug(),
]);
return true;
} catch (\Exception $e) {
$this->handleException($e);
}
}

private function handleException($e)
{
// TODO: test coverage
Expand Down
28 changes: 28 additions & 0 deletions tests/OpenTokTest/OpenTokTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2836,5 +2836,33 @@ public function testDefaultTimeoutErrorsIfLessThanZero(): void
$this->expectExceptionMessage('Default Timeout must be a number greater than zero');
new OpenTok('1234', 'abd', ['timeout' => -1]);
}

public function testCanStartCaptions(): void
{
$this->setupOTWithMocks([[
'code' => 202,
'headers' => [
'Content-Type' => 'application/json'
],
'path' => '/v2/project/APIKEY/session/SESSIONID/caption-start'
]]);

$result = $this->opentok->startCaptions('SESSION_ID', 'abc');
$this->assertEquals('7c0680fc-6274-4de5-a66f-d0648e8d3ac2', $result['captionsId']);
}

public function testCanStopCaptions(): void
{
$this->setupOTWithMocks([[
'code' => 202,
'headers' => [
'Content-Type' => 'application/json'
],
'path' => '/v2/project/APIKEY/session/SESSIONID/caption-stop'
]]);

$result = $this->opentok->stopCaptions('7c0680fc-6274-4de5-a66f-d0648e8d3ac2');
$this->assertTrue($result);
}
}

3 changes: 3 additions & 0 deletions tests/mock/v2/project/APIKEY/session/SESSIONID/caption-start
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"captionsId": "7c0680fc-6274-4de5-a66f-d0648e8d3ac2"
}
Empty file.

0 comments on commit 33a6776

Please sign in to comment.