Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,36 @@ $resertation = $account->tnsreservations()->tnsreservation("0099ff73-da96-4303-8
$resertation = $account->tnsreservations()->tnsreservation("0099ff73-da96-4303-8a0a-00ff316c07aa");
$resertation->delete();
```

## Billing reports

### Listing all Billing Report instances
```PHP
$billingReports = $account->billingreports()->getList();
```

### Request new billing report
```PHP
$billingReport = $account->billingreports()->request(
array("Type" => "bdr",
"DateRange" => array(
"StartDate" => "2018-02-05",
"EndDate" => "2018-02-06",
)));
```

### Checking status of the billing report
```PHP
$billingReport = $account->billingreports()->billingreport('a12b456c8-abcd-1a3b-a1b2-0a2b4c6d8e0f2');
```

### Download zip with content of the billing report
```PHP
$zipStream = $billingReport->file();
```

### Download zip with content of the billing report and save to file
```PHP
$outFile = '/tmp/report.zip';
$billingReport->file(['stream' => true, 'sink' => $outFile]);
```
30 changes: 20 additions & 10 deletions core/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,36 +316,46 @@ private function prepareXmlBody($data, $baseNode)
*/
private function parseResponse($response)
{
$responseBody = (string) $response->getBody();
$result = [];

if (!$responseBody && $response->hasHeader('Location'))
if ($response->hasHeader('Location'))
{
$location = $response->getHeader('Location');
return ['Location' => reset($location)];
$result['Location'] = reset($location);
unset($location);
}

if (!$responseBody)
$contentType = $response->getHeader('Content-Type');
$contentType = reset($contentType);

if ($contentType && strpos($contentType, 'zip') !== false)
{
return [];
return $response->getBody();
}

$contentType = $response->getHeader('Content-Type');
$contentType = reset($contentType);
$responseBody = (string) $response->getBody();

if (!$responseBody)
{
return $result;
}

if ($contentType && strpos($contentType, 'json') !== false)
{
return json_decode($responseBody, true);
$responseBody = json_decode($responseBody, true);
}

try
{
$xml = new SimpleXMLElement($responseBody);
return $this->xml2array($xml);
$responseBody = $this->xml2array($xml);
unset($xml);
}
catch (Exception $e)
{
return [];
}

return array_replace($result, $responseBody);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ public function users() {
return $this->users;
}

/**
* Get BillingReports instance
*
* @return BillingReports
*/
public function billingreports()
{
if (!isset($this->billingReports))
{
$this->billingReports = new BillingReports($this);
}

return $this->billingReports;
}

public function reports() {
if(!isset($this->reports))
$this->reports = new Reports($this);
Expand Down
138 changes: 138 additions & 0 deletions src/BillingReports.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

/**
* @model BillingReports
*
*/

namespace Iris;

class BillingReports extends RestEntry
{
public function __construct($parent)
{
$this->parent = $parent;
parent::_init(
$this->parent->get_rest_client(),
$this->parent->get_relative_namespace()
);
}

public function getList($filters = [])
{
$billingReports = [];

$data = parent::_get('billingreports');

if (!(isset($data['BillingReportList']['BillingReport']) && is_array($data['BillingReportList']['BillingReport'])))
{
return $billingReports;
}

foreach ($data['BillingReportList']['BillingReport'] as $item)
{
if (isset($item['BillingReportId']))
{
$item['Id'] = $item['BillingReportId'];
unset($item['BillingReportId']);
}
if (isset($item['BillingReportKind']))
{
$item['Type'] = $item['BillingReportKind'];
unset($item['BillingReportKind']);
}
$billingReports[] = new BillingReport($this, $item);
}

return $billingReports;
}

public function billingreport($id)
{
$billingReport = new BillingReport($this, ["Id" => $id]);
$billingReport->get();

return $billingReport;
}

public function get_appendix()
{
return '/billingreports';
}

public function request($data)
{
$billingReport = new BillingReport($this, $data);

return $billingReport->save();
}
}

class BillingReport extends RestEntry
{
use BaseModel;

protected $fields = array(
"Id" => array("type" => "string"),
"UserId" => array("type" => "string"),
"Type" => array("type" => "string"),
"DateRange" => array("type" => "\Iris\DateRange"),
"ReportStatus" => array("type" => "string"),
"CreatedDate" => array("type" => "string"),
"Description" => array("type" => "string"),
);

public function __construct($parent, $data)
{
$this->set_data($data);
$this->parent = $parent;
parent::_init(
$parent->get_rest_client(),
$parent->get_relative_namespace()
);
}

public function get()
{
$data = parent::_get($this->get_id());
$this->set_data($data);
}

public function get_id()
{
if (!isset($this->Id))
{
throw new \Exception('Id should be provided');
}

return $this->Id;
}

public function save()
{
$header = parent::post(null, "BillingReport", $this->to_array());
$splitted = explode("/", $header['Location']);
$this->Id = end($splitted);

return $this;
}

/**
* Download zip report file
*
* @return mixed
* @throws \Exception
*/
public function file($options = [])
{
if (!isset($this->ReportStatus) || $this->ReportStatus !== 'COMPLETED')
{
return false;
}

$url = sprintf('%s/%s', $this->get_id(), 'file');
$url = parent::get_url($url);

return $this->client->get($url, $options);
}
}
16 changes: 16 additions & 0 deletions src/simpleModels/DateRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Iris;

class DateRange {
use BaseModel;

protected $fields = array(
"StartDate" => array("type" => "string"),
"EndDate" => array("type" => "string"),
);

public function __construct($data) {
$this->set_data($data);
}
}
77 changes: 76 additions & 1 deletion tests/AccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,38 @@ public static function setUpBeforeClass() {
new Response(200, [], "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Quantity><Count>4</Count></Quantity>"),
new Response(200, [], "<?xml version=\"1.0\"?><TNs><TotalCount>4</TotalCount><Links><first></first></Links><TelephoneNumbers><Count>2</Count><TelephoneNumber>4158714245</TelephoneNumber><TelephoneNumber>4352154439</TelephoneNumber></TelephoneNumbers></TNs>"),
new Response(200, [], "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Quantity><Count>4</Count></Quantity>"),
new Response(200, [], "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><BdrCreationResponse><Info>Your BDR archive is currently being constructed</Info> </BdrCreationResponse>")
new Response(200, [], "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><BdrCreationResponse><Info>Your BDR archive is currently being constructed</Info> </BdrCreationResponse>"),
new Response(200, [], "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><BillingReportsRetrievalResponse>
<BillingReportList>
<BillingReport>
<BillingReportId>5f8734f0-d7c3-445c-b1e2-cdbb620e4ff7</BillingReportId>
<BillingReportKind>DIDSNAP</BillingReportKind>
<UserId>jbm</UserId>
<ReportStatus>PROCESSING</ReportStatus>
<Description>The requested report archive is still being constructed, please check back later.</Description>
<CreatedDate>2017-11-01 14:12:16</CreatedDate>
<DateRange>
<StartDate>2017-01-01</StartDate>
<EndDate>2017-09-30</EndDate>
</DateRange>
</BillingReport>
<BillingReport>
<BillingReportId>7680a54a-b1f1-4d43-8af6-bf3a701ad202</BillingReportId>
<BillingReportKind>DIDSNAP</BillingReportKind>
<UserId>jbm</UserId>
<ReportStatus>COMPLETE</ReportStatus>
<Description>The requested report archive is failed</Description>
<CreatedDate>2017-11-06 14:22:21</CreatedDate>
<DateRange>
<StartDate>2017-05-01</StartDate>
<EndDate>2017-10-31</EndDate>
</DateRange>
</BillingReport>
</BillingReportList>
</BillingReportsRetrievalResponse>"),
new Response(201, ['Location' => 'https://api.test.inetwork.com:443/v1.0/accounts/9500249/billingreports/a12b456c8-abcd-1a3b-a1b2-0a2b4c6d8e0f2'], '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><BillingReportCreationResponse><ReportStatus>RECEIVED</ReportStatus><Description>The report archive is currently being constructed.</Description></BillingReportCreationResponse>'),
new Response(200, ['Location' => 'https://api.test.inetwork.com:443/v1.0/accounts/9500249/billingreports/a12b456c8-abcd-1a3b-a1b2-0a2b4c6d8e0f2/file'], '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><BillingReportRetrievalResponse><ReportStatus>COMPLETED</ReportStatus><Description>The report archive is constructed.</Description></BillingReportRetrievalResponse>'),
new Response(200, ['Content-Type' => 'application/zip'], 'zipcontent'),
]);

self::$container = [];
Expand Down Expand Up @@ -203,4 +234,48 @@ public function testBdr() {
self::$index++;
}

public function testBillingReports() {
$billingReports = self::$account->billingreports()->getList();

$this->assertEquals(2, count($billingReports));
$this->assertEquals('5f8734f0-d7c3-445c-b1e2-cdbb620e4ff7', $billingReports[0]->Id);
$this->assertEquals("GET", self::$container[self::$index]['request']->getMethod());
$this->assertEquals("https://api.test.inetwork.com/v1.0/accounts/9500249/billingreports", self::$container[self::$index]['request']->getUri());
unset($billingReports);

self::$index++;
}

public function testBillingReportRequest() {
$response = self::$account->billingreports()->request(array(
'Type' => 'BDR',
'DateRange' => array(
'StartDate' => '2017-11-01',
'EndDate' => '2017-11-02',
)
));

$this->assertEquals("a12b456c8-abcd-1a3b-a1b2-0a2b4c6d8e0f2", $response->Id);
$this->assertEquals("POST", self::$container[self::$index]['request']->getMethod());
$this->assertEquals("https://api.test.inetwork.com/v1.0/accounts/9500249/billingreports", self::$container[self::$index]['request']->getUri());
self::$index++;
}

public function testBillingReportReadyAndDownload() {
$billingReport = self::$account->billingreports()
->billingreport('a12b456c8-abcd-1a3b-a1b2-0a2b4c6d8e0f2');

$this->assertEquals("a12b456c8-abcd-1a3b-a1b2-0a2b4c6d8e0f2", $billingReport->Id);
$this->assertEquals("COMPLETED", $billingReport->ReportStatus);
$this->assertEquals("GET", self::$container[self::$index]['request']->getMethod());
$this->assertEquals("https://api.test.inetwork.com/v1.0/accounts/9500249/billingreports/a12b456c8-abcd-1a3b-a1b2-0a2b4c6d8e0f2", self::$container[self::$index]['request']->getUri());
self::$index++;

$zip = $billingReport->file();

$this->assertEquals("zipcontent", $zip->getContents());
$this->assertEquals("GET", self::$container[self::$index]['request']->getMethod());
$this->assertEquals("https://api.test.inetwork.com/v1.0/accounts/9500249/billingreports/a12b456c8-abcd-1a3b-a1b2-0a2b4c6d8e0f2/file", self::$container[self::$index]['request']->getUri());
self::$index++;
}
}