From 33a677600c9b1995c876c62c80272c168a9a273d Mon Sep 17 00:00:00 2001 From: James Seconde Date: Mon, 11 Sep 2023 15:42:08 +0100 Subject: [PATCH] Add start stop caption methods --- src/OpenTok/OpenTok.php | 24 +++++++ src/OpenTok/Util/Client.php | 66 +++++++++++++++++++ tests/OpenTokTest/OpenTokTest.php | 28 ++++++++ .../APIKEY/session/SESSIONID/caption-start | 3 + .../APIKEY/session/SESSIONID/caption-stop | 0 5 files changed, 121 insertions(+) create mode 100644 tests/mock/v2/project/APIKEY/session/SESSIONID/caption-start create mode 100644 tests/mock/v2/project/APIKEY/session/SESSIONID/caption-stop diff --git a/src/OpenTok/OpenTok.php b/src/OpenTok/OpenTok.php index 6a69aed5..26e70a7d 100644 --- a/src/OpenTok/OpenTok.php +++ b/src/OpenTok/OpenTok.php @@ -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) { diff --git a/src/OpenTok/Util/Client.php b/src/OpenTok/Util/Client.php index 9212c52e..76606ec1 100755 --- a/src/OpenTok/Util/Client.php +++ b/src/OpenTok/Util/Client.php @@ -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 diff --git a/tests/OpenTokTest/OpenTokTest.php b/tests/OpenTokTest/OpenTokTest.php index c24d77a0..4dbacfe2 100644 --- a/tests/OpenTokTest/OpenTokTest.php +++ b/tests/OpenTokTest/OpenTokTest.php @@ -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); + } } diff --git a/tests/mock/v2/project/APIKEY/session/SESSIONID/caption-start b/tests/mock/v2/project/APIKEY/session/SESSIONID/caption-start new file mode 100644 index 00000000..cfca81f9 --- /dev/null +++ b/tests/mock/v2/project/APIKEY/session/SESSIONID/caption-start @@ -0,0 +1,3 @@ +{ + "captionsId": "7c0680fc-6274-4de5-a66f-d0648e8d3ac2" +} \ No newline at end of file diff --git a/tests/mock/v2/project/APIKEY/session/SESSIONID/caption-stop b/tests/mock/v2/project/APIKEY/session/SESSIONID/caption-stop new file mode 100644 index 00000000..e69de29b