Skip to content

Commit 2778a27

Browse files
committed
Run reports + class to manage responses
- Stream reports per line
1 parent db57eef commit 2778a27

4 files changed

Lines changed: 158 additions & 0 deletions

File tree

Atomx/AtomxClient.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Exception;
44
use GuzzleHttp\Message\Response;
5+
use GuzzleHttp\Stream\Stream;
56

67
class AtomxClient extends ApiClient {
78
protected $apiBase = null;
@@ -74,6 +75,10 @@ public function login()
7475
throw new ApiException('Unable to login to API!');
7576
}
7677

78+
if ($response instanceof Stream) {
79+
$response = json_decode($response->getContents(), true);
80+
}
81+
7782
$this->shouldSendToken = true;
7883

7984
if ($response['success'] !== true)

Atomx/ReportStreamer.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php namespace Atomx;
2+
3+
use Atomx\AccountStore;
4+
use GuzzleHttp\Stream\Stream;
5+
6+
7+
class ReportStreamer {
8+
private $stream;
9+
10+
private $overflow = '';
11+
private $columns = [];
12+
private $count = 0;
13+
14+
public function __construct(Stream $stream, $report = null)
15+
{
16+
if (!is_null($report)) {
17+
$this->columns = array_merge($report['report']['groups'], (isset($report['report']['sums']) ? $report['report']['sums'] : $report['report']['metrics']));
18+
$this->count = $report['report']['count'];
19+
}
20+
21+
$this->stream = $stream;
22+
}
23+
24+
public function getOverflow()
25+
{
26+
return $this->overflow;
27+
}
28+
29+
public function readLine()
30+
{
31+
$buffer = $this->overflow;
32+
33+
$this->overflow = '';
34+
35+
if (empty($buffer)) {
36+
if ($this->stream->eof())
37+
return false;
38+
}
39+
40+
while (($break = strpos($buffer, "\n")) === false && !$this->stream->eof()) {
41+
$buffer .= $this->stream->read(1024);
42+
}
43+
44+
if ($break !== false) {
45+
if (strlen($buffer) > $break+1)
46+
$this->overflow = substr($buffer, $break+1);
47+
48+
$buffer = substr($buffer, 0, $break);
49+
}
50+
$line = str_getcsv($buffer, "\t");
51+
52+
if (count($line) == 0)
53+
return false;
54+
55+
return $line;
56+
}
57+
}

Atomx/Resources/Report.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php namespace Atomx\Resources;
2+
3+
use Atomx\ApiException;
4+
use Atomx\AtomxClient;
5+
use Atomx\ReportStreamer;
6+
use GuzzleHttp\Message\Response;
7+
use GuzzleHttp\Stream\Stream;
8+
9+
class Report extends AtomxClient {
10+
private $returnStream = false;
11+
12+
// Run report
13+
public function run($json)
14+
{
15+
return $this->postUrl('report', compact('json'));
16+
}
17+
18+
public function status($reportId)
19+
{
20+
return $this->getUrl('report/' . $reportId, ['status' => true]);
21+
}
22+
23+
public function isReady($report)
24+
{
25+
if (isset($report['report']['is_ready'])) {
26+
return $report['report']['is_ready'];
27+
}
28+
29+
return false;
30+
}
31+
32+
public function download($report)
33+
{
34+
$reportId = $report['report']['id'];
35+
$stream = Stream::factory();
36+
37+
$this->returnStream = true;
38+
39+
$stream = $this->getUrl('report/' . $reportId, [], [
40+
// 'save_to' => $stream,
41+
// 'timeout' => 0,
42+
// 'connect_timeout' => 0
43+
]);
44+
45+
$this->returnStream = false;
46+
return new ReportStreamer($stream);
47+
}
48+
49+
protected function handleResponse(Response $response)
50+
{
51+
if ($response->getStatusCode() == 200) {
52+
$stream = $response->getBody();
53+
54+
if ($this->returnStream == true)
55+
return $stream;
56+
else
57+
return $stream->getContents();
58+
}
59+
60+
throw new ApiException('Request failed, received the following status: ' .
61+
$response->getStatusCode() . ' Body: ' . $response->getBody()->getContents());
62+
}
63+
}

Atomx/Response.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php namespace Atomx;
2+
3+
class Response {
4+
5+
private $namespace;
6+
private $response;
7+
8+
public function __construct($response)
9+
{
10+
$this->response = $response;
11+
$this->namespace = $response['resource'];
12+
}
13+
14+
public function isValid()
15+
{
16+
return $this->response['success'] == true;
17+
}
18+
19+
public function isMultiple()
20+
{
21+
return isset($this->response['count']);
22+
}
23+
24+
public function count()
25+
{
26+
return count($this->response['count']);
27+
}
28+
29+
public function __get($key)
30+
{
31+
return $this->response[$this->namespace][$key];
32+
}
33+
}

0 commit comments

Comments
 (0)