Skip to content

Commit

Permalink
Merge pull request #55 from apivideo/date-rage-required-in-analytics
Browse files Browse the repository at this point in the history
feat(all) Date range param is required in analytics endpoints
  • Loading branch information
bot-api-video authored Sep 14, 2022
2 parents d711b99 + a0c5e4e commit fe530d1
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
All changes to this project will be documented in this file.

## [1.2.6] - 2022-09-13
- period parameter is now mandatory in analytics endpoints

## [1.2.5] - 2022-07-05
- Add SDK origin header

Expand Down
4 changes: 2 additions & 2 deletions docs/Api/RawStatisticsApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Note: `queryParams` argument is an associative array with the keys listed below.

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
`period` | **string**| Period must have one of the following formats: - For a day : \"2018-01-01\", - For a week: \"2018-W01\", - For a month: \"2018-01\" - For a year: \"2018\" For a range period: - Date range: \"2018-01-01/2018-01-15\" | [optional]
`period` | **string**| Period must have one of the following formats: - For a day : \"2018-01-01\", - For a week: \"2018-W01\", - For a month: \"2018-01\" - For a year: \"2018\" For a range period: - Date range: \"2018-01-01/2018-01-15\" |
`currentPage` | **int**| Choose the number of search results to return per page. Minimum value: 1 | [optional] [default to 1]
`pageSize` | **int**| Results per page. Allowed values 1-100, default is 25. | [optional] [default to 25]

Expand Down Expand Up @@ -125,7 +125,7 @@ Note: `queryParams` argument is an associative array with the keys listed below.

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
`period` | **string**| Period must have one of the following formats: - For a day : 2018-01-01, - For a week: 2018-W01, - For a month: 2018-01 - For a year: 2018 For a range period: - Date range: 2018-01-01/2018-01-15 | [optional]
`period` | **string**| Period must have one of the following formats: - For a day : 2018-01-01, - For a week: 2018-W01, - For a month: 2018-01 - For a year: 2018 For a range period: - Date range: 2018-01-01/2018-01-15 |
`metadata` | [**array<string,string>**](../Model/string.md)| Metadata and [Dynamic Metadata](https://api.video/blog/endpoints/dynamic-metadata) filter. Send an array of key value pairs you want to filter sessios with. | [optional]
`currentPage` | **int**| Choose the number of search results to return per page. Minimum value: 1 | [optional] [default to 1]
`pageSize` | **int**| Results per page. Allowed values 1-100, default is 25. | [optional] [default to 25]
Expand Down
12 changes: 12 additions & 0 deletions src/Api/RawStatisticsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ private function buildListLiveStreamSessionsRequest(string $liveStreamId, array
'Missing the required parameter $liveStreamId when calling '
);
}
// verify the required parameter 'period' is set
if ($period === null || (is_array($period) && count($period) === 0)) {
throw new \InvalidArgumentException(
'Missing the required parameter $period when calling '
);
}

$resourcePath = '/analytics/live-streams/{liveStreamId}';
$formParams = [];
Expand Down Expand Up @@ -250,6 +256,12 @@ private function buildListVideoSessionsRequest(string $videoId, array $queryPara
'Missing the required parameter $videoId when calling '
);
}
// verify the required parameter 'period' is set
if ($period === null || (is_array($period) && count($period) === 0)) {
throw new \InvalidArgumentException(
'Missing the required parameter $period when calling '
);
}

$resourcePath = '/analytics/videos/{videoId}';
$formParams = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function authenticate(): void
$request = $request->setHeader('AV-Origin-Sdk', $this->originSdkHeaderValue);
}

$properties = $this->client->request($request);
$properties = $this->client->request($request, true);

$this->accessToken = $properties[self::PROP_ACCESS_TOKEN];
}
Expand Down
45 changes: 26 additions & 19 deletions src/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function __construct(string $baseUri, ?string $apiKey, ClientInterface $h
$this->originSdkHeaderValue = "";

if ($apiKey) {
$this->authenticator = new Authenticator($this, $apiKey, 'php:1.2.5');
$this->authenticator = new Authenticator($this, $apiKey, 'php:1.2.6');
}
}

Expand All @@ -87,7 +87,7 @@ public function __construct(string $baseUri, ?string $apiKey, ClientInterface $h
* @return array|null
* @throws ClientExceptionInterface
*/
public function request(Request $commandRequest): ?array
public function request(Request $commandRequest, bool $skipAuthRequest = false): ?array
{
$stream = $commandRequest->getStream();

Expand All @@ -110,9 +110,9 @@ public function request(Request $commandRequest): ?array
if($this->originSdkHeaderValue) {
$request = $request->withHeader('AV-Origin-Sdk', $this->originSdkHeaderValue);
}
$request = $request->withHeader('AV-Origin-Client', 'php:1.2.5');
$request = $request->withHeader('AV-Origin-Client', 'php:1.2.6');

return $this->sendRequest($request);
return $this->sendRequest($request, $skipAuthRequest);
}

/**
Expand Down Expand Up @@ -180,34 +180,41 @@ public function getStreamFactory(): StreamFactoryInterface
return $this->streamFactory;
}

/**
* @return ClientInterface
*/
public function getHttpClient(): ClientInterface
{
return $this->httpClient;
}

/**
* @param RequestInterface $request
*
* @return array|null
* @throws ClientExceptionInterface
* @throws AuthenticationFailedException
*/
private function sendRequest(RequestInterface $request): ?array
private function sendRequest(RequestInterface $request, bool $skipAuthRequest = false): ?array
{
if ($this->authenticator) {
if ($this->authenticator && !$skipAuthRequest) {
$request = $this->authenticator->authenticateRequest($request);
}

try {
$response = $this->httpClient->sendRequest($request);
$response = $this->httpClient->sendRequest($request);

if (Authenticator::isExpiredAuthTokenResponse($response)) {
throw new ExpiredAuthTokenException();
if (Authenticator::isExpiredAuthTokenResponse($response)) {
if(!$skipAuthRequest) {
$this->authenticator->authenticate();
return $this->sendRequest($request);
}
throw new AuthenticationFailedException();
}

if (400 <= $response->getStatusCode()) {
throw new HttpException($request, $response);
}

return json_decode($response->getBody()->getContents(), true);
} catch (ExpiredAuthTokenException $e) {
$this->authenticator->authenticate();

return $this->sendRequest($request);
if (400 <= $response->getStatusCode()) {
throw new HttpException($request, $response);
}

return json_decode($response->getBody()->getContents(), true);
}
}
4 changes: 2 additions & 2 deletions tests/Api/RawStatisticsApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function testListLiveStreamSessions()
{
$liveStream = (new Helper($this->client))->createLiveStream();

$response = $this->client->rawStatistics()->listLiveStreamSessions($liveStream->getLiveStreamId());
$response = $this->client->rawStatistics()->listLiveStreamSessions($liveStream->getLiveStreamId(), ['period' => '2022-01']);

$this->assertCount(0, $response->getData());
}
Expand All @@ -20,7 +20,7 @@ public function testListVideoSessions()
{
$video = (new Helper($this->client))->createVideo();

$response = $this->client->rawStatistics()->listVideoSessions($video->getVideoId());
$response = $this->client->rawStatistics()->listVideoSessions($video->getVideoId(), ['period' => '2022-01']);

$this->assertCount(0, $response->getData());
}
Expand Down

0 comments on commit fe530d1

Please sign in to comment.