Skip to content

Commit aa5506c

Browse files
pashameshDaniel Tolbert
authored andcommitted
Added support of Billing Reports (#21)
* Added support of Billing Reports * Added ability to pass request options to `file` method of bilingreports * Fixed missing `options` argument of `file` method
1 parent 6967b0b commit aa5506c

File tree

6 files changed

+298
-11
lines changed

6 files changed

+298
-11
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,3 +524,36 @@ $resertation = $account->tnsreservations()->tnsreservation("0099ff73-da96-4303-8
524524
$resertation = $account->tnsreservations()->tnsreservation("0099ff73-da96-4303-8a0a-00ff316c07aa");
525525
$resertation->delete();
526526
```
527+
528+
## Billing reports
529+
530+
### Listing all Billing Report instances
531+
```PHP
532+
$billingReports = $account->billingreports()->getList();
533+
```
534+
535+
### Request new billing report
536+
```PHP
537+
$billingReport = $account->billingreports()->request(
538+
array("Type" => "bdr",
539+
"DateRange" => array(
540+
"StartDate" => "2018-02-05",
541+
"EndDate" => "2018-02-06",
542+
)));
543+
```
544+
545+
### Checking status of the billing report
546+
```PHP
547+
$billingReport = $account->billingreports()->billingreport('a12b456c8-abcd-1a3b-a1b2-0a2b4c6d8e0f2');
548+
```
549+
550+
### Download zip with content of the billing report
551+
```PHP
552+
$zipStream = $billingReport->file();
553+
```
554+
555+
### Download zip with content of the billing report and save to file
556+
```PHP
557+
$outFile = '/tmp/report.zip';
558+
$billingReport->file(['stream' => true, 'sink' => $outFile]);
559+
```

core/Client.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -316,36 +316,46 @@ private function prepareXmlBody($data, $baseNode)
316316
*/
317317
private function parseResponse($response)
318318
{
319-
$responseBody = (string) $response->getBody();
319+
$result = [];
320320

321-
if (!$responseBody && $response->hasHeader('Location'))
321+
if ($response->hasHeader('Location'))
322322
{
323323
$location = $response->getHeader('Location');
324-
return ['Location' => reset($location)];
324+
$result['Location'] = reset($location);
325+
unset($location);
325326
}
326327

327-
if (!$responseBody)
328+
$contentType = $response->getHeader('Content-Type');
329+
$contentType = reset($contentType);
330+
331+
if ($contentType && strpos($contentType, 'zip') !== false)
328332
{
329-
return [];
333+
return $response->getBody();
330334
}
331335

332-
$contentType = $response->getHeader('Content-Type');
333-
$contentType = reset($contentType);
336+
$responseBody = (string) $response->getBody();
337+
338+
if (!$responseBody)
339+
{
340+
return $result;
341+
}
334342

335343
if ($contentType && strpos($contentType, 'json') !== false)
336344
{
337-
return json_decode($responseBody, true);
345+
$responseBody = json_decode($responseBody, true);
338346
}
339347

340348
try
341349
{
342350
$xml = new SimpleXMLElement($responseBody);
343-
return $this->xml2array($xml);
351+
$responseBody = $this->xml2array($xml);
352+
unset($xml);
344353
}
345354
catch (Exception $e)
346355
{
347-
return [];
348356
}
357+
358+
return array_replace($result, $responseBody);
349359
}
350360

351361
/**

src/Account.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ public function users() {
110110
return $this->users;
111111
}
112112

113+
/**
114+
* Get BillingReports instance
115+
*
116+
* @return BillingReports
117+
*/
118+
public function billingreports()
119+
{
120+
if (!isset($this->billingReports))
121+
{
122+
$this->billingReports = new BillingReports($this);
123+
}
124+
125+
return $this->billingReports;
126+
}
127+
113128
public function reports() {
114129
if(!isset($this->reports))
115130
$this->reports = new Reports($this);

src/BillingReports.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
/**
4+
* @model BillingReports
5+
*
6+
*/
7+
8+
namespace Iris;
9+
10+
class BillingReports extends RestEntry
11+
{
12+
public function __construct($parent)
13+
{
14+
$this->parent = $parent;
15+
parent::_init(
16+
$this->parent->get_rest_client(),
17+
$this->parent->get_relative_namespace()
18+
);
19+
}
20+
21+
public function getList($filters = [])
22+
{
23+
$billingReports = [];
24+
25+
$data = parent::_get('billingreports');
26+
27+
if (!(isset($data['BillingReportList']['BillingReport']) && is_array($data['BillingReportList']['BillingReport'])))
28+
{
29+
return $billingReports;
30+
}
31+
32+
foreach ($data['BillingReportList']['BillingReport'] as $item)
33+
{
34+
if (isset($item['BillingReportId']))
35+
{
36+
$item['Id'] = $item['BillingReportId'];
37+
unset($item['BillingReportId']);
38+
}
39+
if (isset($item['BillingReportKind']))
40+
{
41+
$item['Type'] = $item['BillingReportKind'];
42+
unset($item['BillingReportKind']);
43+
}
44+
$billingReports[] = new BillingReport($this, $item);
45+
}
46+
47+
return $billingReports;
48+
}
49+
50+
public function billingreport($id)
51+
{
52+
$billingReport = new BillingReport($this, ["Id" => $id]);
53+
$billingReport->get();
54+
55+
return $billingReport;
56+
}
57+
58+
public function get_appendix()
59+
{
60+
return '/billingreports';
61+
}
62+
63+
public function request($data)
64+
{
65+
$billingReport = new BillingReport($this, $data);
66+
67+
return $billingReport->save();
68+
}
69+
}
70+
71+
class BillingReport extends RestEntry
72+
{
73+
use BaseModel;
74+
75+
protected $fields = array(
76+
"Id" => array("type" => "string"),
77+
"UserId" => array("type" => "string"),
78+
"Type" => array("type" => "string"),
79+
"DateRange" => array("type" => "\Iris\DateRange"),
80+
"ReportStatus" => array("type" => "string"),
81+
"CreatedDate" => array("type" => "string"),
82+
"Description" => array("type" => "string"),
83+
);
84+
85+
public function __construct($parent, $data)
86+
{
87+
$this->set_data($data);
88+
$this->parent = $parent;
89+
parent::_init(
90+
$parent->get_rest_client(),
91+
$parent->get_relative_namespace()
92+
);
93+
}
94+
95+
public function get()
96+
{
97+
$data = parent::_get($this->get_id());
98+
$this->set_data($data);
99+
}
100+
101+
public function get_id()
102+
{
103+
if (!isset($this->Id))
104+
{
105+
throw new \Exception('Id should be provided');
106+
}
107+
108+
return $this->Id;
109+
}
110+
111+
public function save()
112+
{
113+
$header = parent::post(null, "BillingReport", $this->to_array());
114+
$splitted = explode("/", $header['Location']);
115+
$this->Id = end($splitted);
116+
117+
return $this;
118+
}
119+
120+
/**
121+
* Download zip report file
122+
*
123+
* @return mixed
124+
* @throws \Exception
125+
*/
126+
public function file($options = [])
127+
{
128+
if (!isset($this->ReportStatus) || $this->ReportStatus !== 'COMPLETED')
129+
{
130+
return false;
131+
}
132+
133+
$url = sprintf('%s/%s', $this->get_id(), 'file');
134+
$url = parent::get_url($url);
135+
136+
return $this->client->get($url, $options);
137+
}
138+
}

src/simpleModels/DateRange.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Iris;
4+
5+
class DateRange {
6+
use BaseModel;
7+
8+
protected $fields = array(
9+
"StartDate" => array("type" => "string"),
10+
"EndDate" => array("type" => "string"),
11+
);
12+
13+
public function __construct($data) {
14+
$this->set_data($data);
15+
}
16+
}

tests/AccountTest.php

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,38 @@ public static function setUpBeforeClass() {
2727
new Response(200, [], "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Quantity><Count>4</Count></Quantity>"),
2828
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>"),
2929
new Response(200, [], "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Quantity><Count>4</Count></Quantity>"),
30-
new Response(200, [], "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><BdrCreationResponse><Info>Your BDR archive is currently being constructed</Info> </BdrCreationResponse>")
30+
new Response(200, [], "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><BdrCreationResponse><Info>Your BDR archive is currently being constructed</Info> </BdrCreationResponse>"),
31+
new Response(200, [], "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><BillingReportsRetrievalResponse>
32+
<BillingReportList>
33+
<BillingReport>
34+
<BillingReportId>5f8734f0-d7c3-445c-b1e2-cdbb620e4ff7</BillingReportId>
35+
<BillingReportKind>DIDSNAP</BillingReportKind>
36+
<UserId>jbm</UserId>
37+
<ReportStatus>PROCESSING</ReportStatus>
38+
<Description>The requested report archive is still being constructed, please check back later.</Description>
39+
<CreatedDate>2017-11-01 14:12:16</CreatedDate>
40+
<DateRange>
41+
<StartDate>2017-01-01</StartDate>
42+
<EndDate>2017-09-30</EndDate>
43+
</DateRange>
44+
</BillingReport>
45+
<BillingReport>
46+
<BillingReportId>7680a54a-b1f1-4d43-8af6-bf3a701ad202</BillingReportId>
47+
<BillingReportKind>DIDSNAP</BillingReportKind>
48+
<UserId>jbm</UserId>
49+
<ReportStatus>COMPLETE</ReportStatus>
50+
<Description>The requested report archive is failed</Description>
51+
<CreatedDate>2017-11-06 14:22:21</CreatedDate>
52+
<DateRange>
53+
<StartDate>2017-05-01</StartDate>
54+
<EndDate>2017-10-31</EndDate>
55+
</DateRange>
56+
</BillingReport>
57+
</BillingReportList>
58+
</BillingReportsRetrievalResponse>"),
59+
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>'),
60+
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>'),
61+
new Response(200, ['Content-Type' => 'application/zip'], 'zipcontent'),
3162
]);
3263

3364
self::$container = [];
@@ -203,4 +234,48 @@ public function testBdr() {
203234
self::$index++;
204235
}
205236

237+
public function testBillingReports() {
238+
$billingReports = self::$account->billingreports()->getList();
239+
240+
$this->assertEquals(2, count($billingReports));
241+
$this->assertEquals('5f8734f0-d7c3-445c-b1e2-cdbb620e4ff7', $billingReports[0]->Id);
242+
$this->assertEquals("GET", self::$container[self::$index]['request']->getMethod());
243+
$this->assertEquals("https://api.test.inetwork.com/v1.0/accounts/9500249/billingreports", self::$container[self::$index]['request']->getUri());
244+
unset($billingReports);
245+
246+
self::$index++;
247+
}
248+
249+
public function testBillingReportRequest() {
250+
$response = self::$account->billingreports()->request(array(
251+
'Type' => 'BDR',
252+
'DateRange' => array(
253+
'StartDate' => '2017-11-01',
254+
'EndDate' => '2017-11-02',
255+
)
256+
));
257+
258+
$this->assertEquals("a12b456c8-abcd-1a3b-a1b2-0a2b4c6d8e0f2", $response->Id);
259+
$this->assertEquals("POST", self::$container[self::$index]['request']->getMethod());
260+
$this->assertEquals("https://api.test.inetwork.com/v1.0/accounts/9500249/billingreports", self::$container[self::$index]['request']->getUri());
261+
self::$index++;
262+
}
263+
264+
public function testBillingReportReadyAndDownload() {
265+
$billingReport = self::$account->billingreports()
266+
->billingreport('a12b456c8-abcd-1a3b-a1b2-0a2b4c6d8e0f2');
267+
268+
$this->assertEquals("a12b456c8-abcd-1a3b-a1b2-0a2b4c6d8e0f2", $billingReport->Id);
269+
$this->assertEquals("COMPLETED", $billingReport->ReportStatus);
270+
$this->assertEquals("GET", self::$container[self::$index]['request']->getMethod());
271+
$this->assertEquals("https://api.test.inetwork.com/v1.0/accounts/9500249/billingreports/a12b456c8-abcd-1a3b-a1b2-0a2b4c6d8e0f2", self::$container[self::$index]['request']->getUri());
272+
self::$index++;
273+
274+
$zip = $billingReport->file();
275+
276+
$this->assertEquals("zipcontent", $zip->getContents());
277+
$this->assertEquals("GET", self::$container[self::$index]['request']->getMethod());
278+
$this->assertEquals("https://api.test.inetwork.com/v1.0/accounts/9500249/billingreports/a12b456c8-abcd-1a3b-a1b2-0a2b4c6d8e0f2/file", self::$container[self::$index]['request']->getUri());
279+
self::$index++;
280+
}
206281
}

0 commit comments

Comments
 (0)