Dns analytics report (#121)

* Add DNS Analytics report endpoint


Add DNS Analytics report endpoint


Add DNS Analytics report endpoint

* Remove useless response error test

* Add tests for DNS analytics report class methods

* Implement methods for listing grouped by time metrics report
This commit is contained in:
Constantin Cuciurcă
2020-07-07 13:17:48 +03:00
committed by GitHub
parent 06de5b0c04
commit ffc3989607
6 changed files with 372 additions and 2 deletions

View File

@@ -0,0 +1,69 @@
<?php
namespace Cloudflare\API\Configurations;
class DNSAnalytics implements Configurations
{
protected $configs = [];
public function getArray(): array
{
return $this->configs;
}
public function setDimensions(array $dimensions)
{
if (count($dimensions) !== 0) {
$this->configs['dimensions'] = implode(',', $dimensions);
}
}
public function setMetrics(array $metrics)
{
if (count($metrics) !== 0) {
$this->configs['metrics'] = implode(',', $metrics);
}
}
public function setSince(string $since)
{
if ($since) {
$this->configs['since'] = $since;
}
}
public function setUntil(string $until)
{
if ($until) {
$this->configs['until'] = $until;
}
}
public function setSorting(array $sorting)
{
if (count($sorting) !== 0) {
$this->configs['sort'] = implode(',', $sorting);
}
}
public function setFilters(string $filters)
{
if ($filters) {
$this->configs['filters'] = $filters;
}
}
public function setLimit(int $limit)
{
if ($limit) {
$this->configs['limit'] = $limit;
}
}
public function setTimeDelta(string $timeDelta)
{
if ($timeDelta) {
$this->configs['time_delta'] = $timeDelta;
}
}
}

View File

@@ -0,0 +1,144 @@
<?php
/**
* Created by Visual Studio Code.
* User: elliot.alderson
* Date: 2020-02-06
* Time: 03:40 AM
*/
namespace Cloudflare\API\Endpoints;
use Cloudflare\API\Adapter\Adapter;
use Cloudflare\API\Traits\BodyAccessorTrait;
use Cloudflare\API\Configurations\DNSAnalytics as Configs;
class DNSAnalytics implements API
{
use BodyAccessorTrait;
private $adapter;
public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
}
/**
* Retrieves a list of summarised aggregate metrics over a given time period.
*
* @param string $zoneID ID of zone to get report for
* @param string $dimensions Comma separated names of dimensions
* @param string $metrics Comma separated names of dimension to get metrics for
* @param string $sort Comma separated names of dimension to sort by prefixed by order - (descending) or + (ascending)
* @param string $filters Segmentation filter in 'attribute operator value' format
* @param string $since Start date and time of requesting data period in the ISO8601 format
* @param string $until End date and time of requesting data period in the ISO8601 format
* @param string $limit Limit number of returned metrics
* @return array
*/
public function getReportTable(
string $zoneID,
array $dimensions = [],
array $metrics = [],
array $sort = [],
string $filters = '',
string $since = '',
string $until = '',
int $limit = 100
): \stdClass {
if (count($dimensions) === 0) {
throw new EndpointException(
'At least one dimension is required for getting a report.'
);
}
if (count($metrics) === 0) {
throw new EndpointException(
'At least one metric is required for getting a report.'
);
}
if (!$since) {
throw new EndpointException(
'Start date is required for getting a report.'
);
}
if (!$until) {
throw new EndpointException(
'End date is required for getting a report.'
);
}
$options = [
'dimensions' => implode(',', $dimensions),
'metrics' => implode(',', $metrics),
'since' => $since,
'until' => $until
];
if (count($sort) !== 0) {
$options['sort'] = implode(',', $sort);
}
if ($filters) {
$options['filters'] = $filters;
}
if ($limit) {
$options['limit'] = $limit;
}
$endpoint = 'zones/' . $zoneID . '/dns_analytics/report';
$report = $this->adapter->get($endpoint, $options);
$this->body = json_decode($report->getBody());
return $this->body->result;
}
/**
* Retrieves a list of aggregate metrics grouped by time interval.
*
* @param string $zoneID ID of zone to get report for
* @param string $dimensions Comma separated names of dimensions
* @param string $metrics Comma separated names of dimension to get metrics for
* @param string $sort Comma separated names of dimension to sort by prefixed by order - (descending) or + (ascending)
* @param string $filters Segmentation filter in 'attribute operator value' format
* @param string $since Start date and time of requesting data period in the ISO8601 format
* @param string $until End date and time of requesting data period in the ISO8601 format
* @param string $limit Limit number of returned metrics
* @param string $timeDelta Unit of time to group data by
* @return array
*/
public function getReportByTime(
string $zoneID,
array $dimensions = [],
array $metrics = [],
array $sort = [],
string $filters = '',
string $since = '',
string $until = '',
int $limit = 100,
string $timeDelta = ''
): \stdClass {
$options = new Configs();
$options->setDimensions($dimensions);
$options->setMetrics($metrics);
$options->setSince($since);
$options->setUntil($until);
$options->setSorting($sort);
$options->setFilters($filters);
$options->setLimit($limit);
$options->setTimeDelta($timeDelta);
$endpoint = 'zones/' . $zoneID . '/dns_analytics/report/bytime';
$report = $this->adapter->get($endpoint, $options->getArray());
$this->body = json_decode($report->getBody());
return $this->body->result;
}
}