Skip to content

Commit 33a6776

Browse files
committed
Add start stop caption methods
1 parent 57d9bd6 commit 33a6776

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

src/OpenTok/OpenTok.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,30 @@ public function connectAudio(string $sessionId, string $token, array $websocketO
12621262
return $this->client->connectAudio($sessionId, $token, $websocketOptions);
12631263
}
12641264

1265+
public function startCaptions(
1266+
string $sessionId,
1267+
string $token,
1268+
?string $languageCode = null,
1269+
?int $maxDuration = null,
1270+
?bool $partialCaptions = null,
1271+
?string $statusCallbackUrl = null
1272+
): array
1273+
{
1274+
return $this->client->startCaptions(
1275+
$sessionId,
1276+
$token,
1277+
$languageCode,
1278+
$maxDuration,
1279+
$partialCaptions,
1280+
$statusCallbackUrl
1281+
);
1282+
}
1283+
1284+
public function stopCaptions(string $captionsId)
1285+
{
1286+
return $this->client->stopCaptions($captionsId);
1287+
}
1288+
12651289
/** @internal */
12661290
private function signString($string, $secret)
12671291
{

src/OpenTok/Util/Client.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,9 +881,75 @@ public function connectAudio(string $sessionId, string $token, array $websocketO
881881
$this->handleException($e);
882882
return false;
883883
}
884+
885+
return $jsonResponse;
886+
}
887+
888+
public function startCaptions(
889+
string $sessionId,
890+
string $token,
891+
?string $languageCode,
892+
?int $maxDuration,
893+
?bool $partialCaptions,
894+
?string $statusCallbackUrl
895+
)
896+
{
897+
$request = new Request(
898+
'POST',
899+
'/v2/project/' . $this->apiKey . '/captions'
900+
);
901+
902+
$body = [
903+
'sessionId' => $sessionId,
904+
'token' => $token,
905+
];
906+
907+
if ($languageCode !== null) {
908+
$body['languageCode'] = $languageCode;
909+
}
910+
911+
if ($maxDuration !== null) {
912+
$body['maxDuration'] = $maxDuration;
913+
}
914+
915+
if ($partialCaptions !== null) {
916+
$body['partialCaptions'] = $partialCaptions;
917+
}
918+
919+
if ($statusCallbackUrl !== null) {
920+
$body['statusCallbackUrl'] = $statusCallbackUrl;
921+
}
922+
923+
try {
924+
$response = $this->client->send($request, [
925+
'debug' => $this->isDebug(),
926+
'json' => $body
927+
]);
928+
$jsonResponse = json_decode($response->getBody(), true);
929+
} catch (\Exception $e) {
930+
$this->handleException($e);
931+
}
932+
884933
return $jsonResponse;
885934
}
886935

936+
public function stopCaptions(string $captionsId)
937+
{
938+
$request = new Request(
939+
'POST',
940+
'/v2/project/' . $this->apiKey . '/captions/' . $captionsId . '/stop'
941+
);
942+
943+
try {
944+
$this->client->send($request, [
945+
'debug' => $this->isDebug(),
946+
]);
947+
return true;
948+
} catch (\Exception $e) {
949+
$this->handleException($e);
950+
}
951+
}
952+
887953
private function handleException($e)
888954
{
889955
// TODO: test coverage

tests/OpenTokTest/OpenTokTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2836,5 +2836,33 @@ public function testDefaultTimeoutErrorsIfLessThanZero(): void
28362836
$this->expectExceptionMessage('Default Timeout must be a number greater than zero');
28372837
new OpenTok('1234', 'abd', ['timeout' => -1]);
28382838
}
2839+
2840+
public function testCanStartCaptions(): void
2841+
{
2842+
$this->setupOTWithMocks([[
2843+
'code' => 202,
2844+
'headers' => [
2845+
'Content-Type' => 'application/json'
2846+
],
2847+
'path' => '/v2/project/APIKEY/session/SESSIONID/caption-start'
2848+
]]);
2849+
2850+
$result = $this->opentok->startCaptions('SESSION_ID', 'abc');
2851+
$this->assertEquals('7c0680fc-6274-4de5-a66f-d0648e8d3ac2', $result['captionsId']);
2852+
}
2853+
2854+
public function testCanStopCaptions(): void
2855+
{
2856+
$this->setupOTWithMocks([[
2857+
'code' => 202,
2858+
'headers' => [
2859+
'Content-Type' => 'application/json'
2860+
],
2861+
'path' => '/v2/project/APIKEY/session/SESSIONID/caption-stop'
2862+
]]);
2863+
2864+
$result = $this->opentok->stopCaptions('7c0680fc-6274-4de5-a66f-d0648e8d3ac2');
2865+
$this->assertTrue($result);
2866+
}
28392867
}
28402868

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"captionsId": "7c0680fc-6274-4de5-a66f-d0648e8d3ac2"
3+
}

tests/mock/v2/project/APIKEY/session/SESSIONID/caption-stop

Whitespace-only changes.

0 commit comments

Comments
 (0)