Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reports api #17

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ called:
$zencoder->inputs->details($input_id);
$zencoder->outputs->details($output_id);
$zencoder->notifications->parseIncoming();
$zencoder->reports->vod($array);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like support for live reports is in this request - can you add a line for that here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

$zencoder->reports->all($array);

Any errors will throw a Services_Zencoder_Exception. You can call getErrors() on an exception
and it will return any errors received from the Zencoder API.
Expand Down Expand Up @@ -175,6 +177,78 @@ Then submit the job to test if it works.
Modify the above script to meet your needs.
Your [notifications page](https://app.zencoder.com/notifications) will come in handy.

REPORTS
----------------------
The ZencoderReports class is used to get reports over the zencoder api.
See [reports api doc](https://app.zencoder.com/docs/api/reports) for required/optional parameters.

### Get usage for VOD
Create a script to get usage for VOD

#### Example
<?php

// Make sure this points to a copy of Zencoder.php on the same server as this script.
require_once('Services/Zencoder.php');

// Initialize the Services_Zencoder class
$zencoder = new Services_Zencoder('93h630j1dsyshjef620qlkavnmzui3');

// Get reports
$params = array(
'from' => '2014-02-01',
'to' => '2014-02-28',
)
$reports = $zencoder->reports->vod($params);
// for live reports you just call the 'live' method i.e $reports = $zencoder->reports->live($params) ($params is optional)

if ($reports->statistics) {
foreach ($reports->statistics as $statistic) {
print_r($statistic);
}
print_r($reports->total);
} else {
echo "no statistics found";
}

?>

### Get usage for VOD & Live
Get reports containing a breakdown of VOD and live-streaming usage.

**see return structure at:**
<https://app.zencoder.com/docs/api/reports/all>

#### Example
<?php

// Make sure this points to a copy of Zencoder.php on the same server as this script.
require_once('Services/Zencoder.php');

// Initialize the Services_Zencoder class
$zencoder = new Services_Zencoder('93h630j1dsyshjef620qlkavnmzui3');

// Get reports
$params = array(
'from' => '2014-02-01',
'to' => '2014-02-28',
)
$reports = $zencoder->reports->all($params);

// $reports->statistics if not empty will be an array with keys 'vod' and 'live' (same for $reports->total)

if (!empty($reports->statistics['vod'])) {
foreach ($reports->statistics['vod'] as $statistic) {
print_r($statistic);
}
print_r($reports->total['vod']);
} else {
echo "no vod statistics found";
}

?>


VERSIONS
---------
Version 2.1.1 - 2012-08-02 Fixing issue where jobs index call didn't return jobs as individual objects
Expand Down
9 changes: 9 additions & 0 deletions Services/Zencoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ class Services_Zencoder extends Services_Zencoder_Base
* @var Services_Zencoder_Outputs
*/
public $outputs;
/**
* Provides access to the Zencoder Reports API
*
* Valid functions: vod, live, minutes, all
*
* @var Services_Zencoder_Reports
*/
public $reports;

/**
* Initialize the Services_Zencoder class and sub-classes.
Expand Down Expand Up @@ -127,6 +135,7 @@ public function __construct(
$this->jobs = new Services_Zencoder_Jobs($this);
$this->notifications = new Services_Zencoder_Notifications($this);
$this->outputs = new Services_Zencoder_Outputs($this);
$this->reports = new Services_Zencoder_Reports($this);
}

/**
Expand Down
113 changes: 113 additions & 0 deletions Services/Zencoder/Report.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

/**
* Zencoder API client interface.
*
* @category Services
* @package Services_Zencoder
* @author Cyril Tata <[email protected]>
* @version Release: 2.1.2
* @license http://creativecommons.org/licenses/MIT/MIT
* @link http://github.com/zencoder/zencoder-php
*/
class Services_Zencoder_Report extends Services_Zencoder_Object {

/**
* Array for statistics of the report
*
* @var array
*/
public $statistics = array();

/**
* Holds the totals in case of single type of report
*
* @var mixed (Services_Zencoder_Report_VodTotal|Services_Zencoder_Report_LiveTotal)
*/
public $total;

/**
* A copy of the raw API response for debug purposes
*
* @var mixed
*/
protected $raw_response;

/**
* Flag to check if you will be filling array of arrays (when type = 'all')
*
* @var boolean
*/
protected $is_multiple = false;

/**
* The type of report that is being fetched ('live', 'vod' 'minutes' or 'all')
*
* @var string
*/
protected $type;

/**
* Create a new Services_Zencoder_Job object.
*
* @param mixed $params API response
* @param string $type The type of statistic we are fetching
*/
public function __construct($params, $type) {
$this->raw_response = $params;
$this->type = $type;
$this->is_multiple = $this->type === "all";
$this->_update_attributes($params);
}

private function _update_attributes($attributes = array()) {
foreach ($attributes as $attr_name => $attr_value) {
if (($attr_name == "statistics")) {
if (!$this->is_multiple) {
$this->_create_statistics($attr_value, $this->type);
} else {
foreach ($attr_value as $type => $attrs) {
$this->_create_statistics($attrs, $type);
}
}
} elseif (($attr_name == "total")) {
if (!$this->is_multiple) {
$this->_create_totals($attr_value, $this->type);
} else {
foreach ($attr_value as $type => $attrs) {
$this->_create_totals($attrs, $type);
}
}
}
}
}

private function _create_statistics($statistics = array(), $type = null) {
$class = $this->_get_report_class('statistic', $type);
if ($this->is_multiple) {
$this->statistics[$type] = array();
}

foreach ($statistics as $stat_attrs) {
if ($this->is_multiple) {
$this->statistics[$type][] = new $class($stat_attrs);
} else {
$this->statistics[] = new $class($stat_attrs);
}
}
}

private function _create_totals($totals, $type = null) {
$class = $this->_get_report_class('total', $type);
if ($this->is_multiple) {
$this->total[$type] = new $class($totals);
} else {
$this->total = new $class($totals);
}
}

private function _get_report_class($attr_name, $type) {
return 'Services_Zencoder_Report_' . ucwords($type) . ucwords($attr_name);
}

}
15 changes: 15 additions & 0 deletions Services/Zencoder/Report/LiveStatistic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Zencoder API client interface.
*
* @category Services
* @package Services_Zencoder
* @author Cyril Tata <[email protected]>
* @version Release: 2.1.2
* @license http://creativecommons.org/licenses/MIT/MIT
* @link http://github.com/zencoder/zencoder-php
*/

class Services_Zencoder_Report_LiveStatistic extends Services_Zencoder_Object
{
}
15 changes: 15 additions & 0 deletions Services/Zencoder/Report/LiveTotal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Zencoder API client interface.
*
* @category Services
* @package Services_Zencoder
* @author Cyril Tata <[email protected]>
* @version Release: 2.1.2
* @license http://creativecommons.org/licenses/MIT/MIT
* @link http://github.com/zencoder/zencoder-php
*/

class Services_Zencoder_Report_LiveTotal extends Services_Zencoder_Object
{
}
15 changes: 15 additions & 0 deletions Services/Zencoder/Report/MinutesStatistic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Zencoder API client interface.
*
* @category Services
* @package Services_Zencoder
* @author Cyril Tata <[email protected]>
* @version Release: 2.1.2
* @license http://creativecommons.org/licenses/MIT/MIT
* @link http://github.com/zencoder/zencoder-php
*/

class Services_Zencoder_Report_MinutesStatistic extends Services_Zencoder_Object
{
}
15 changes: 15 additions & 0 deletions Services/Zencoder/Report/MinutesTotal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Zencoder API client interface.
*
* @category Services
* @package Services_Zencoder
* @author Cyril Tata <[email protected]>
* @version Release: 2.1.2
* @license http://creativecommons.org/licenses/MIT/MIT
* @link http://github.com/zencoder/zencoder-php
*/

class Services_Zencoder_Report_MinutesTotal extends Services_Zencoder_Object
{
}
15 changes: 15 additions & 0 deletions Services/Zencoder/Report/VodStatistic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Zencoder API client interface.
*
* @category Services
* @package Services_Zencoder
* @author Cyril Tata <[email protected]>
* @version Release: 2.1.2
* @license http://creativecommons.org/licenses/MIT/MIT
* @link http://github.com/zencoder/zencoder-php
*/

class Services_Zencoder_Report_VodStatistic extends Services_Zencoder_Object
{
}
15 changes: 15 additions & 0 deletions Services/Zencoder/Report/VodTotal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Zencoder API client interface.
*
* @category Services
* @package Services_Zencoder
* @author Cyril Tata <[email protected]>
* @version Release: 2.1.2
* @license http://creativecommons.org/licenses/MIT/MIT
* @link http://github.com/zencoder/zencoder-php
*/

class Services_Zencoder_Report_VodTotal extends Services_Zencoder_Object
{
}
58 changes: 58 additions & 0 deletions Services/Zencoder/Reports.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* Zencoder API client interface.
*
* @category Services
* @package Services_Zencoder
* @author Cyril Tata <[email protected]>
* @version Release: 2.1.2
* @license http://creativecommons.org/licenses/MIT/MIT
* @link http://github.com/zencoder/zencoder-php
*/
class Services_Zencoder_Reports extends Services_Zencoder_Base
{
/**
* A list of valid 'methods' to be trapped in __call()
*
* @link https://app.zencoder.com/docs/api/reports/
* @var array
*/
protected $methods = array('vod', 'live', 'minutes', 'all');

/**
* Return all reports for VOD
*
* @link https://app.zencoder.com/docs/api/reports
*
* @param string $method The method name indicates the type of report we want to get
* @param array $args A list of arguments for the overriden methods. Each methods takes 2 arguements.
* The first being an associative array of query string parameters and the second
* an associative array of option overrides
*
* @throws Services_Zencoder_Exception
*
* @return Services_Zencoder_Report The object representation of the resource
*/
public function __call($method, $args) {
if (!in_array($method, $this->methods)) {
throw new Services_Zencoder_Exception("Unsupported method call '$method' for Services_Zencoder_Reports");
}

// initialize query string parameters and optional overrides
$params = $opts = array();

// set query string parameters
if (isset($args[0]) && is_array($args[0])) {
$params = $args[0];
}

// set optional overrides
if (isset($args[1]) && is_array($args[1])) {
$opts = $args[1];
}

return new Services_Zencoder_Report($this->proxy->retrieveData("reports/$method", $params, $opts), $method);
}

}