From 8c802cc7b7e5b90ab507ed2ac9aee1970f678132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4dlich?= Date: Tue, 11 Apr 2023 17:18:43 +0200 Subject: [PATCH] CT-2342: Integrate /v1/vms/{videoManagerId}/analytics/videos endpoint to retrieve number of plays --- lib/ApiClient/AbstractApiClient.php | 19 +++++++++++ lib/Interfaces/ApiClientInterface.php | 2 ++ .../VMPro/ApiClient/Methods/GetPlaysTest.php | 32 +++++++++++++++++++ .../Resources/ApiResponse/getPlays.json | 32 +++++++++++++++++++ .../Resources/ApiResponse/getPlaysEmpty.json | 3 ++ 5 files changed, 88 insertions(+) create mode 100644 tests/MovingImage/Test/Client/VMPro/ApiClient/Methods/GetPlaysTest.php create mode 100644 tests/MovingImage/Test/Fixtures/Resources/ApiResponse/getPlays.json create mode 100644 tests/MovingImage/Test/Fixtures/Resources/ApiResponse/getPlaysEmpty.json diff --git a/lib/ApiClient/AbstractApiClient.php b/lib/ApiClient/AbstractApiClient.php index b3ac3fa..61cfb31 100644 --- a/lib/ApiClient/AbstractApiClient.php +++ b/lib/ApiClient/AbstractApiClient.php @@ -262,6 +262,25 @@ public function getVideo(int $videoManagerId, string $videoId, ?VideoRequestPara return $this->deserialize($response->getBody()->getContents(), Video::class); } + public function getPlays(int $videoManagerId, string $videoId): int + { + $options = [ + self::OPT_VIDEO_MANAGER_ID => $videoManagerId, + 'videoIds' => [ + $videoId + ] + ]; + + $response = $this->makeRequest('GET', 'analytics/videos', $options); + + $payload = json_decode($response->getBody()->getContents(), true); + + return array_key_exists('videos', $payload) && !empty($payload['videos']) + ? $payload['videos'][0]['plays'] + : 0 + ; + } + /** * {@inheritdoc} */ diff --git a/lib/Interfaces/ApiClientInterface.php b/lib/Interfaces/ApiClientInterface.php index 83cbbbc..6651612 100644 --- a/lib/Interfaces/ApiClientInterface.php +++ b/lib/Interfaces/ApiClientInterface.php @@ -117,6 +117,8 @@ public function deleteVideo(int $videoManagerId, string $videoId): void; public function getVideo(int $videoManagerId, string $videoId, ?VideoRequestParameters $parameters = null): Video; + public function getPlays(int $videoManagerId, string $videoId): int; + public function getCount(int $videoManagerId, ?VideosRequestParameters $parameters = null): int; /** diff --git a/tests/MovingImage/Test/Client/VMPro/ApiClient/Methods/GetPlaysTest.php b/tests/MovingImage/Test/Client/VMPro/ApiClient/Methods/GetPlaysTest.php new file mode 100644 index 0000000..8f82dd8 --- /dev/null +++ b/tests/MovingImage/Test/Client/VMPro/ApiClient/Methods/GetPlaysTest.php @@ -0,0 +1,32 @@ +createMockGuzzleClient(200, [], Fixture::getApiResponse('getPlays')); + + $client = $this->createApiClient($httpClient, $this->createSerializer()); + $plays = $client->getPlays(1, $id); + + self::assertSame(1, $plays); + } + + public function testGetPlaysVideoDoesNotExist() + { + $id = '74WGsUCJ3QJMjN8-LeYFpm'; + $httpClient = $this->createMockGuzzleClient(200, [], Fixture::getApiResponse('getPlaysEmpty')); + + $client = $this->createApiClient($httpClient, $this->createSerializer()); + $plays = $client->getPlays(1, $id); + + self::assertSame(0, $plays); + } +} diff --git a/tests/MovingImage/Test/Fixtures/Resources/ApiResponse/getPlays.json b/tests/MovingImage/Test/Fixtures/Resources/ApiResponse/getPlays.json new file mode 100644 index 0000000..0db7d36 --- /dev/null +++ b/tests/MovingImage/Test/Fixtures/Resources/ApiResponse/getPlays.json @@ -0,0 +1,32 @@ +{ + "videos": [ + { + "id": "74WGsUCJ3QJMjN8-LeYFpm", + "title": "title", + "description": "description", + "createdDate": "2023-04-11T13:17:18.755Z", + "modifiedDate": "2023-04-11T13:17:18.755Z", + "duration": 0, + "uploadedFile": "string", + "published": true, + "channels": [ + { + "id": 0, + "name": "string" + } + ], + "groups": [ + { + "id": 0, + "name": "string" + } + ], + "metadata": { + "additionalProp1": {}, + "additionalProp2": {}, + "additionalProp3": {} + }, + "plays": 1 + } + ] +} \ No newline at end of file diff --git a/tests/MovingImage/Test/Fixtures/Resources/ApiResponse/getPlaysEmpty.json b/tests/MovingImage/Test/Fixtures/Resources/ApiResponse/getPlaysEmpty.json new file mode 100644 index 0000000..c08a597 --- /dev/null +++ b/tests/MovingImage/Test/Fixtures/Resources/ApiResponse/getPlaysEmpty.json @@ -0,0 +1,3 @@ +{ + "videos": [] +} \ No newline at end of file